Skip to content

Commit c0b7f28

Browse files
committed
---
yaml --- r: 123865 b: refs/heads/snap-stage3 c: f9d3b9e h: refs/heads/master i: 123863: 6a9e592 v: v3
1 parent b7ff256 commit c0b7f28

File tree

2 files changed

+56
-7
lines changed

2 files changed

+56
-7
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: 4e2da7cb79143b0e7206a684629ed942599ec8e9
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: be7a17062b103b31798afcd525c51a9642038465
4+
refs/heads/snap-stage3: f9d3b9e488f88b5d9c9e23f9bcc7e933565a9649
55
refs/heads/try: 296eb104620b346d88bc4a2c2ab7693e6d3db019
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b

branches/snap-stage3/src/libcore/any.rs

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,61 @@
1313
//! This module implements the `Any` trait, which enables dynamic typing
1414
//! of any `'static` type through runtime reflection.
1515
//!
16-
//! `Any` itself can be used to get a `TypeId`, and has more features when used as a trait object.
17-
//! As `&Any` (a borrowed trait object), it has the `is` and `as_ref` methods, to test if the
18-
//! contained value is of a given type, and to get a reference to the inner value as a type. As
19-
//! `&mut Any`, there is also the `as_mut` method, for getting a mutable reference to the inner
20-
//! value. `Box<Any>` adds the `move` method, which will unwrap a `Box<T>` from the object. See
21-
//! the extension traits (`*Ext`) for the full details.
16+
//! `Any` itself can be used to get a `TypeId`, and has more features when used
17+
//! as a trait object. As `&Any` (a borrowed trait object), it has the `is` and
18+
//! `as_ref` methods, to test if the contained value is of a given type, and to
19+
//! get a reference to the inner value as a type. As`&mut Any`, there is also
20+
//! the `as_mut` method, for getting a mutable reference to the inner value.
21+
//! `Box<Any>` adds the `move` method, which will unwrap a `Box<T>` from the
22+
//! object. See the extension traits (`*Ext`) for the full details.
23+
//!
24+
//! Note that &Any is limited to testing whether a value is of a specified
25+
//! concrete type, and cannot be used to test whether a type implements a trait.
26+
//!
27+
//! # Examples
28+
//!
29+
//! Consider a situation where we want to log out a value passed to a function.
30+
//! We know the value we're working on implements Show, but we don't know its
31+
//! concrete type. We want to give special treatment to certain types: in this
32+
//! case printing out the length of String values prior to their value.
33+
//! We don't know the concrete type of our value at compile time, so we need to
34+
//! use runtime reflection instead.
35+
//!
36+
//! ```rust
37+
//! use std::fmt::Show;
38+
//! use std::any::{Any, AnyRefExt};
39+
//!
40+
//! // Logger function for any type that implements Show.
41+
//! fn log<T: Any+Show>(value: &T) {
42+
//! let value_any = value as &Any;
43+
//!
44+
//! // try to convert our value to a String. If successful, we want to
45+
//! // output the String's length as well as its value. If not, it's a
46+
//! // different type: just print it out unadorned.
47+
//! match value_any.as_ref::<String>() {
48+
//! Some(as_string) => {
49+
//! println!("String ({}): {}", as_string.len(), as_string);
50+
//! }
51+
//! None => {
52+
//! println!("{}", value);
53+
//! }
54+
//! }
55+
//! }
56+
//!
57+
//! // This function wants to log its parameter out prior to doing work with it.
58+
//! fn do_work<T: Show>(value: &T) {
59+
//! log(value);
60+
//! // ...do some other work
61+
//! }
62+
//!
63+
//! fn main() {
64+
//! let my_string = "Hello World".to_string();
65+
//! do_work(&my_string);
66+
//!
67+
//! let my_i8: i8 = 100;
68+
//! do_work(&my_i8);
69+
//! }
70+
//! ```
2271
2372
use mem::{transmute, transmute_copy};
2473
use option::{Option, Some, None};

0 commit comments

Comments
 (0)