Skip to content

Commit 4c3cff6

Browse files
Improve std::any module doc
1 parent 46e7f4b commit 4c3cff6

File tree

1 file changed

+158
-2
lines changed

1 file changed

+158
-2
lines changed

src/libcore/any.rs

+158-2
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,23 @@ use marker::{Reflect, Sized};
9292
#[stable(feature = "rust1", since = "1.0.0")]
9393
pub trait Any: Reflect + 'static {
9494
/// Gets the `TypeId` of `self`.
95+
///
96+
/// # Examples
97+
///
98+
/// ```
99+
/// #![feature(get_type_id)]
100+
///
101+
/// use std::any::{Any, TypeId};
102+
///
103+
/// fn is_string(s: &Any) -> bool {
104+
/// TypeId::of::<String>() == s.get_type_id()
105+
/// }
106+
///
107+
/// fn main() {
108+
/// assert_eq!(is_string(&0), false);
109+
/// assert_eq!(is_string(&"cookie monster".to_owned()), true);
110+
/// }
111+
/// ```
95112
#[unstable(feature = "get_type_id",
96113
reason = "this method will likely be replaced by an associated static",
97114
issue = "27745")]
@@ -125,7 +142,26 @@ impl fmt::Debug for Any + Send {
125142
}
126143

127144
impl Any {
128-
/// Returns true if the boxed type is the same as `T`
145+
/// Returns true if the boxed type is the same as `T`.
146+
///
147+
/// # Examples
148+
///
149+
/// ```
150+
/// use std::any::Any;
151+
///
152+
/// fn is_string(s: &Any) {
153+
/// if s.is::<String>() {
154+
/// println!("It's a string!");
155+
/// } else {
156+
/// println!("Not a string...");
157+
/// }
158+
/// }
159+
///
160+
/// fn main() {
161+
/// is_string(&0);
162+
/// is_string(&"cookie monster".to_owned());
163+
/// }
164+
/// ```
129165
#[stable(feature = "rust1", since = "1.0.0")]
130166
#[inline]
131167
pub fn is<T: Any>(&self) -> bool {
@@ -141,6 +177,25 @@ impl Any {
141177

142178
/// Returns some reference to the boxed value if it is of type `T`, or
143179
/// `None` if it isn't.
180+
///
181+
/// # Examples
182+
///
183+
/// ```
184+
/// use std::any::Any;
185+
///
186+
/// fn print_if_string(s: &Any) {
187+
/// if let Some(string) = s.downcast_ref::<String>() {
188+
/// println!("It's a string({}): '{}'", string.len(), string);
189+
/// } else {
190+
/// println!("Not a string...");
191+
/// }
192+
/// }
193+
///
194+
/// fn main() {
195+
/// print_if_string(&0);
196+
/// print_if_string(&"cookie monster".to_owned());
197+
/// }
198+
/// ```
144199
#[stable(feature = "rust1", since = "1.0.0")]
145200
#[inline]
146201
pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
@@ -159,6 +214,29 @@ impl Any {
159214

160215
/// Returns some mutable reference to the boxed value if it is of type `T`, or
161216
/// `None` if it isn't.
217+
///
218+
/// # Examples
219+
///
220+
/// ```
221+
/// use std::any::Any;
222+
///
223+
/// fn modify_if_u32(s: &mut Any) {
224+
/// if let Some(num) = s.downcast_mut::<u32>() {
225+
/// *num = 42;
226+
/// }
227+
/// }
228+
///
229+
/// fn main() {
230+
/// let mut x = 10u32;
231+
/// let mut s = "starlord".to_owned();
232+
///
233+
/// modify_if_u32(&mut x);
234+
/// modify_if_u32(&mut s);
235+
///
236+
/// assert_eq!(x, 42);
237+
/// assert_eq!(&s, "starlord");
238+
/// }
239+
/// ```
162240
#[stable(feature = "rust1", since = "1.0.0")]
163241
#[inline]
164242
pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> {
@@ -178,20 +256,81 @@ impl Any {
178256

179257
impl Any+Send {
180258
/// Forwards to the method defined on the type `Any`.
259+
///
260+
/// # Examples
261+
///
262+
/// ```
263+
/// use std::any::Any;
264+
///
265+
/// fn is_string(s: &(Any + Send)) {
266+
/// if s.is::<String>() {
267+
/// println!("It's a string!");
268+
/// } else {
269+
/// println!("Not a string...");
270+
/// }
271+
/// }
272+
///
273+
/// fn main() {
274+
/// is_string(&0);
275+
/// is_string(&"cookie monster".to_owned());
276+
/// }
277+
/// ```
181278
#[stable(feature = "rust1", since = "1.0.0")]
182279
#[inline]
183280
pub fn is<T: Any>(&self) -> bool {
184281
Any::is::<T>(self)
185282
}
186283

187284
/// Forwards to the method defined on the type `Any`.
285+
///
286+
/// # Examples
287+
///
288+
/// ```
289+
/// use std::any::Any;
290+
///
291+
/// fn print_if_string(s: &(Any + Send)) {
292+
/// if let Some(string) = s.downcast_ref::<String>() {
293+
/// println!("It's a string({}): '{}'", string.len(), string);
294+
/// } else {
295+
/// println!("Not a string...");
296+
/// }
297+
/// }
298+
///
299+
/// fn main() {
300+
/// print_if_string(&0);
301+
/// print_if_string(&"cookie monster".to_owned());
302+
/// }
303+
/// ```
188304
#[stable(feature = "rust1", since = "1.0.0")]
189305
#[inline]
190306
pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
191307
Any::downcast_ref::<T>(self)
192308
}
193309

194310
/// Forwards to the method defined on the type `Any`.
311+
///
312+
/// # Examples
313+
///
314+
/// ```
315+
/// use std::any::Any;
316+
///
317+
/// fn modify_if_u32(s: &mut (Any+ Send)) {
318+
/// if let Some(num) = s.downcast_mut::<u32>() {
319+
/// *num = 42;
320+
/// }
321+
/// }
322+
///
323+
/// fn main() {
324+
/// let mut x = 10u32;
325+
/// let mut s = "starlord".to_owned();
326+
///
327+
/// modify_if_u32(&mut x);
328+
/// modify_if_u32(&mut s);
329+
///
330+
/// assert_eq!(x, 42);
331+
/// assert_eq!(&s, "starlord");
332+
/// }
333+
/// ```
195334
#[stable(feature = "rust1", since = "1.0.0")]
196335
#[inline]
197336
pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> {
@@ -220,7 +359,24 @@ pub struct TypeId {
220359

221360
impl TypeId {
222361
/// Returns the `TypeId` of the type this generic function has been
223-
/// instantiated with
362+
/// instantiated with.
363+
///
364+
/// # Examples
365+
///
366+
/// ```
367+
/// #![feature(get_type_id)]
368+
///
369+
/// use std::any::{Any, TypeId};
370+
///
371+
/// fn is_string(s: &Any) -> bool {
372+
/// TypeId::of::<String>() == s.get_type_id()
373+
/// }
374+
///
375+
/// fn main() {
376+
/// assert_eq!(is_string(&0), false);
377+
/// assert_eq!(is_string(&"cookie monster".to_owned()), true);
378+
/// }
379+
/// ```
224380
#[stable(feature = "rust1", since = "1.0.0")]
225381
pub fn of<T: ?Sized + Reflect + 'static>() -> TypeId {
226382
TypeId {

0 commit comments

Comments
 (0)