@@ -92,6 +92,23 @@ use marker::{Reflect, Sized};
92
92
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
93
93
pub trait Any : Reflect + ' static {
94
94
/// 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
+ /// ```
95
112
#[ unstable( feature = "get_type_id" ,
96
113
reason = "this method will likely be replaced by an associated static" ,
97
114
issue = "27745" ) ]
@@ -125,7 +142,26 @@ impl fmt::Debug for Any + Send {
125
142
}
126
143
127
144
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
+ /// ```
129
165
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
130
166
#[ inline]
131
167
pub fn is < T : Any > ( & self ) -> bool {
@@ -141,6 +177,25 @@ impl Any {
141
177
142
178
/// Returns some reference to the boxed value if it is of type `T`, or
143
179
/// `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
+ /// ```
144
199
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
145
200
#[ inline]
146
201
pub fn downcast_ref < T : Any > ( & self ) -> Option < & T > {
@@ -159,6 +214,29 @@ impl Any {
159
214
160
215
/// Returns some mutable reference to the boxed value if it is of type `T`, or
161
216
/// `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
+ /// ```
162
240
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
163
241
#[ inline]
164
242
pub fn downcast_mut < T : Any > ( & mut self ) -> Option < & mut T > {
@@ -178,20 +256,81 @@ impl Any {
178
256
179
257
impl Any +Send {
180
258
/// 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
+ /// ```
181
278
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
182
279
#[ inline]
183
280
pub fn is < T : Any > ( & self ) -> bool {
184
281
Any :: is :: < T > ( self )
185
282
}
186
283
187
284
/// 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
+ /// ```
188
304
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
189
305
#[ inline]
190
306
pub fn downcast_ref < T : Any > ( & self ) -> Option < & T > {
191
307
Any :: downcast_ref :: < T > ( self )
192
308
}
193
309
194
310
/// 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
+ /// ```
195
334
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
196
335
#[ inline]
197
336
pub fn downcast_mut < T : Any > ( & mut self ) -> Option < & mut T > {
@@ -220,7 +359,24 @@ pub struct TypeId {
220
359
221
360
impl TypeId {
222
361
/// 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
+ /// ```
224
380
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
225
381
pub fn of < T : ?Sized + Reflect + ' static > ( ) -> TypeId {
226
382
TypeId {
0 commit comments