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.
This commit is contained in:
commit
99093b79f1
3 changed files with 40 additions and 0 deletions
|
@ -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> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue