1#![no_std]
2#![doc = concat!(
3 include_str!("../README.md"),
4 "\n\n",
5 include_str!("../README_CN.md"),
6)]
7
8use core::{mem::MaybeUninit, pin::Pin};
9
10#[doc(hidden)]
11pub extern crate self as inplace_init;
12
13#[cfg(feature = "alloc")]
14extern crate alloc;
15
16#[cfg(feature = "std")]
17extern crate std;
18
19pub use inplace_init_macros::{Init, init, pin_init, pin_init_local};
20
21mod init;
22mod storage;
23
24pub use init::{
25 Init, InitDefault, InitDefaultInit, InitDefaultValue, InitField, InitFieldInit, InitFields,
26 InitFn, InitFnRaw, InitMode, InitRaw, InitReceipt, InitSlot, InitState, InitTargetPointer,
27 InitTyBool, InitTyChar, InitTyI8, InitTyI16, InitTyI32, InitTyI64, InitTyI128, InitTyIsize,
28 InitTyU8, InitTyU16, InitTyU32, InitTyU64, InitTyU128, InitTyUsize, MapInitError, MovableField,
29 PinInitMode, PinnedField, RawInit, Set, Unset,
30};
31pub use storage::{InitStorage, InitValue};
32
33#[cfg(feature = "alloc")]
34pub use storage::{InitBoxExt, PinInitBoxExt};
35
36pub fn init_in<'slot, T, I, E>(
44 slot: &'slot mut MaybeUninit<T>,
45 initializer: I,
46) -> Result<InitReceipt<'slot, T, InitMode>, E>
47where
48 I: Init<T, E>,
49{
50 let slot = unsafe { InitSlot::__from_raw(slot.as_mut_ptr()) };
52 initializer.initialize(slot)
53}
54
55pub unsafe fn pin_init_in<'slot, T, I, E>(
66 slot: Pin<&'slot mut MaybeUninit<T>>,
67 initializer: I,
68) -> Result<InitReceipt<'slot, T, PinInitMode>, E>
69where
70 I: Init<T, E, PinInitMode>,
71{
72 let storage = unsafe { Pin::get_unchecked_mut(slot) };
74 let slot = unsafe { InitSlot::__from_raw(storage.as_mut_ptr()) };
76 initializer.initialize(slot)
77}
78
79#[doc(hidden)]
85pub const fn __validate_init_fields<T: InitFields>(provided: &[(&str, bool)], pinned: bool) {
86 let expected = T::FIELDS;
87 assert!(
88 expected.len() == T::FIELD_COUNT,
89 "`InitFields` field metadata has an incorrect length"
90 );
91 assert!(
92 expected.len() == provided.len(),
93 "The number of initialized fields does not match the target struct"
94 );
95
96 let mut expected_index = 0;
97 while expected_index < expected.len() {
98 let (expected_name, is_pinned) = expected[expected_index];
99 let mut prior_index = 0;
100 while prior_index < expected_index {
101 let (prior_name, _) = expected[prior_index];
102 assert!(
103 !__str_eq(expected_name, prior_name),
104 "`InitFields` field names must be unique"
105 );
106 prior_index += 1;
107 }
108
109 let mut matching_index = 0;
110 let mut match_count = 0;
111 let mut initialized_in_place = false;
112 while matching_index < provided.len() {
113 let (provided_name, provided_in_place) = provided[matching_index];
114 if __str_eq(expected_name, provided_name) {
115 match_count += 1;
116 initialized_in_place = provided_in_place;
117 }
118 matching_index += 1;
119 }
120
121 assert!(
122 match_count == 1,
123 "Initialized fields do not match the target struct's fields"
124 );
125 assert!(
126 !is_pinned || (pinned && initialized_in_place),
127 "`#[pin]` fields must be initialized with the `<-` syntax in `pin_init!`"
128 );
129 expected_index += 1;
130 }
131}
132
133const fn __str_eq(left: &str, right: &str) -> bool {
136 let left = left.as_bytes();
137 let right = right.as_bytes();
138 if left.len() != right.len() {
139 return false;
140 }
141
142 let mut index = 0;
143 while index < left.len() {
144 if left[index] != right[index] {
145 return false;
146 }
147 index += 1;
148 }
149 true
150}