Skip to content

liballoc: implement From for Box, Rc, Arc #29580

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

Merged
merged 3 commits into from
Nov 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/liballoc/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ use core::ptr::{self, Shared};
use core::marker::Unsize;
use core::hash::{Hash, Hasher};
use core::{usize, isize};
use core::convert::From;
use heap::deallocate;

const MAX_REFCOUNT: usize = (isize::MAX) as usize;
Expand Down Expand Up @@ -894,6 +895,13 @@ impl<T: ?Sized + Hash> Hash for Arc<T> {
}
}

#[stable(feature = "from_for_ptrs", since = "1.6.0")]
impl<T> From<T> for Arc<T> {
fn from(t: T) -> Self {
Arc::new(t)
}
}

#[cfg(test)]
mod tests {
use std::clone::Clone;
Expand All @@ -908,6 +916,7 @@ mod tests {
use std::vec::Vec;
use super::{Arc, Weak};
use std::sync::Mutex;
use std::convert::From;

struct Canary(*mut atomic::AtomicUsize);

Expand Down Expand Up @@ -1137,6 +1146,13 @@ mod tests {
drop(x);
assert!(y.upgrade().is_none());
}

#[test]
fn test_from_owned() {
let foo = 123;
let foo_arc = Arc::from(foo);
assert!(123 == *foo_arc);
}
}

impl<T: ?Sized> borrow::Borrow<T> for Arc<T> {
Expand Down
8 changes: 8 additions & 0 deletions src/liballoc/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ use core::ops::{CoerceUnsized, Deref, DerefMut};
use core::ops::{Placer, Boxed, Place, InPlace, BoxPlace};
use core::ptr::{self, Unique};
use core::raw::TraitObject;
use core::convert::From;

/// A value that represents the heap. This is the default place that the `box`
/// keyword allocates into when no place is supplied.
Expand Down Expand Up @@ -375,6 +376,13 @@ impl<T: ?Sized + Hash> Hash for Box<T> {
}
}

#[stable(feature = "from_for_ptrs", since = "1.6.0")]
impl<T> From<T> for Box<T> {
fn from(t: T) -> Self {
Box::new(t)
}
}

impl Box<Any> {
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down
16 changes: 16 additions & 0 deletions src/liballoc/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ use core::marker::{self, Unsize};
use core::mem::{self, align_of_val, size_of_val, forget};
use core::ops::{CoerceUnsized, Deref};
use core::ptr::{self, Shared};
use core::convert::From;

use heap::deallocate;

Expand Down Expand Up @@ -698,6 +699,13 @@ impl<T> fmt::Pointer for Rc<T> {
}
}

#[stable(feature = "from_for_ptrs", since = "1.6.0")]
impl<T> From<T> for Rc<T> {
fn from(t: T) -> Self {
Rc::new(t)
}
}

/// A weak version of `Rc<T>`.
///
/// Weak references do not count when determining if the inner value should be
Expand Down Expand Up @@ -903,6 +911,7 @@ mod tests {
use std::result::Result::{Err, Ok};
use std::mem::drop;
use std::clone::Clone;
use std::convert::From;

#[test]
fn test_clone() {
Expand Down Expand Up @@ -1105,6 +1114,13 @@ mod tests {
let foo: Rc<[i32]> = Rc::new([1, 2, 3]);
assert_eq!(foo, foo.clone());
}

#[test]
fn test_from_owned() {
let foo = 123;
let foo_rc = Rc::from(foo);
assert!(123 == *foo_rc);
}
}

impl<T: ?Sized> borrow::Borrow<T> for Rc<T> {
Expand Down