1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! This module implements the `Any` trait, which enables dynamic typing
15 use option::{Option, Some, None};
17 use unstable::intrinsics;
19 use unstable::intrinsics::TypeId;
21 ///////////////////////////////////////////////////////////////////////////////
23 ///////////////////////////////////////////////////////////////////////////////
25 /// The `Any` trait is implemented by all types, and can be used as a trait object
26 /// for dynamic typing
28 /// Get the `TypeId` of `self`
29 fn get_type_id(&self) -> TypeId;
31 /// Get a void pointer to `self`
32 fn as_void_ptr(&self) -> *Void;
34 /// Get a mutable void pointer to `self`
35 fn as_mut_void_ptr(&mut self) -> *mut Void;
38 impl<T: 'static> Any for T {
39 /// Get the `TypeId` of `self`
40 fn get_type_id(&self) -> TypeId {
44 /// Get a void pointer to `self`
45 fn as_void_ptr(&self) -> *Void {
49 /// Get a mutable void pointer to `self`
50 fn as_mut_void_ptr(&mut self) -> *mut Void {
51 self as *mut T as *mut Void
55 ///////////////////////////////////////////////////////////////////////////////
56 // Extension methods for Any trait objects.
57 // Implemented as three extension traits so that generics work.
58 ///////////////////////////////////////////////////////////////////////////////
60 /// Extension methods for a referenced `Any` trait object
61 pub trait AnyRefExt<'self> {
62 /// Returns true if the boxed type is the same as `T`
63 fn is<T: 'static>(self) -> bool;
65 /// Returns some reference to the boxed value if it is of type `T`, or
66 /// `None` if it isn't.
67 fn as_ref<T: 'static>(self) -> Option<&'self T>;
70 impl<'self> AnyRefExt<'self> for &'self Any {
72 fn is<T: 'static>(self) -> bool {
73 // Get TypeId of the type this function is instantiated with
74 let t = TypeId::of::<T>();
76 // Get TypeId of the type in the trait object
77 let boxed = self.get_type_id();
79 // Compare both TypeIds on equality
84 fn as_ref<T: 'static>(self) -> Option<&'self T> {
86 Some(unsafe { transmute(self.as_void_ptr()) })
93 /// Extension methods for a mutable referenced `Any` trait object
94 pub trait AnyMutRefExt<'self> {
95 /// Returns some mutable reference to the boxed value if it is of type `T`, or
96 /// `None` if it isn't.
97 fn as_mut<T: 'static>(self) -> Option<&'self mut T>;
100 impl<'self> AnyMutRefExt<'self> for &'self mut Any {
102 fn as_mut<T: 'static>(self) -> Option<&'self mut T> {
104 Some(unsafe { transmute(self.as_mut_void_ptr()) })
111 /// Extension methods for a owning `Any` trait object
112 pub trait AnyOwnExt {
113 /// Returns the boxed value if it is of type `T`, or
114 /// `None` if it isn't.
115 fn move<T: 'static>(self) -> Option<~T>;
118 impl AnyOwnExt for ~Any {
120 fn move<T: 'static>(self) -> Option<~T> {
123 // Extract the pointer to the boxed value, temporary alias with self
124 let ptr: ~T = transmute(self.as_void_ptr());
126 // Prevent destructor on self being run
127 intrinsics::forget(self);
137 ///////////////////////////////////////////////////////////////////////////////
138 // Trait implementations
139 ///////////////////////////////////////////////////////////////////////////////
141 impl ToStr for ~Any {
142 fn to_str(&self) -> ~str { ~"~Any" }
145 impl<'self> ToStr for &'self Any {
146 fn to_str(&self) -> ~str { ~"&Any" }
152 use super::AnyRefExt;
153 use option::{Some, None};
159 static TEST: &'static str = "Test";
162 fn any_as_void_ptr() {
163 let (a, b, c) = (~5u as ~Any, ~TEST as ~Any, ~Test as ~Any);
168 assert_eq!(a.as_void_ptr(), a_r.as_void_ptr());
169 assert_eq!(b.as_void_ptr(), b_r.as_void_ptr());
170 assert_eq!(c.as_void_ptr(), c_r.as_void_ptr());
172 let (a, b, c) = (@5u as @Any, @TEST as @Any, @Test as @Any);
177 assert_eq!(a.as_void_ptr(), a_r.as_void_ptr());
178 assert_eq!(b.as_void_ptr(), b_r.as_void_ptr());
179 assert_eq!(c.as_void_ptr(), c_r.as_void_ptr());
181 let (a, b, c) = (&5u as &Any, &TEST as &Any, &Test as &Any);
186 assert_eq!(a.as_void_ptr(), a_r.as_void_ptr());
187 assert_eq!(b.as_void_ptr(), b_r.as_void_ptr());
188 assert_eq!(c.as_void_ptr(), c_r.as_void_ptr());
191 let mut y: &'static str = "Test";
192 let (a, b, c) = (&mut 5u as &mut Any,
199 assert_eq!(a.as_void_ptr(), a_r.as_void_ptr());
200 assert_eq!(b.as_void_ptr(), b_r.as_void_ptr());
201 assert_eq!(c.as_void_ptr(), c_r.as_void_ptr());
203 let (a, b, c) = (5u, "hello", Test);
204 let (a_r, b_r, c_r) = (&a as &Any, &b as &Any, &c as &Any);
206 assert_eq!(a.as_void_ptr(), a_r.as_void_ptr());
207 assert_eq!(b.as_void_ptr(), b_r.as_void_ptr());
208 assert_eq!(c.as_void_ptr(), c_r.as_void_ptr());
212 fn any_as_mut_void_ptr() {
213 let y: &'static str = "Test";
214 let mut a = ~5u as ~Any;
215 let mut b = ~y as ~Any;
216 let mut c = ~Test as ~Any;
218 let a_ptr = a.as_mut_void_ptr();
219 let b_ptr = b.as_mut_void_ptr();
220 let c_ptr = c.as_mut_void_ptr();
222 let a_r: &mut Any = a;
223 let b_r: &mut Any = b;
224 let c_r: &mut Any = c;
226 assert_eq!(a_ptr, a_r.as_mut_void_ptr());
227 assert_eq!(b_ptr, b_r.as_mut_void_ptr());
228 assert_eq!(c_ptr, c_r.as_mut_void_ptr());
231 let mut y: &'static str = "Test";
232 let a = &mut 5u as &mut Any;
233 let b = &mut y as &mut Any;
234 let c = &mut x as &mut Any;
236 let a_ptr = a.as_mut_void_ptr();
237 let b_ptr = b.as_mut_void_ptr();
238 let c_ptr = c.as_mut_void_ptr();
240 let a_r: &mut Any = a;
241 let b_r: &mut Any = b;
242 let c_r: &mut Any = c;
244 assert_eq!(a_ptr, a_r.as_mut_void_ptr());
245 assert_eq!(b_ptr, b_r.as_mut_void_ptr());
246 assert_eq!(c_ptr, c_r.as_mut_void_ptr());
248 let y: &'static str = "Test";
253 let a_ptr = a.as_mut_void_ptr();
254 let b_ptr = b.as_mut_void_ptr();
255 let c_ptr = c.as_mut_void_ptr();
257 let (a_r, b_r, c_r) = (&mut a as &mut Any, &mut b as &mut Any, &mut c as &mut Any);
259 assert_eq!(a_ptr, a_r.as_mut_void_ptr());
260 assert_eq!(b_ptr, b_r.as_mut_void_ptr());
261 assert_eq!(c_ptr, c_r.as_mut_void_ptr());
265 fn any_referenced() {
266 let (a, b, c) = (&5u as &Any, &TEST as &Any, &Test as &Any);
268 assert!(a.is::<uint>());
269 assert!(!b.is::<uint>());
270 assert!(!c.is::<uint>());
272 assert!(!a.is::<&'static str>());
273 assert!(b.is::<&'static str>());
274 assert!(!c.is::<&'static str>());
276 assert!(!a.is::<Test>());
277 assert!(!b.is::<Test>());
278 assert!(c.is::<Test>());
283 let (a, b, c) = (~5u as ~Any, ~TEST as ~Any, ~Test as ~Any);
285 assert!(a.is::<uint>());
286 assert!(!b.is::<uint>());
287 assert!(!c.is::<uint>());
289 assert!(!a.is::<&'static str>());
290 assert!(b.is::<&'static str>());
291 assert!(!c.is::<&'static str>());
293 assert!(!a.is::<Test>());
294 assert!(!b.is::<Test>());
295 assert!(c.is::<Test>());
300 let (a, b, c) = (@5u as @Any, @TEST as @Any, @Test as @Any);
302 assert!(a.is::<uint>());
303 assert!(!b.is::<uint>());
304 assert!(!c.is::<uint>());
306 assert!(!a.is::<&'static str>());
307 assert!(b.is::<&'static str>());
308 assert!(!c.is::<&'static str>());
310 assert!(!a.is::<Test>());
311 assert!(!b.is::<Test>());
312 assert!(c.is::<Test>());
319 match a.as_ref::<uint>() {
321 x => fail!("Unexpected value {:?}", x)
324 match a.as_ref::<Test>() {
326 x => fail!("Unexpected value {:?}", x)
335 let a_r = &mut a as &mut Any;
336 let tmp: &mut uint = b;
337 let b_r = tmp as &mut Any;
339 match a_r.as_mut::<uint>() {
344 x => fail!("Unexpected value {:?}", x)
347 match b_r.as_mut::<uint>() {
352 x => fail!("Unexpected value {:?}", x)
355 match a_r.as_mut::<Test>() {
357 x => fail!("Unexpected value {:?}", x)
360 match b_r.as_mut::<Test>() {
362 x => fail!("Unexpected value {:?}", x)
365 match a_r.as_mut::<uint>() {
367 x => fail!("Unexpected value {:?}", x)
370 match b_r.as_mut::<uint>() {
372 x => fail!("Unexpected value {:?}", x)
379 let b = ~Test as ~Any;
381 assert_eq!(a.move(), Some(~8u));
382 assert_eq!(b.move(), Some(~Test));
385 let b = ~Test as ~Any;
387 assert_eq!(a.move(), None::<~Test>);
388 assert_eq!(b.move(), None::<~uint>);