|
10 | 10 |
|
11 | 11 | //! Operations on unique pointer types |
12 | 12 |
|
13 | | -// FIXME: this module should not exist in libcore. It must currently because the |
14 | | -// Box implementation is quite ad-hoc in the compiler. Once there is |
15 | | -// proper support in the compiler this type will be able to be defined in |
16 | | -// its own module. |
| 13 | +use any::{Any, AnyRefExt}; |
| 14 | +use clone::Clone; |
| 15 | +use cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering}; |
| 16 | +use default::Default; |
| 17 | +use intrinsics; |
| 18 | +use mem; |
| 19 | +use raw::TraitObject; |
| 20 | +use result::{Ok, Err, Result}; |
17 | 21 |
|
18 | 22 | /// A value that represents the global exchange heap. This is the default |
19 | 23 | /// place that the `box` keyword allocates into when no place is supplied. |
|
23 | 27 | /// let foo = box(HEAP) Bar::new(...); |
24 | 28 | /// let foo = box Bar::new(...); |
25 | 29 | #[lang="exchange_heap"] |
26 | | -#[cfg(not(test))] |
27 | | -pub static HEAP: () = (); |
28 | | - |
29 | | -#[cfg(test)] |
30 | 30 | pub static HEAP: () = (); |
31 | 31 |
|
32 | 32 | /// A type that represents a uniquely-owned value. |
33 | 33 | #[lang="owned_box"] |
34 | | -#[cfg(not(test))] |
35 | 34 | pub struct Box<T>(*T); |
36 | 35 |
|
37 | | -#[cfg(test)] |
38 | | -pub struct Box<T>(*T); |
| 36 | +impl<T: Default> Default for Box<T> { |
| 37 | + fn default() -> Box<T> { box Default::default() } |
| 38 | +} |
| 39 | + |
| 40 | +impl<T: Clone> Clone for Box<T> { |
| 41 | + /// Return a copy of the owned box. |
| 42 | + #[inline] |
| 43 | + fn clone(&self) -> Box<T> { box {(**self).clone()} } |
| 44 | + |
| 45 | + /// Perform copy-assignment from `source` by reusing the existing allocation. |
| 46 | + #[inline] |
| 47 | + fn clone_from(&mut self, source: &Box<T>) { |
| 48 | + (**self).clone_from(&(**source)); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// box pointers |
| 53 | +impl<T:Eq> Eq for Box<T> { |
| 54 | + #[inline] |
| 55 | + fn eq(&self, other: &Box<T>) -> bool { *(*self) == *(*other) } |
| 56 | + #[inline] |
| 57 | + fn ne(&self, other: &Box<T>) -> bool { *(*self) != *(*other) } |
| 58 | +} |
| 59 | +impl<T:Ord> Ord for Box<T> { |
| 60 | + #[inline] |
| 61 | + fn lt(&self, other: &Box<T>) -> bool { *(*self) < *(*other) } |
| 62 | + #[inline] |
| 63 | + fn le(&self, other: &Box<T>) -> bool { *(*self) <= *(*other) } |
| 64 | + #[inline] |
| 65 | + fn ge(&self, other: &Box<T>) -> bool { *(*self) >= *(*other) } |
| 66 | + #[inline] |
| 67 | + fn gt(&self, other: &Box<T>) -> bool { *(*self) > *(*other) } |
| 68 | +} |
| 69 | +impl<T: TotalOrd> TotalOrd for Box<T> { |
| 70 | + #[inline] |
| 71 | + fn cmp(&self, other: &Box<T>) -> Ordering { (**self).cmp(*other) } |
| 72 | +} |
| 73 | +impl<T: TotalEq> TotalEq for Box<T> {} |
| 74 | + |
| 75 | +/// Extension methods for an owning `Any` trait object |
| 76 | +pub trait AnyOwnExt { |
| 77 | + /// Returns the boxed value if it is of type `T`, or |
| 78 | + /// `Err(Self)` if it isn't. |
| 79 | + fn move<T: 'static>(self) -> Result<Box<T>, Self>; |
| 80 | +} |
| 81 | + |
| 82 | +impl AnyOwnExt for Box<Any> { |
| 83 | + #[inline] |
| 84 | + fn move<T: 'static>(self) -> Result<Box<T>, Box<Any>> { |
| 85 | + if self.is::<T>() { |
| 86 | + unsafe { |
| 87 | + // Get the raw representation of the trait object |
| 88 | + let to: TraitObject = |
| 89 | + *mem::transmute::<&Box<Any>, &TraitObject>(&self); |
| 90 | + |
| 91 | + // Prevent destructor on self being run |
| 92 | + intrinsics::forget(self); |
| 93 | + |
| 94 | + // Extract the data pointer |
| 95 | + Ok(mem::transmute(to.data)) |
| 96 | + } |
| 97 | + } else { |
| 98 | + Err(self) |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments