| 1 | |
| 2 |
|
| 3 | #[cfg(all(feature = "alloc", not(feature = "std")))]
|
| 4 | use alloc::{boxed::Box, sync::Arc};
|
| 5 | #[cfg(feature = "alloc")]
|
| 6 | use core::alloc::AllocError;
|
| 7 | use core::{mem::MaybeUninit, pin::Pin};
|
| 8 | #[cfg(feature = "std")]
|
| 9 | use std::sync::Arc;
|
| 10 |
|
| 11 | #[cfg(not(feature = "alloc"))]
|
| 12 | type AllocError = core::convert::Infallible;
|
| 13 |
|
| 14 | use crate::{
|
| 15 | init_from_closure, pin_init_from_closure, InPlaceWrite, Init, PinInit, ZeroableOption,
|
| 16 | };
|
| 17 |
|
| 18 | pub extern crate alloc;
|
| 19 |
|
| 20 | |
| 21 | |
| 22 | unsafe impl<T> ZeroableOption for Box<T> {}
|
| 23 |
|
| 24 | |
| 25 | pub trait InPlaceInit<T>: Sized {
|
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | fn try_pin_init<E>(init: impl PinInit<T, E>) -> Result<Pin<Self>, E>
|
| 31 | where
|
| 32 | E: From<AllocError>;
|
| 33 |
|
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | #[inline]
|
| 39 | fn pin_init(init: impl PinInit<T>) -> Result<Pin<Self>, AllocError> {
|
| 40 | |
| 41 | let init = unsafe {
|
| 42 | pin_init_from_closure(|slot| match init.__init(slot) {
|
| 43 | Ok(()) => Ok(()),
|
| 44 | Err(i) => match i {},
|
| 45 | })
|
| 46 | };
|
| 47 | Self::try_pin_init(init)
|
| 48 | }
|
| 49 |
|
| 50 | |
| 51 | fn try_init<E>(init: impl Init<T, E>) -> Result<Self, E>
|
| 52 | where
|
| 53 | E: From<AllocError>;
|
| 54 |
|
| 55 | |
| 56 | #[inline]
|
| 57 | fn init(init: impl Init<T>) -> Result<Self, AllocError> {
|
| 58 | |
| 59 | let init = unsafe {
|
| 60 | init_from_closure(|slot| match init.__init(slot) {
|
| 61 | Ok(()) => Ok(()),
|
| 62 | Err(i) => match i {},
|
| 63 | })
|
| 64 | };
|
| 65 | Self::try_init(init)
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | #[cfg(feature = "alloc")]
|
| 70 | macro_rules! try_new_uninit {
|
| 71 | ($type:ident) => {
|
| 72 | $type::try_new_uninit()?
|
| 73 | };
|
| 74 | }
|
| 75 | #[cfg(all(feature = "std", not(feature = "alloc")))]
|
| 76 | macro_rules! try_new_uninit {
|
| 77 | ($type:ident) => {
|
| 78 | $type::new_uninit()
|
| 79 | };
|
| 80 | }
|
| 81 |
|
| 82 | impl<T> InPlaceInit<T> for Box<T> {
|
| 83 | #[inline]
|
| 84 | fn try_pin_init<E>(init: impl PinInit<T, E>) -> Result<Pin<Self>, E>
|
| 85 | where
|
| 86 | E: From<AllocError>,
|
| 87 | {
|
| 88 | try_new_uninit!(Box).write_pin_init(init)
|
| 89 | }
|
| 90 |
|
| 91 | #[inline]
|
| 92 | fn try_init<E>(init: impl Init<T, E>) -> Result<Self, E>
|
| 93 | where
|
| 94 | E: From<AllocError>,
|
| 95 | {
|
| 96 | try_new_uninit!(Box).write_init(init)
|
| 97 | }
|
| 98 | }
|
| 99 |
|
| 100 | impl<T> InPlaceInit<T> for Arc<T> {
|
| 101 | #[inline]
|
| 102 | fn try_pin_init<E>(init: impl PinInit<T, E>) -> Result<Pin<Self>, E>
|
| 103 | where
|
| 104 | E: From<AllocError>,
|
| 105 | {
|
| 106 | let mut this = try_new_uninit!(Arc);
|
| 107 | let Some(slot) = Arc::get_mut(&mut this) else {
|
| 108 | |
| 109 | unsafe { core::hint::unreachable_unchecked() }
|
| 110 | };
|
| 111 | let slot = slot.as_mut_ptr();
|
| 112 | |
| 113 | |
| 114 | unsafe { init.__init(slot)? };
|
| 115 | |
| 116 | Ok(unsafe { Pin::new_unchecked(this.assume_init()) })
|
| 117 | }
|
| 118 |
|
| 119 | #[inline]
|
| 120 | fn try_init<E>(init: impl Init<T, E>) -> Result<Self, E>
|
| 121 | where
|
| 122 | E: From<AllocError>,
|
| 123 | {
|
| 124 | let mut this = try_new_uninit!(Arc);
|
| 125 | let Some(slot) = Arc::get_mut(&mut this) else {
|
| 126 | |
| 127 | unsafe { core::hint::unreachable_unchecked() }
|
| 128 | };
|
| 129 | let slot = slot.as_mut_ptr();
|
| 130 | |
| 131 | |
| 132 | unsafe { init.__init(slot)? };
|
| 133 | |
| 134 | Ok(unsafe { this.assume_init() })
|
| 135 | }
|
| 136 | }
|
| 137 |
|
| 138 | impl<T> InPlaceWrite<T> for Box<MaybeUninit<T>> {
|
| 139 | type Initialized = Box<T>;
|
| 140 |
|
| 141 | #[inline]
|
| 142 | fn write_init<E>(mut self, init: impl Init<T, E>) -> Result<Self::Initialized, E> {
|
| 143 | let slot = self.as_mut_ptr();
|
| 144 | |
| 145 | |
| 146 | unsafe { init.__init(slot)? };
|
| 147 | |
| 148 | Ok(unsafe { self.assume_init() })
|
| 149 | }
|
| 150 |
|
| 151 | #[inline]
|
| 152 | fn write_pin_init<E>(mut self, init: impl PinInit<T, E>) -> Result<Pin<Self::Initialized>, E> {
|
| 153 | let slot = self.as_mut_ptr();
|
| 154 | |
| 155 | |
| 156 | unsafe { init.__init(slot)? };
|
| 157 | |
| 158 | Ok(unsafe { self.assume_init() }.into())
|
| 159 | }
|
| 160 | }
|