-// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
+// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
use core::clone::Clone;
use core::cmp::{PartialEq, PartialOrd, Eq, Ord, Ordering};
use core::default::Default;
+use core::error::{Error, FromError};
use core::fmt;
use core::hash::{self, Hash};
use core::marker::Sized;
use core::mem;
+use core::ops::{Deref, DerefMut};
use core::option::Option;
use core::ptr::Unique;
use core::raw::TraitObject;
-use core::result::Result;
use core::result::Result::{Ok, Err};
-use core::ops::{Deref, DerefMut};
+use core::result::Result;
/// 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:
///
/// ```rust
+/// #![feature(box_syntax)]
/// use std::boxed::HEAP;
///
+/// fn main() {
/// # struct Bar;
/// # impl Bar { fn new(_a: int) { } }
-/// let foo = box(HEAP) Bar::new(2);
-/// let foo = box Bar::new(2);
+/// let foo = box(HEAP) Bar::new(2);
+/// let foo = box Bar::new(2);
+/// }
/// ```
#[lang = "exchange_heap"]
-#[experimental = "may be renamed; uncertain about custom allocator design"]
+#[unstable = "may be renamed; uncertain about custom allocator design"]
pub static HEAP: () = ();
/// A type that represents a uniquely-owned value.
#[stable]
pub struct Box<T>(Unique<T>);
+impl<T> Box<T> {
+ /// Moves `x` into a freshly allocated box on the global exchange heap.
+ #[stable]
+ pub fn new(x: T) -> Box<T> {
+ box x
+ }
+}
+
#[stable]
impl<T: Default> Default for Box<T> {
#[stable]
fn cmp(&self, other: &Box<T>) -> Ordering {
Ord::cmp(&**self, &**other)
}
-
-#[stable]}
+}
+#[stable]
impl<T: ?Sized + Eq> Eq for Box<T> {}
-#[cfg(stage0)]
-impl<S: hash::Writer, T: ?Sized + Hash<S>> Hash<S> for Box<T> {
- #[inline]
- fn hash(&self, state: &mut S) {
- (**self).hash(state);
- }
-}
-#[cfg(not(stage0))]
impl<S: hash::Hasher, T: ?Sized + Hash<S>> Hash<S> for Box<T> {
#[inline]
fn hash(&self, state: &mut S) {
}
/// Extension methods for an owning `Any` trait object.
-#[unstable = "post-DST and coherence changes, this will not be a trait but \
- rather a direct `impl` on `Box<Any>`"]
+#[unstable = "this trait will likely disappear once compiler bugs blocking \
+ a direct impl on `Box<Any>` have been fixed "]
+// FIXME(#18737): this should be a direct impl on `Box<Any>`. If you're
+// removing this please make sure that you can downcase on
+// `Box<Any + Send>` as well as `Box<Any>`
pub trait BoxAny {
/// Returns the boxed value if it is of type `T`, or
/// `Err(Self)` if it isn't.
fn downcast<T: 'static>(self) -> Result<Box<T>, Self>;
}
+#[stable]
impl BoxAny for Box<Any> {
#[inline]
- #[unstable = "method may be renamed with respect to other downcasting \
- methods"]
fn downcast<T: 'static>(self) -> Result<Box<T>, Box<Any>> {
if self.is::<T>() {
unsafe {
}
}
-impl<T: ?Sized + fmt::Show> fmt::Show for Box<T> {
+#[stable]
+impl<T: fmt::Display + ?Sized> fmt::Display for Box<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write!(f, "Box({:?})", &**self)
+ fmt::Display::fmt(&**self, f)
}
}
-impl<T: ?Sized + fmt::String> fmt::String for Box<T> {
+#[stable]
+impl<T: fmt::Debug + ?Sized> fmt::Debug for Box<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- fmt::String::fmt(&**self, f)
+ fmt::Debug::fmt(&**self, f)
}
}
-impl fmt::Show for Box<Any> {
+#[stable]
+impl fmt::Debug for Box<Any> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.pad("Box<Any>")
}
fn deref_mut(&mut self) -> &mut T { &mut **self }
}
+impl<'a, E: Error + 'a> FromError<E> for Box<Error + 'a> {
+ fn from_error(err: E) -> Box<Error + 'a> {
+ Box::new(err)
+ }
+}
+
#[cfg(test)]
mod test {
#[test]
fn test_owned_clone() {
- let a = box 5i;
+ let a = Box::new(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>;
+ let a = Box::new(8u) as Box<Any>;
+ let b = Box::new(Test) as Box<Any>;
match a.downcast::<uint>() {
- Ok(a) => { assert!(a == box 8u); }
+ Ok(a) => { assert!(a == Box::new(8u)); }
Err(..) => panic!()
}
match b.downcast::<Test>() {
- Ok(a) => { assert!(a == box Test); }
+ Ok(a) => { assert!(a == Box::new(Test)); }
Err(..) => panic!()
}
- let a = box 8u as Box<Any>;
- let b = box Test as Box<Any>;
+ let a = Box::new(8u) as Box<Any>;
+ let b = Box::new(Test) as Box<Any>;
assert!(a.downcast::<Box<Test>>().is_err());
assert!(b.downcast::<Box<uint>>().is_err());
#[test]
fn test_show() {
- let a = box 8u as Box<Any>;
- let b = box Test as Box<Any>;
+ let a = Box::new(8u) as Box<Any>;
+ let b = Box::new(Test) as Box<Any>;
let a_str = a.to_str();
let b_str = b.to_str();
assert_eq!(a_str, "Box<Any>");
#[test]
fn deref() {
fn homura<T: Deref<Target=i32>>(_: T) { }
- homura(box 765i32);
+ homura(Box::new(765i32));
}
}