From fcc79f2d60d55114ae890e1112393ef0e2e8d93f Mon Sep 17 00:00:00 2001 From: Alexander Bulaev Date: Wed, 4 Nov 2015 15:03:33 +0300 Subject: [PATCH 1/3] liballoc: implement From for Box, Rc, Arc Sometimes when writing generic code you want to abstract over owning/pointer type so that calling code isn't restricted by one concrete owning/pointer type. This commit makes possible such code: ``` fn i_will_work_with_arc>>(t: T) { let the_arc = t.into(); // Do something } i_will_work_with_arc(MyTy::new()); i_will_work_with_arc(Box::new(MyTy::new())); let arc_that_i_already_have = Arc::new(MyTy::new()); i_will_work_with_arc(arc_that_i_already_have); ``` Please note that this patch doesn't work with DSTs. --- src/liballoc/arc.rs | 31 +++++++++++++++++++++++++++++++ src/liballoc/boxed.rs | 8 ++++++++ src/liballoc/rc.rs | 30 ++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+) diff --git a/src/liballoc/arc.rs b/src/liballoc/arc.rs index 95dcdd22fd084..36f915d19e9ca 100644 --- a/src/liballoc/arc.rs +++ b/src/liballoc/arc.rs @@ -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; @@ -894,8 +895,23 @@ impl Hash for Arc { } } +#[stable(feature = "rust1", since = "1.6.0")] +impl From for Arc { + fn from(t: T) -> Self { + Arc::new(t) + } +} + +#[stable(feature = "rust1", since = "1.6.0")] +impl From> for Arc { + fn from(t: Box) -> Self { + Arc::new(*t) + } +} + #[cfg(test)] mod tests { + use std::boxed::Box; use std::clone::Clone; use std::sync::mpsc::channel; use std::mem::drop; @@ -908,6 +924,7 @@ mod tests { use std::vec::Vec; use super::{Arc, Weak}; use std::sync::Mutex; + use std::convert::From; struct Canary(*mut atomic::AtomicUsize); @@ -1137,6 +1154,20 @@ 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); + } + + #[test] + fn test_from_box() { + let foo_box = Box::new(123); + let foo_arc = Arc::from(foo_box); + assert!(123 == *foo_arc); + } } impl borrow::Borrow for Arc { diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 4783b4339dac3..3899c9be46d61 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -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. @@ -375,6 +376,13 @@ impl Hash for Box { } } +#[stable(feature = "rust1", since = "1.6.0")] +impl From for Box { + fn from(t: T) -> Self { + Box::new(t) + } +} + impl Box { #[inline] #[stable(feature = "rust1", since = "1.0.0")] diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index d229d1c815c2d..e7deceb94c370 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -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; @@ -698,6 +699,20 @@ impl fmt::Pointer for Rc { } } +#[stable(feature = "rust1", since = "1.6.0")] +impl From for Rc { + fn from(t: T) -> Self { + Rc::new(t) + } +} + +#[stable(feature = "rust1", since = "1.6.0")] +impl From> for Rc { + fn from(t: Box) -> Self { + Rc::new(*t) + } +} + /// A weak version of `Rc`. /// /// Weak references do not count when determining if the inner value should be @@ -903,6 +918,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() { @@ -1105,6 +1121,20 @@ 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); + } + + #[test] + fn test_from_box() { + let foo_box = Box::new(123); + let foo_rc = Rc::from(foo_box); + assert!(123 == *foo_rc); + } } impl borrow::Borrow for Rc { From 8ce9c19b5bf6259eeaa4482a824893cdf3dfe0e3 Mon Sep 17 00:00:00 2001 From: Alexander Bulaev Date: Thu, 12 Nov 2015 10:40:02 +0300 Subject: [PATCH 2/3] Remove impls for cases considered `niche` --- src/liballoc/arc.rs | 15 --------------- src/liballoc/rc.rs | 14 -------------- 2 files changed, 29 deletions(-) diff --git a/src/liballoc/arc.rs b/src/liballoc/arc.rs index 36f915d19e9ca..a2ccac3ba7eee 100644 --- a/src/liballoc/arc.rs +++ b/src/liballoc/arc.rs @@ -902,16 +902,8 @@ impl From for Arc { } } -#[stable(feature = "rust1", since = "1.6.0")] -impl From> for Arc { - fn from(t: Box) -> Self { - Arc::new(*t) - } -} - #[cfg(test)] mod tests { - use std::boxed::Box; use std::clone::Clone; use std::sync::mpsc::channel; use std::mem::drop; @@ -1161,13 +1153,6 @@ mod tests { let foo_arc = Arc::from(foo); assert!(123 == *foo_arc); } - - #[test] - fn test_from_box() { - let foo_box = Box::new(123); - let foo_arc = Arc::from(foo_box); - assert!(123 == *foo_arc); - } } impl borrow::Borrow for Arc { diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index e7deceb94c370..626abb0b43a0c 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -706,13 +706,6 @@ impl From for Rc { } } -#[stable(feature = "rust1", since = "1.6.0")] -impl From> for Rc { - fn from(t: Box) -> Self { - Rc::new(*t) - } -} - /// A weak version of `Rc`. /// /// Weak references do not count when determining if the inner value should be @@ -1128,13 +1121,6 @@ mod tests { let foo_rc = Rc::from(foo); assert!(123 == *foo_rc); } - - #[test] - fn test_from_box() { - let foo_box = Box::new(123); - let foo_rc = Rc::from(foo_box); - assert!(123 == *foo_rc); - } } impl borrow::Borrow for Rc { From 67c07d445036e8db7782f3c661a8eb4a70fbe417 Mon Sep 17 00:00:00 2001 From: Alexander Bulaev Date: Mon, 16 Nov 2015 11:04:17 +0300 Subject: [PATCH 3/3] Fix feature name --- src/liballoc/arc.rs | 2 +- src/liballoc/boxed.rs | 2 +- src/liballoc/rc.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/liballoc/arc.rs b/src/liballoc/arc.rs index a2ccac3ba7eee..1456baffdc9bc 100644 --- a/src/liballoc/arc.rs +++ b/src/liballoc/arc.rs @@ -895,7 +895,7 @@ impl Hash for Arc { } } -#[stable(feature = "rust1", since = "1.6.0")] +#[stable(feature = "from_for_ptrs", since = "1.6.0")] impl From for Arc { fn from(t: T) -> Self { Arc::new(t) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 3899c9be46d61..a40b9eb2b557c 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -376,7 +376,7 @@ impl Hash for Box { } } -#[stable(feature = "rust1", since = "1.6.0")] +#[stable(feature = "from_for_ptrs", since = "1.6.0")] impl From for Box { fn from(t: T) -> Self { Box::new(t) diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index 626abb0b43a0c..a835cdf129a6f 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -699,7 +699,7 @@ impl fmt::Pointer for Rc { } } -#[stable(feature = "rust1", since = "1.6.0")] +#[stable(feature = "from_for_ptrs", since = "1.6.0")] impl From for Rc { fn from(t: T) -> Self { Rc::new(t)