From cbc31df4fc084b47a5c6456df2efb6e28b82a7da Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Tue, 13 May 2014 14:58:29 -0700 Subject: std: Move the owned module from core to std The compiler was updated to recognize that implementations for ty_uniq(..) are allowed if the Box lang item is located in the current crate. This enforces the idea that libcore cannot allocated, and moves all related trait implementations from libcore to libstd. This is a breaking change in that the AnyOwnExt trait has moved from the any module to the owned module. Any previous users of std::any::AnyOwnExt should now use std::owned::AnyOwnExt instead. This was done because the trait is intended for Box traits and only Box traits. [breaking-change] --- src/libstd/lib.rs | 8 +++- src/libstd/owned.rs | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/libstd/rt/task.rs | 3 +- src/libstd/task.rs | 5 ++- 4 files changed, 111 insertions(+), 6 deletions(-) create mode 100644 src/libstd/owned.rs (limited to 'src/libstd') diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index 34ed7933c39..a37f9a516fd 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -133,14 +133,16 @@ extern crate core; #[cfg(test)] pub use ops = realstd::ops; #[cfg(test)] pub use cmp = realstd::cmp; #[cfg(test)] pub use ty = realstd::ty; -#[cfg(test)] pub use owned = realstd::owned; +#[cfg(not(stage0), test)] pub use owned = realstd::owned; #[cfg(not(test))] pub use cmp = core::cmp; #[cfg(not(test))] pub use kinds = core::kinds; #[cfg(not(test))] pub use ops = core::ops; -#[cfg(not(test))] pub use owned = core::owned; #[cfg(not(test))] pub use ty = core::ty; +#[cfg(stage0, test)] pub use owned = realstd::owned; +#[cfg(stage0, not(test))] pub use owned = core::owned; + pub use core::any; pub use core::bool; pub use core::cell; @@ -207,6 +209,8 @@ pub mod ascii; pub mod rc; pub mod gc; +#[cfg(not(stage0), not(test))] +pub mod owned; /* Common traits */ diff --git a/src/libstd/owned.rs b/src/libstd/owned.rs new file mode 100644 index 00000000000..3af12c5154c --- /dev/null +++ b/src/libstd/owned.rs @@ -0,0 +1,101 @@ +// Copyright 2012 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +//! Operations on unique pointer types + +use any::{Any, AnyRefExt}; +use clone::Clone; +use cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering}; +use default::Default; +use intrinsics; +use mem; +use raw::TraitObject; +use result::{Ok, Err, 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: +/// +/// let foo = box(HEAP) Bar::new(...); +/// let foo = box Bar::new(...); +#[lang="exchange_heap"] +pub static HEAP: () = (); + +/// A type that represents a uniquely-owned value. +#[lang="owned_box"] +pub struct Box(*T); + +impl Default for Box { + fn default() -> Box { box Default::default() } +} + +impl Clone for Box { + /// Return a copy of the owned box. + #[inline] + fn clone(&self) -> Box { box {(**self).clone()} } + + /// Perform copy-assignment from `source` by reusing the existing allocation. + #[inline] + fn clone_from(&mut self, source: &Box) { + (**self).clone_from(&(**source)); + } +} + +// box pointers +impl Eq for Box { + #[inline] + fn eq(&self, other: &Box) -> bool { *(*self) == *(*other) } + #[inline] + fn ne(&self, other: &Box) -> bool { *(*self) != *(*other) } +} +impl Ord for Box { + #[inline] + fn lt(&self, other: &Box) -> bool { *(*self) < *(*other) } + #[inline] + fn le(&self, other: &Box) -> bool { *(*self) <= *(*other) } + #[inline] + fn ge(&self, other: &Box) -> bool { *(*self) >= *(*other) } + #[inline] + fn gt(&self, other: &Box) -> bool { *(*self) > *(*other) } +} +impl TotalOrd for Box { + #[inline] + fn cmp(&self, other: &Box) -> Ordering { (**self).cmp(*other) } +} +impl TotalEq for Box {} + +/// Extension methods for an owning `Any` trait object +pub trait AnyOwnExt { + /// Returns the boxed value if it is of type `T`, or + /// `Err(Self)` if it isn't. + fn move(self) -> Result, Self>; +} + +impl AnyOwnExt for Box { + #[inline] + fn move(self) -> Result, Box> { + if self.is::() { + unsafe { + // Get the raw representation of the trait object + let to: TraitObject = + *mem::transmute::<&Box, &TraitObject>(&self); + + // Prevent destructor on self being run + intrinsics::forget(self); + + // Extract the data pointer + Ok(mem::transmute(to.data)) + } + } else { + Err(self) + } + } +} diff --git a/src/libstd/rt/task.rs b/src/libstd/rt/task.rs index cd0445056b2..31a20145306 100644 --- a/src/libstd/rt/task.rs +++ b/src/libstd/rt/task.rs @@ -13,7 +13,6 @@ //! local storage, and logging. Even a 'freestanding' Rust would likely want //! to implement this. -use any::AnyOwnExt; use cleanup; use clone::Clone; use comm::Sender; @@ -24,7 +23,7 @@ use local_data; use mem; use ops::Drop; use option::{Option, Some, None}; -use owned::Box; +use owned::{AnyOwnExt, Box}; use prelude::drop; use result::{Result, Ok, Err}; use rt::Runtime; diff --git a/src/libstd/task.rs b/src/libstd/task.rs index 2f7b31ae31d..7fb61c29112 100644 --- a/src/libstd/task.rs +++ b/src/libstd/task.rs @@ -47,10 +47,11 @@ use rt::local::Local; use rt::task::Task; use str::{Str, SendStr, IntoMaybeOwned}; -#[cfg(test)] use any::{AnyOwnExt, AnyRefExt}; +#[cfg(test)] use any::AnyRefExt; +#[cfg(test)] use owned::AnyOwnExt; +#[cfg(test)] use realstd::result::ResultUnwrap; #[cfg(test)] use result; #[cfg(test)] use str::StrAllocating; -#[cfg(test)] use realstd::result::ResultUnwrap; /// Indicates the manner in which a task exited. /// -- cgit 1.4.1-3-g733a5