about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorAlexander Bulaev <aleks.bulaev@gmail.com>2015-11-04 15:03:33 +0300
committerAlexander Bulaev <aleks.bulaev@gmail.com>2015-11-04 15:03:33 +0300
commitfcc79f2d60d55114ae890e1112393ef0e2e8d93f (patch)
tree06adda2f3d316e558bcbf5418711977445a9ba98 /src
parenta216e847272ddbd3033037b606eaf2d801c250b9 (diff)
downloadrust-fcc79f2d60d55114ae890e1112393ef0e2e8d93f.tar.gz
rust-fcc79f2d60d55114ae890e1112393ef0e2e8d93f.zip
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: Into<Arc<MyTy>>>(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.
Diffstat (limited to 'src')
-rw-r--r--src/liballoc/arc.rs31
-rw-r--r--src/liballoc/boxed.rs8
-rw-r--r--src/liballoc/rc.rs30
3 files changed, 69 insertions, 0 deletions
diff --git a/src/liballoc/arc.rs b/src/liballoc/arc.rs
index 95dcdd22fd0..36f915d19e9 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<T: ?Sized + Hash> Hash for Arc<T> {
     }
 }
 
+#[stable(feature = "rust1", since = "1.6.0")]
+impl<T> From<T> for Arc<T> {
+    fn from(t: T) -> Self {
+        Arc::new(t)
+    }
+}
+
+#[stable(feature = "rust1", since = "1.6.0")]
+impl<T> From<Box<T>> for Arc<T> {
+    fn from(t: Box<T>) -> 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<T: ?Sized> borrow::Borrow<T> for Arc<T> {
diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs
index 4783b4339da..3899c9be46d 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<T: ?Sized + Hash> Hash for Box<T> {
     }
 }
 
+#[stable(feature = "rust1", 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")]
diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs
index d229d1c815c..e7deceb94c3 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<T> fmt::Pointer for Rc<T> {
     }
 }
 
+#[stable(feature = "rust1", since = "1.6.0")]
+impl<T> From<T> for Rc<T> {
+    fn from(t: T) -> Self {
+        Rc::new(t)
+    }
+}
+
+#[stable(feature = "rust1", since = "1.6.0")]
+impl<T> From<Box<T>> for Rc<T> {
+    fn from(t: Box<T>) -> Self {
+        Rc::new(*t)
+    }
+}
+
 /// A weak version of `Rc<T>`.
 ///
 /// 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<T: ?Sized> borrow::Borrow<T> for Rc<T> {