2015-01-07 14:37:07 -06:00
|
|
|
// Copyright 2012-2015 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
|
|
|
|
2015-01-12 20:40:19 -06:00
|
|
|
#![stable(feature = "grandfathered", since = "1.0.0")]
|
2015-01-02 00:24:06 -06:00
|
|
|
|
2015-01-01 00:13:08 -06:00
|
|
|
use core::any::Any;
|
2014-05-13 18:10:05 -05:00
|
|
|
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;
|
2015-01-03 21:42:21 -06:00
|
|
|
use core::hash::{self, Hash};
|
2015-01-06 16:33:42 -06:00
|
|
|
use core::marker::Sized;
|
2014-05-13 18:10:05 -05:00
|
|
|
use core::mem;
|
2014-06-18 01:25:51 -05:00
|
|
|
use core::option::Option;
|
2014-12-22 07:25:58 -06:00
|
|
|
use core::ptr::Unique;
|
2014-05-13 18:10:05 -05:00
|
|
|
use core::raw::TraitObject;
|
2014-11-28 10:57:41 -06:00
|
|
|
use core::result::Result;
|
|
|
|
use core::result::Result::{Ok, Err};
|
2014-12-19 16:44:21 -06:00
|
|
|
use core::ops::{Deref, DerefMut};
|
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
|
2015-01-07 20:53:58 -06:00
|
|
|
/// #![feature(box_syntax)]
|
2014-08-04 05:48:39 -05:00
|
|
|
/// use std::boxed::HEAP;
|
2014-06-18 03:04:35 -05:00
|
|
|
///
|
2015-01-07 20:53:58 -06:00
|
|
|
/// fn main() {
|
2014-08-04 05:48:39 -05:00
|
|
|
/// # struct Bar;
|
|
|
|
/// # impl Bar { fn new(_a: int) { } }
|
2015-01-07 20:53:58 -06:00
|
|
|
/// let foo = box(HEAP) Bar::new(2);
|
|
|
|
/// let foo = box Bar::new(2);
|
|
|
|
/// }
|
2014-08-04 05:48:39 -05:00
|
|
|
/// ```
|
2014-07-10 16:19:17 -05:00
|
|
|
#[lang = "exchange_heap"]
|
2015-01-21 18:15:40 -06:00
|
|
|
#[unstable(feature = "unnamed_feature",
|
2015-01-12 20:40:19 -06:00
|
|
|
reason = "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"]
|
2015-01-12 20:40:19 -06:00
|
|
|
#[stable(feature = "grandfathered", since = "1.0.0")]
|
2014-12-22 07:25:58 -06:00
|
|
|
pub struct Box<T>(Unique<T>);
|
2014-05-13 16:58:29 -05:00
|
|
|
|
2015-01-06 18:10:50 -06:00
|
|
|
impl<T> Box<T> {
|
|
|
|
/// Moves `x` into a freshly allocated box on the global exchange heap.
|
2015-01-12 20:40:19 -06:00
|
|
|
#[stable(feature = "grandfathered", since = "1.0.0")]
|
2015-01-06 18:10:50 -06:00
|
|
|
pub fn new(x: T) -> Box<T> {
|
|
|
|
box x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-12 20:40:19 -06:00
|
|
|
#[stable(feature = "grandfathered", since = "1.0.0")]
|
2014-05-13 16:58:29 -05:00
|
|
|
impl<T: Default> Default for Box<T> {
|
2015-01-12 20:40:19 -06:00
|
|
|
#[stable(feature = "grandfathered", since = "1.0.0")]
|
2014-05-13 16:58:29 -05:00
|
|
|
fn default() -> Box<T> { box Default::default() }
|
|
|
|
}
|
|
|
|
|
2015-01-12 20:40:19 -06:00
|
|
|
#[stable(feature = "grandfathered", since = "1.0.0")]
|
2014-11-06 11:52:45 -06:00
|
|
|
impl<T> Default for Box<[T]> {
|
2015-01-12 20:40:19 -06:00
|
|
|
#[stable(feature = "grandfathered", since = "1.0.0")]
|
2014-11-06 11:52:45 -06:00
|
|
|
fn default() -> Box<[T]> { box [] }
|
|
|
|
}
|
|
|
|
|
2015-01-12 20:40:19 -06:00
|
|
|
#[stable(feature = "grandfathered", since = "1.0.0")]
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-12 20:40:19 -06:00
|
|
|
#[stable(feature = "grandfathered", since = "1.0.0")]
|
2015-01-05 15:16:49 -06:00
|
|
|
impl<T: ?Sized + PartialEq> PartialEq for Box<T> {
|
2014-10-30 15:33:47 -05:00
|
|
|
#[inline]
|
|
|
|
fn eq(&self, other: &Box<T>) -> bool { PartialEq::eq(&**self, &**other) }
|
|
|
|
#[inline]
|
|
|
|
fn ne(&self, other: &Box<T>) -> bool { PartialEq::ne(&**self, &**other) }
|
|
|
|
}
|
2015-01-12 20:40:19 -06:00
|
|
|
#[stable(feature = "grandfathered", since = "1.0.0")]
|
2015-01-05 15:16:49 -06:00
|
|
|
impl<T: ?Sized + PartialOrd> PartialOrd for Box<T> {
|
2014-10-30 15:33:47 -05:00
|
|
|
#[inline]
|
|
|
|
fn partial_cmp(&self, other: &Box<T>) -> Option<Ordering> {
|
|
|
|
PartialOrd::partial_cmp(&**self, &**other)
|
|
|
|
}
|
|
|
|
#[inline]
|
|
|
|
fn lt(&self, other: &Box<T>) -> bool { PartialOrd::lt(&**self, &**other) }
|
|
|
|
#[inline]
|
|
|
|
fn le(&self, other: &Box<T>) -> bool { PartialOrd::le(&**self, &**other) }
|
|
|
|
#[inline]
|
|
|
|
fn ge(&self, other: &Box<T>) -> bool { PartialOrd::ge(&**self, &**other) }
|
|
|
|
#[inline]
|
|
|
|
fn gt(&self, other: &Box<T>) -> bool { PartialOrd::gt(&**self, &**other) }
|
|
|
|
}
|
2015-01-12 20:40:19 -06:00
|
|
|
#[stable(feature = "grandfathered", since = "1.0.0")]
|
2015-01-05 15:16:49 -06:00
|
|
|
impl<T: ?Sized + Ord> Ord for Box<T> {
|
2014-10-30 15:33:47 -05:00
|
|
|
#[inline]
|
|
|
|
fn cmp(&self, other: &Box<T>) -> Ordering {
|
|
|
|
Ord::cmp(&**self, &**other)
|
|
|
|
}
|
2015-01-07 14:37:07 -06:00
|
|
|
}
|
2015-01-12 20:40:19 -06:00
|
|
|
#[stable(feature = "grandfathered", since = "1.0.0")]
|
2015-01-05 15:16:49 -06:00
|
|
|
impl<T: ?Sized + Eq> Eq for Box<T> {}
|
2014-10-30 15:33:47 -05:00
|
|
|
|
std: Stabilize the std::hash module
This commit aims to prepare the `std::hash` module for alpha by formalizing its
current interface whileholding off on adding `#[stable]` to the new APIs. The
current usage with the `HashMap` and `HashSet` types is also reconciled by
separating out composable parts of the design. The primary goal of this slight
redesign is to separate the concepts of a hasher's state from a hashing
algorithm itself.
The primary change of this commit is to separate the `Hasher` trait into a
`Hasher` and a `HashState` trait. Conceptually the old `Hasher` trait was
actually just a factory for various states, but hashing had very little control
over how these states were used. Additionally the old `Hasher` trait was
actually fairly unrelated to hashing.
This commit redesigns the existing `Hasher` trait to match what the notion of a
`Hasher` normally implies with the following definition:
trait Hasher {
type Output;
fn reset(&mut self);
fn finish(&self) -> Output;
}
This `Hasher` trait emphasizes that hashing algorithms may produce outputs other
than a `u64`, so the output type is made generic. Other than that, however, very
little is assumed about a particular hasher. It is left up to implementors to
provide specific methods or trait implementations to feed data into a hasher.
The corresponding `Hash` trait becomes:
trait Hash<H: Hasher> {
fn hash(&self, &mut H);
}
The old default of `SipState` was removed from this trait as it's not something
that we're willing to stabilize until the end of time, but the type parameter is
always required to implement `Hasher`. Note that the type parameter `H` remains
on the trait to enable multidispatch for specialization of hashing for
particular hashers.
Note that `Writer` is not mentioned in either of `Hash` or `Hasher`, it is
simply used as part `derive` and the implementations for all primitive types.
With these definitions, the old `Hasher` trait is realized as a new `HashState`
trait in the `collections::hash_state` module as an unstable addition for
now. The current definition looks like:
trait HashState {
type Hasher: Hasher;
fn hasher(&self) -> Hasher;
}
The purpose of this trait is to emphasize that the one piece of functionality
for implementors is that new instances of `Hasher` can be created. This
conceptually represents the two keys from which more instances of a
`SipHasher` can be created, and a `HashState` is what's stored in a
`HashMap`, not a `Hasher`.
Implementors of custom hash algorithms should implement the `Hasher` trait, and
only hash algorithms intended for use in hash maps need to implement or worry
about the `HashState` trait.
The entire module and `HashState` infrastructure remains `#[unstable]` due to it
being recently redesigned, but some other stability decision made for the
`std::hash` module are:
* The `Writer` trait remains `#[experimental]` as it's intended to be replaced
with an `io::Writer` (more details soon).
* The top-level `hash` function is `#[unstable]` as it is intended to be generic
over the hashing algorithm instead of hardwired to `SipHasher`
* The inner `sip` module is now private as its one export, `SipHasher` is
reexported in the `hash` module.
And finally, a few changes were made to the default parameters on `HashMap`.
* The `RandomSipHasher` default type parameter was renamed to `RandomState`.
This renaming emphasizes that it is not a hasher, but rather just state to
generate hashers. It also moves away from the name "sip" as it may not always
be implemented as `SipHasher`. This type lives in the
`std::collections::hash_map` module as `#[unstable]`
* The associated `Hasher` type of `RandomState` is creatively called...
`Hasher`! This concrete structure lives next to `RandomState` as an
implemenation of the "default hashing algorithm" used for a `HashMap`. Under
the hood this is currently implemented as `SipHasher`, but it draws an
explicit interface for now and allows us to modify the implementation over
time if necessary.
There are many breaking changes outlined above, and as a result this commit is
a:
[breaking-change]
2014-12-09 14:37:23 -06:00
|
|
|
impl<S: hash::Hasher, T: ?Sized + Hash<S>> Hash<S> for Box<T> {
|
|
|
|
#[inline]
|
|
|
|
fn hash(&self, state: &mut S) {
|
|
|
|
(**self).hash(state);
|
|
|
|
}
|
|
|
|
}
|
2014-12-12 20:43:07 -06:00
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Extension methods for an owning `Any` trait object.
|
2015-01-21 18:15:40 -06:00
|
|
|
#[unstable(feature = "unnamed_feature",
|
2015-01-12 20:40:19 -06:00
|
|
|
reason = "this trait will likely disappear once compiler bugs blocking \
|
|
|
|
a direct impl on `Box<Any>` have been fixed ")]
|
2015-01-14 18:08:07 -06:00
|
|
|
// 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>`
|
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.
|
2015-01-12 20:40:19 -06:00
|
|
|
#[stable(feature = "grandfathered", since = "1.0.0")]
|
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
|
|
|
|
2015-01-12 20:40:19 -06:00
|
|
|
#[stable(feature = "grandfathered", since = "1.0.0")]
|
2014-11-22 18:06:21 -06:00
|
|
|
impl BoxAny for Box<Any> {
|
2014-06-25 20:18:13 -05:00
|
|
|
#[inline]
|
2014-11-22 18:06:21 -06:00
|
|
|
fn downcast<T: 'static>(self) -> Result<Box<T>, Box<Any>> {
|
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-11-22 18:06:21 -06:00
|
|
|
mem::transmute::<Box<Any>, TraitObject>(self);
|
2014-06-25 20:18:13 -05:00
|
|
|
|
|
|
|
// Extract the data pointer
|
|
|
|
Ok(mem::transmute(to.data))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-05 15:16:49 -06:00
|
|
|
impl<T: ?Sized + fmt::Show> fmt::Show for Box<T> {
|
2014-05-11 13:14:14 -05:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2014-12-20 02:09:35 -06:00
|
|
|
write!(f, "Box({:?})", &**self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-12 20:40:19 -06:00
|
|
|
#[stable(feature = "grandfathered", since = "1.0.0")]
|
2015-01-06 17:22:24 -06:00
|
|
|
impl<T: ?Sized + fmt::String> fmt::String for Box<T> {
|
2014-12-20 02:09:35 -06:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
fmt::String::fmt(&**self, f)
|
2014-05-11 13:14:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-02 00:24:06 -06:00
|
|
|
impl fmt::Show for Box<Any> {
|
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
|
|
|
|
2015-01-12 20:40:19 -06:00
|
|
|
#[stable(feature = "grandfathered", since = "1.0.0")]
|
2015-01-05 15:16:49 -06:00
|
|
|
impl<T: ?Sized> Deref for Box<T> {
|
2015-01-01 13:53:20 -06:00
|
|
|
type Target = T;
|
|
|
|
|
2014-12-19 16:44:21 -06:00
|
|
|
fn deref(&self) -> &T { &**self }
|
|
|
|
}
|
|
|
|
|
2015-01-12 20:40:19 -06:00
|
|
|
#[stable(feature = "grandfathered", since = "1.0.0")]
|
2015-01-05 15:16:49 -06:00
|
|
|
impl<T: ?Sized> DerefMut for Box<T> {
|
2014-12-19 16:44:21 -06:00
|
|
|
fn deref_mut(&mut self) -> &mut T { &mut **self }
|
|
|
|
}
|
|
|
|
|
2014-06-28 15:57:36 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
#[test]
|
|
|
|
fn test_owned_clone() {
|
2015-01-07 16:04:12 -06:00
|
|
|
let a = Box::new(5i);
|
2014-06-28 15:57:36 -05:00
|
|
|
let b: Box<int> = a.clone();
|
|
|
|
assert!(a == b);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn any_move() {
|
2015-01-07 16:04:12 -06:00
|
|
|
let a = Box::new(8u) as Box<Any>;
|
|
|
|
let b = Box::new(Test) as Box<Any>;
|
2014-06-28 15:57:36 -05:00
|
|
|
|
2014-07-10 16:19:17 -05:00
|
|
|
match a.downcast::<uint>() {
|
2015-01-07 16:04:12 -06:00
|
|
|
Ok(a) => { assert!(a == Box::new(8u)); }
|
2014-10-09 14:17:22 -05:00
|
|
|
Err(..) => panic!()
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
2014-07-10 16:19:17 -05:00
|
|
|
match b.downcast::<Test>() {
|
2015-01-07 16:04:12 -06:00
|
|
|
Ok(a) => { assert!(a == Box::new(Test)); }
|
2014-10-09 14:17:22 -05:00
|
|
|
Err(..) => panic!()
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
2015-01-07 16:04:12 -06:00
|
|
|
let a = Box::new(8u) as Box<Any>;
|
|
|
|
let b = Box::new(Test) as Box<Any>;
|
2014-06-28 15:57:36 -05:00
|
|
|
|
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() {
|
2015-01-07 16:04:12 -06:00
|
|
|
let a = Box::new(8u) as Box<Any>;
|
|
|
|
let b = Box::new(Test) as Box<Any>;
|
2014-06-28 15:57:36 -05:00
|
|
|
let a_str = a.to_str();
|
|
|
|
let b_str = b.to_str();
|
2014-11-27 12:03:07 -06:00
|
|
|
assert_eq!(a_str, "Box<Any>");
|
|
|
|
assert_eq!(b_str, "Box<Any>");
|
2014-06-28 15:57:36 -05:00
|
|
|
|
|
|
|
let a = &8u as &Any;
|
|
|
|
let b = &Test as &Any;
|
|
|
|
let s = format!("{}", a);
|
2014-11-27 12:03:07 -06:00
|
|
|
assert_eq!(s, "&Any");
|
2014-06-28 15:57:36 -05:00
|
|
|
let s = format!("{}", b);
|
2014-11-27 12:03:07 -06:00
|
|
|
assert_eq!(s, "&Any");
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
2014-12-19 16:44:21 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn deref() {
|
2015-01-01 13:53:20 -06:00
|
|
|
fn homura<T: Deref<Target=i32>>(_: T) { }
|
2015-01-07 16:04:12 -06:00
|
|
|
homura(Box::new(765i32));
|
2014-12-19 16:44:21 -06:00
|
|
|
}
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|