1use core::convert::Infallible;
19use core::{cell::UnsafeCell, mem::MaybeUninit};
20
21pub use pinned_init::*;
23pub use pinned_init::init_zeroed as zeroed;
25
26pub fn into_init<F, T, E, I: Init<T, E>>(f: F) -> impl Init<T, E>
28where
29 F: FnOnce() -> Result<I, E>,
30{
31 unsafe { init_from_closure(move |slot| f()?.__init(slot)) }
32}
33
34pub trait IntoFallibleInit<T>: Init<T, Infallible> {
37 fn into_fallible<E>(self) -> impl Init<T, E> {
39 unsafe {
40 init_from_closure(move |slot| {
41 unwrap!(Self::__init(self, slot));
42
43 Ok(())
44 })
45 }
46 }
47}
48
49impl<T, I> IntoFallibleInit<T> for I where I: Init<T, Infallible> {}
50
51pub trait UnsafeCellInit<T> {
53 fn init<I: Init<T>>(value: I) -> impl Init<Self>;
56}
57
58impl<T> UnsafeCellInit<T> for UnsafeCell<T> {
59 fn init<I: Init<T>>(value: I) -> impl Init<Self> {
60 unsafe {
61 init_from_closure::<_, Infallible>(move |slot: *mut Self| {
62 let slot: *mut T = slot as _;
64
65 unwrap!(value.__init(slot));
67
68 Ok(())
69 })
70 }
71 }
72}
73
74pub trait InitMaybeUninit<T> {
77 fn init_with<I: Init<T>>(&mut self, init: I) -> &mut T {
79 unwrap!(self.try_init_with(init))
80 }
81
82 fn try_init_with<I: Init<T, E>, E>(&mut self, init: I) -> Result<&mut T, E>;
84
85 fn init_zeroed(&mut self) -> &mut T
87 where
88 T: Zeroable,
89 {
90 self.init_with(pinned_init::init_zeroed())
91 }
92}
93
94impl<T> InitMaybeUninit<T> for MaybeUninit<T> {
95 fn try_init_with<I: Init<T, E>, E>(&mut self, init: I) -> Result<&mut T, E> {
96 unsafe {
97 Init::<T, E>::__init(init, self.as_mut_ptr())?;
98
99 Ok(self.assume_init_mut())
100 }
101 }
102}
103
104pub trait InitDefault: Sized {
112 fn init_default() -> impl Init<Self>;
114}
115
116#[cfg(test)]
117#[cfg_attr(coverage_nightly, coverage(off))]
118mod tests {
119 use core::cell::{Cell, UnsafeCell};
120 use core::mem::MaybeUninit;
121
122 use super::{into_init, Init, InitMaybeUninit, IntoFallibleInit, UnsafeCellInit};
123
124 struct Droppable<'a>(&'a Cell<u32>);
125
126 impl Drop for Droppable<'_> {
127 fn drop(&mut self) {
128 self.0.set(self.0.get() + 1);
129 }
130 }
131
132 #[test]
133 fn maybe_uninit_init_with() {
134 let drops = Cell::new(0);
135
136 let mut slot = MaybeUninit::<Droppable>::uninit();
137 let d = slot.init_with(Droppable(&drops));
138 assert_eq!(d.0.get(), 0);
139
140 unsafe { slot.assume_init_drop() };
142 assert_eq!(drops.get(), 1);
143
144 let mut slot = MaybeUninit::<u32>::uninit();
145 assert_eq!(*slot.init_zeroed(), 0);
146 assert_eq!(*slot.init_with(7), 7);
147 }
148
149 #[test]
150 fn into_init_and_into_fallible() {
151 let drops = Cell::new(0);
152
153 let mut slot = MaybeUninit::<Droppable>::uninit();
154
155 let r = slot.try_init_with(into_init(|| {
157 Err::<Droppable, i32>(3).map(|d| d.into_fallible())
158 }));
159 assert_eq!(r.err(), Some(3));
160 assert_eq!(drops.get(), 0);
161
162 let r = slot.try_init_with(into_init(|| {
163 Ok::<_, i32>(Droppable(&drops).into_fallible())
164 }));
165 assert!(r.is_ok());
166
167 unsafe { slot.assume_init_drop() };
169 assert_eq!(drops.get(), 1);
170
171 let init = Droppable(&drops).into_fallible::<i32>();
173 drop(init);
174 assert_eq!(drops.get(), 2);
175 }
176
177 #[test]
178 fn unsafe_cell_init() {
179 let mut slot = MaybeUninit::<UnsafeCell<u32>>::uninit();
180 let cell = slot.init_with(UnsafeCell::init(42u32));
181 assert_eq!(*cell.get_mut(), 42);
182 }
183
184 #[test]
185 fn value_is_an_initializer() {
186 fn init<T>(init: impl Init<T>) -> T {
187 let mut slot = MaybeUninit::uninit();
188 slot.init_with(init);
189
190 unsafe { slot.assume_init() }
192 }
193
194 assert_eq!(init(5u8), 5);
195 }
196}