about summary refs log tree commit diff
path: root/src/liballoc
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2015-11-16 19:06:52 +0000
committerbors <bors@rust-lang.org>2015-11-16 19:06:52 +0000
commit99093b79f112c5decd555103c890c6af15ed0c68 (patch)
treee45f6f59025ce39c6192da138fe9a40c62f342f9 /src/liballoc
parent3042fedb4f50640e2c9a02297c5ce9a1f3ffaa5a (diff)
parent67c07d445036e8db7782f3c661a8eb4a70fbe417 (diff)
downloadrust-99093b79f112c5decd555103c890c6af15ed0c68.tar.gz
rust-99093b79f112c5decd555103c890c6af15ed0c68.zip
Auto merge of #29580 - alexbool:smart-pointer-conversion, r=alexcrichton
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:
```rust
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.
Also to mention, I made those impls stable, and I don't know whether they should be actually stable from the beginning. Please tell me if this should be feature-gated.
Diffstat (limited to 'src/liballoc')
-rw-r--r--src/liballoc/arc.rs16
-rw-r--r--src/liballoc/boxed.rs8
-rw-r--r--src/liballoc/rc.rs16
3 files changed, 40 insertions, 0 deletions
diff --git a/src/liballoc/arc.rs b/src/liballoc/arc.rs
index 34c8b5d4139..8205e13205f 100644
--- a/src/liballoc/arc.rs
+++ b/src/liballoc/arc.rs
@@ -87,6 +87,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;
@@ -896,6 +897,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;
@@ -910,6 +918,7 @@ mod tests {
     use std::vec::Vec;
     use super::{Arc, Weak};
     use std::sync::Mutex;
+    use std::convert::From;
 
     struct Canary(*mut atomic::AtomicUsize);
 
@@ -1139,6 +1148,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> {
diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs
index 2e4ac13b34d..b5c6cdff119 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.
@@ -373,6 +374,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")]
diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs
index b94e74ada9c..88db3cfe4b6 100644
--- a/src/liballoc/rc.rs
+++ b/src/liballoc/rc.rs
@@ -169,6 +169,7 @@ use core::ops::Deref;
 #[cfg(not(stage0))]
 use core::ops::CoerceUnsized;
 use core::ptr::{self, Shared};
+use core::convert::From;
 
 use heap::deallocate;
 
@@ -701,6 +702,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
@@ -906,6 +914,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() {
@@ -1108,6 +1117,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> {