1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use core::mem::MaybeUninit;
use core::pin::Pin;
use alloc::boxed::Box;
use alloc::rc::Rc;
use alloc::sync::Arc;
use crate::move_ref::DerefMove;
use crate::move_ref::MoveRef;
use crate::new::Emplace;
use crate::new::TryNew;
unsafe impl<T> DerefMove for Box<T> {
type Uninit = Box<MaybeUninit<T>>;
#[inline]
fn deinit(self) -> Self::Uninit {
unsafe { Box::from_raw(Box::into_raw(self).cast::<MaybeUninit<T>>()) }
}
#[inline]
unsafe fn deref_move(this: &mut Self::Uninit) -> MoveRef<Self::Target> {
MoveRef::new_unchecked(&mut *(&mut **this as *mut MaybeUninit<T> as *mut T))
}
}
impl<T> Emplace<T> for Box<T> {
fn try_emplace<N: TryNew<Output = T>>(n: N) -> Result<Pin<Self>, N::Error> {
let mut uninit = Box::new(MaybeUninit::<T>::uninit());
unsafe {
let pinned = Pin::new_unchecked(&mut *uninit);
n.try_new(pinned)?;
Ok(Pin::new_unchecked(Box::from_raw(
Box::into_raw(uninit).cast::<T>(),
)))
}
}
}
impl<T> Emplace<T> for Rc<T> {
fn try_emplace<N: TryNew<Output = T>>(n: N) -> Result<Pin<Self>, N::Error> {
let uninit = Rc::new(MaybeUninit::<T>::uninit());
unsafe {
let pinned = Pin::new_unchecked(&mut *(Rc::as_ptr(&uninit) as *mut _));
n.try_new(pinned)?;
Ok(Pin::new_unchecked(Rc::from_raw(
Rc::into_raw(uninit).cast::<T>(),
)))
}
}
}
impl<T> Emplace<T> for Arc<T> {
fn try_emplace<N: TryNew<Output = T>>(n: N) -> Result<Pin<Self>, N::Error> {
let uninit = Arc::new(MaybeUninit::<T>::uninit());
unsafe {
let pinned = Pin::new_unchecked(&mut *(Arc::as_ptr(&uninit) as *mut _));
n.try_new(pinned)?;
Ok(Pin::new_unchecked(Arc::from_raw(
Arc::into_raw(uninit).cast::<T>(),
)))
}
}
}