2014-08-04 05:48:39 -05:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2014-05-13 16:58:29 -05:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
//! A unique pointer type.
|
2014-05-13 16:58:29 -05:00
|
|
|
|
2014-05-13 18:10:05 -05:00
|
|
|
use core::any::{Any, AnyRefExt};
|
|
|
|
use core::clone::Clone;
|
2014-05-31 12:43:52 -05:00
|
|
|
use core::cmp::{PartialEq, PartialOrd, Eq, Ord, Ordering};
|
2014-05-13 18:10:05 -05:00
|
|
|
use core::default::Default;
|
|
|
|
use core::fmt;
|
|
|
|
use core::intrinsics;
|
|
|
|
use core::mem;
|
2014-06-18 01:25:51 -05:00
|
|
|
use core::option::Option;
|
2014-05-13 18:10:05 -05:00
|
|
|
use core::raw::TraitObject;
|
|
|
|
use core::result::{Ok, Err, Result};
|
2014-05-13 16:58:29 -05:00
|
|
|
|
|
|
|
/// A value that represents the global exchange heap. This is the default
|
|
|
|
/// place that the `box` keyword allocates into when no place is supplied.
|
|
|
|
///
|
|
|
|
/// The following two examples are equivalent:
|
|
|
|
///
|
2014-08-04 05:48:39 -05:00
|
|
|
/// ```rust
|
|
|
|
/// use std::boxed::HEAP;
|
2014-06-18 03:04:35 -05:00
|
|
|
///
|
2014-08-04 05:48:39 -05:00
|
|
|
/// # struct Bar;
|
|
|
|
/// # impl Bar { fn new(_a: int) { } }
|
|
|
|
/// let foo = box(HEAP) Bar::new(2);
|
|
|
|
/// let foo = box Bar::new(2);
|
|
|
|
/// ```
|
2014-07-10 16:19:17 -05:00
|
|
|
#[lang = "exchange_heap"]
|
|
|
|
#[experimental = "may be renamed; uncertain about custom allocator design"]
|
2014-05-13 16:58:29 -05:00
|
|
|
pub static HEAP: () = ();
|
|
|
|
|
|
|
|
/// A type that represents a uniquely-owned value.
|
2014-07-10 16:19:17 -05:00
|
|
|
#[lang = "owned_box"]
|
|
|
|
#[unstable = "custom allocators will add an additional type parameter (with default)"]
|
2014-06-25 14:47:34 -05:00
|
|
|
pub struct Box<T>(*mut T);
|
2014-05-13 16:58:29 -05:00
|
|
|
|
|
|
|
impl<T: Default> Default for Box<T> {
|
|
|
|
fn default() -> Box<T> { box Default::default() }
|
|
|
|
}
|
|
|
|
|
2014-06-23 18:34:29 -05:00
|
|
|
#[unstable]
|
2014-05-13 16:58:29 -05:00
|
|
|
impl<T: Clone> Clone for Box<T> {
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Returns a copy of the owned box.
|
2014-05-13 16:58:29 -05:00
|
|
|
#[inline]
|
|
|
|
fn clone(&self) -> Box<T> { box {(**self).clone()} }
|
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Performs copy-assignment from `source` by reusing the existing allocation.
|
2014-05-13 16:58:29 -05:00
|
|
|
#[inline]
|
|
|
|
fn clone_from(&mut self, source: &Box<T>) {
|
|
|
|
(**self).clone_from(&(**source));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-29 19:45:07 -05:00
|
|
|
impl<T:PartialEq> PartialEq for Box<T> {
|
2014-05-13 16:58:29 -05:00
|
|
|
#[inline]
|
|
|
|
fn eq(&self, other: &Box<T>) -> bool { *(*self) == *(*other) }
|
|
|
|
#[inline]
|
|
|
|
fn ne(&self, other: &Box<T>) -> bool { *(*self) != *(*other) }
|
|
|
|
}
|
2014-05-29 19:45:07 -05:00
|
|
|
impl<T:PartialOrd> PartialOrd for Box<T> {
|
2014-06-18 01:25:51 -05:00
|
|
|
#[inline]
|
|
|
|
fn partial_cmp(&self, other: &Box<T>) -> Option<Ordering> {
|
2014-07-07 18:35:15 -05:00
|
|
|
(**self).partial_cmp(&**other)
|
2014-06-18 01:25:51 -05:00
|
|
|
}
|
2014-05-13 16:58:29 -05:00
|
|
|
#[inline]
|
|
|
|
fn lt(&self, other: &Box<T>) -> bool { *(*self) < *(*other) }
|
|
|
|
#[inline]
|
|
|
|
fn le(&self, other: &Box<T>) -> bool { *(*self) <= *(*other) }
|
|
|
|
#[inline]
|
|
|
|
fn ge(&self, other: &Box<T>) -> bool { *(*self) >= *(*other) }
|
|
|
|
#[inline]
|
|
|
|
fn gt(&self, other: &Box<T>) -> bool { *(*self) > *(*other) }
|
|
|
|
}
|
2014-05-31 12:43:52 -05:00
|
|
|
impl<T: Ord> Ord for Box<T> {
|
2014-05-13 16:58:29 -05:00
|
|
|
#[inline]
|
2014-07-07 18:35:15 -05:00
|
|
|
fn cmp(&self, other: &Box<T>) -> Ordering {
|
|
|
|
(**self).cmp(&**other)
|
|
|
|
}
|
2014-05-13 16:58:29 -05:00
|
|
|
}
|
2014-05-31 12:43:52 -05:00
|
|
|
impl<T: Eq> Eq for Box<T> {}
|
2014-05-13 16:58:29 -05:00
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Extension methods for an owning `Any` trait object.
|
std: Stabilize unit, bool, ty, tuple, arc, any
This commit applies stability attributes to the contents of these modules,
summarized here:
* The `unit` and `bool` modules have become #[unstable] as they are purely meant
for documentation purposes and are candidates for removal.
* The `ty` module has been deprecated, and the inner `Unsafe` type has been
renamed to `UnsafeCell` and moved to the `cell` module. The `marker1` field
has been removed as the compiler now always infers `UnsafeCell` to be
invariant. The `new` method i stable, but the `value` field, `get` and
`unwrap` methods are all unstable.
* The `tuple` module has its name as stable, the naming of the `TupleN` traits
as stable while the methods are all #[unstable]. The other impls in the module
have appropriate stability for the corresponding trait.
* The `arc` module has received the exact same treatment as the `rc` module
previously did.
* The `any` module has its name as stable. The `Any` trait is also stable, with
a new private supertrait which now contains the `get_type_id` method. This is
to make the method a private implementation detail rather than a public-facing
detail.
The two extension traits in the module are marked #[unstable] as they will not
be necessary with DST. The `is` method is #[stable], the as_{mut,ref} methods
have been renamed to downcast_{mut,ref} and are #[unstable].
The extension trait `BoxAny` has been clarified as to why it is unstable as it
will not be necessary with DST.
This is a breaking change because the `marker1` field was removed from the
`UnsafeCell` type. To deal with this change, you can simply delete the field and
only specify the value of the `data` field in static initializers.
[breaking-change]
2014-07-23 21:10:12 -05:00
|
|
|
#[unstable = "post-DST and coherence changes, this will not be a trait but \
|
|
|
|
rather a direct `impl` on `Box<Any>`"]
|
2014-07-10 16:19:17 -05:00
|
|
|
pub trait BoxAny {
|
2014-05-13 16:58:29 -05:00
|
|
|
/// Returns the boxed value if it is of type `T`, or
|
|
|
|
/// `Err(Self)` if it isn't.
|
std: Stabilize unit, bool, ty, tuple, arc, any
This commit applies stability attributes to the contents of these modules,
summarized here:
* The `unit` and `bool` modules have become #[unstable] as they are purely meant
for documentation purposes and are candidates for removal.
* The `ty` module has been deprecated, and the inner `Unsafe` type has been
renamed to `UnsafeCell` and moved to the `cell` module. The `marker1` field
has been removed as the compiler now always infers `UnsafeCell` to be
invariant. The `new` method i stable, but the `value` field, `get` and
`unwrap` methods are all unstable.
* The `tuple` module has its name as stable, the naming of the `TupleN` traits
as stable while the methods are all #[unstable]. The other impls in the module
have appropriate stability for the corresponding trait.
* The `arc` module has received the exact same treatment as the `rc` module
previously did.
* The `any` module has its name as stable. The `Any` trait is also stable, with
a new private supertrait which now contains the `get_type_id` method. This is
to make the method a private implementation detail rather than a public-facing
detail.
The two extension traits in the module are marked #[unstable] as they will not
be necessary with DST. The `is` method is #[stable], the as_{mut,ref} methods
have been renamed to downcast_{mut,ref} and are #[unstable].
The extension trait `BoxAny` has been clarified as to why it is unstable as it
will not be necessary with DST.
This is a breaking change because the `marker1` field was removed from the
`UnsafeCell` type. To deal with this change, you can simply delete the field and
only specify the value of the `data` field in static initializers.
[breaking-change]
2014-07-23 21:10:12 -05:00
|
|
|
#[unstable = "naming conventions around accessing innards may change"]
|
2014-07-10 16:19:17 -05:00
|
|
|
fn downcast<T: 'static>(self) -> Result<Box<T>, Self>;
|
2014-05-13 16:58:29 -05:00
|
|
|
}
|
2014-05-11 13:14:14 -05:00
|
|
|
|
std: Stabilize unit, bool, ty, tuple, arc, any
This commit applies stability attributes to the contents of these modules,
summarized here:
* The `unit` and `bool` modules have become #[unstable] as they are purely meant
for documentation purposes and are candidates for removal.
* The `ty` module has been deprecated, and the inner `Unsafe` type has been
renamed to `UnsafeCell` and moved to the `cell` module. The `marker1` field
has been removed as the compiler now always infers `UnsafeCell` to be
invariant. The `new` method i stable, but the `value` field, `get` and
`unwrap` methods are all unstable.
* The `tuple` module has its name as stable, the naming of the `TupleN` traits
as stable while the methods are all #[unstable]. The other impls in the module
have appropriate stability for the corresponding trait.
* The `arc` module has received the exact same treatment as the `rc` module
previously did.
* The `any` module has its name as stable. The `Any` trait is also stable, with
a new private supertrait which now contains the `get_type_id` method. This is
to make the method a private implementation detail rather than a public-facing
detail.
The two extension traits in the module are marked #[unstable] as they will not
be necessary with DST. The `is` method is #[stable], the as_{mut,ref} methods
have been renamed to downcast_{mut,ref} and are #[unstable].
The extension trait `BoxAny` has been clarified as to why it is unstable as it
will not be necessary with DST.
This is a breaking change because the `marker1` field was removed from the
`UnsafeCell` type. To deal with this change, you can simply delete the field and
only specify the value of the `data` field in static initializers.
[breaking-change]
2014-07-23 21:10:12 -05:00
|
|
|
#[stable]
|
2014-08-27 20:46:52 -05:00
|
|
|
impl BoxAny for Box<Any+'static> {
|
2014-06-25 20:18:13 -05:00
|
|
|
#[inline]
|
2014-08-27 20:46:52 -05:00
|
|
|
fn downcast<T: 'static>(self) -> Result<Box<T>, Box<Any+'static>> {
|
2014-06-25 20:18:13 -05:00
|
|
|
if self.is::<T>() {
|
|
|
|
unsafe {
|
|
|
|
// Get the raw representation of the trait object
|
|
|
|
let to: TraitObject =
|
2014-07-10 16:19:17 -05:00
|
|
|
*mem::transmute::<&Box<Any>, &TraitObject>(&self);
|
2014-06-25 20:18:13 -05:00
|
|
|
|
|
|
|
// Prevent destructor on self being run
|
|
|
|
intrinsics::forget(self);
|
|
|
|
|
|
|
|
// Extract the data pointer
|
|
|
|
Ok(mem::transmute(to.data))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-11 13:14:14 -05:00
|
|
|
impl<T: fmt::Show> fmt::Show for Box<T> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
(**self).fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-27 20:46:52 -05:00
|
|
|
impl fmt::Show for Box<Any+'static> {
|
2014-05-11 13:14:14 -05:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.pad("Box<Any>")
|
|
|
|
}
|
|
|
|
}
|
2014-06-28 15:57:36 -05:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
#[test]
|
|
|
|
fn test_owned_clone() {
|
|
|
|
let a = box 5i;
|
|
|
|
let b: Box<int> = a.clone();
|
|
|
|
assert!(a == b);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn any_move() {
|
|
|
|
let a = box 8u as Box<Any>;
|
|
|
|
let b = box Test as Box<Any>;
|
|
|
|
|
2014-07-10 16:19:17 -05:00
|
|
|
match a.downcast::<uint>() {
|
2014-06-28 15:57:36 -05:00
|
|
|
Ok(a) => { assert!(a == box 8u); }
|
|
|
|
Err(..) => fail!()
|
|
|
|
}
|
2014-07-10 16:19:17 -05:00
|
|
|
match b.downcast::<Test>() {
|
2014-06-28 15:57:36 -05:00
|
|
|
Ok(a) => { assert!(a == box Test); }
|
|
|
|
Err(..) => fail!()
|
|
|
|
}
|
|
|
|
|
|
|
|
let a = box 8u as Box<Any>;
|
|
|
|
let b = box Test as Box<Any>;
|
|
|
|
|
2014-07-10 16:19:17 -05:00
|
|
|
assert!(a.downcast::<Box<Test>>().is_err());
|
|
|
|
assert!(b.downcast::<Box<uint>>().is_err());
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_show() {
|
|
|
|
let a = box 8u as Box<Any>;
|
|
|
|
let b = box Test as Box<Any>;
|
|
|
|
let a_str = a.to_str();
|
|
|
|
let b_str = b.to_str();
|
|
|
|
assert_eq!(a_str.as_slice(), "Box<Any>");
|
|
|
|
assert_eq!(b_str.as_slice(), "Box<Any>");
|
|
|
|
|
|
|
|
let a = &8u as &Any;
|
|
|
|
let b = &Test as &Any;
|
|
|
|
let s = format!("{}", a);
|
|
|
|
assert_eq!(s.as_slice(), "&Any");
|
|
|
|
let s = format!("{}", b);
|
|
|
|
assert_eq!(s.as_slice(), "&Any");
|
|
|
|
}
|
|
|
|
}
|