#![no_std]
#![doc = concat!(
include_str!("../README.md"),
"\n\n",
include_str!("../README_CN.md"),
)]
use core::{mem::MaybeUninit, pin::Pin};
#[doc(hidden)]
pub extern crate self as inplace_init;
#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(feature = "std")]
extern crate std;
pub use inplace_init_macros::{Init, init, pin_init, pin_init_local};
mod init;
mod storage;
pub use init::{
Init, InitDefault, InitDefaultInit, InitDefaultValue, InitField, InitFieldInit, InitFields,
InitFn, InitFnRaw, InitMode, InitRaw, InitReceipt, InitSlot, InitState, InitTargetPointer,
InitTyBool, InitTyChar, InitTyI8, InitTyI16, InitTyI32, InitTyI64, InitTyI128, InitTyIsize,
InitTyU8, InitTyU16, InitTyU32, InitTyU64, InitTyU128, InitTyUsize, MapInitError, MovableField,
PinInitMode, PinnedField, RawInit, Set, Unset,
};
pub use storage::{InitStorage, InitValue};
#[cfg(feature = "alloc")]
pub use storage::{InitBoxExt, PinInitBoxExt};
pub fn init_in<'slot, T, I, E>(
slot: &'slot mut MaybeUninit<T>,
initializer: I,
) -> Result<InitReceipt<'slot, T, InitMode>, E>
where
I: Init<T, E>,
{
let slot = unsafe { InitSlot::__from_raw(slot.as_mut_ptr()) };
initializer.initialize(slot)
}
pub unsafe fn pin_init_in<'slot, T, I, E>(
slot: Pin<&'slot mut MaybeUninit<T>>,
initializer: I,
) -> Result<InitReceipt<'slot, T, PinInitMode>, E>
where
I: Init<T, E, PinInitMode>,
{
let storage = unsafe { Pin::get_unchecked_mut(slot) };
let slot = unsafe { InitSlot::__from_raw(storage.as_mut_ptr()) };
initializer.initialize(slot)
}
#[doc(hidden)]
pub const fn __validate_init_fields<T: InitFields>(provided: &[(&str, bool)], pinned: bool) {
let expected = T::FIELDS;
assert!(
expected.len() == T::FIELD_COUNT,
"`InitFields` field metadata has an incorrect length"
);
assert!(
expected.len() == provided.len(),
"The number of initialized fields does not match the target struct"
);
let mut expected_index = 0;
while expected_index < expected.len() {
let (expected_name, is_pinned) = expected[expected_index];
let mut prior_index = 0;
while prior_index < expected_index {
let (prior_name, _) = expected[prior_index];
assert!(
!__str_eq(expected_name, prior_name),
"`InitFields` field names must be unique"
);
prior_index += 1;
}
let mut matching_index = 0;
let mut match_count = 0;
let mut initialized_in_place = false;
while matching_index < provided.len() {
let (provided_name, provided_in_place) = provided[matching_index];
if __str_eq(expected_name, provided_name) {
match_count += 1;
initialized_in_place = provided_in_place;
}
matching_index += 1;
}
assert!(
match_count == 1,
"Initialized fields do not match the target struct's fields"
);
assert!(
!is_pinned || (pinned && initialized_in_place),
"`#[pin]` fields must be initialized with the `<-` syntax in `pin_init!`"
);
expected_index += 1;
}
}
const fn __str_eq(left: &str, right: &str) -> bool {
let left = left.as_bytes();
let right = right.as_bytes();
if left.len() != right.len() {
return false;
}
let mut index = 0;
while index < left.len() {
if left[index] != right[index] {
return false;
}
index += 1;
}
true
}