1
Fork 0

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.
This commit is contained in:
Alexander Bulaev 2015-11-04 15:03:33 +03:00
commit fcc79f2d60
3 changed files with 69 additions and 0 deletions

View file

@ -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> {