-
Notifications
You must be signed in to change notification settings - Fork 13.6k
std: Move the owned module from core to std #14184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+219
−105
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <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. | ||
|
||
//! 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>(*T); | ||
|
||
impl<T: Default> Default for Box<T> { | ||
fn default() -> Box<T> { box Default::default() } | ||
} | ||
|
||
impl<T: Clone> Clone for Box<T> { | ||
/// Return a copy of the owned box. | ||
#[inline] | ||
fn clone(&self) -> Box<T> { box {(**self).clone()} } | ||
|
||
/// Perform copy-assignment from `source` by reusing the existing allocation. | ||
#[inline] | ||
fn clone_from(&mut self, source: &Box<T>) { | ||
(**self).clone_from(&(**source)); | ||
} | ||
} | ||
|
||
// box pointers | ||
impl<T:Eq> Eq for Box<T> { | ||
#[inline] | ||
fn eq(&self, other: &Box<T>) -> bool { *(*self) == *(*other) } | ||
#[inline] | ||
fn ne(&self, other: &Box<T>) -> bool { *(*self) != *(*other) } | ||
} | ||
impl<T:Ord> Ord for Box<T> { | ||
#[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) } | ||
} | ||
impl<T: TotalOrd> TotalOrd for Box<T> { | ||
#[inline] | ||
fn cmp(&self, other: &Box<T>) -> Ordering { (**self).cmp(*other) } | ||
} | ||
impl<T: TotalEq> TotalEq for Box<T> {} | ||
|
||
/// 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<T: 'static>(self) -> Result<Box<T>, Self>; | ||
} | ||
|
||
impl AnyOwnExt for Box<Any> { | ||
#[inline] | ||
fn move<T: 'static>(self) -> Result<Box<T>, Box<Any>> { | ||
if self.is::<T>() { | ||
unsafe { | ||
// Get the raw representation of the trait object | ||
let to: TraitObject = | ||
*mem::transmute::<&Box<Any>, &TraitObject>(&self); | ||
|
||
// Prevent destructor on self being run | ||
intrinsics::forget(self); | ||
|
||
// Extract the data pointer | ||
Ok(mem::transmute(to.data)) | ||
} | ||
} else { | ||
Err(self) | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this should've been deleted? (it's
libcore/owned.rs
)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hopefully soon! This is needed for bootstrapping, however (it's
cfg(stage0)
inlibcore/lib.rs
)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, whoops; I was actually assuming that there would be staging required, but then completely missed it. :/