Expand description
§Inplace-Init
Inplace-Init constructs Rust values directly in caller-provided storage. It supports ordinary initialization, initialization at a stable pinned address, and field-by-field construction with rollback when an initializer fails or panics.
§What it provides
InitSlotgives an initializer one exclusive, uninitialized destination.InitReceipttracks the initialized value and owns the responsibility to drop it. Dropping the receipt drops the value; movable receipts can also return the value withinto_inner.InitandInitDefaultdescribe custom initialization and in-place default construction.#[derive(Init)]generates a typestate builder and the field metadata used by the initializer macros.init!,pin_init!, andpin_init_local!provide field-oriented initialization syntax.- The runtime crate is
no_stdby default.allocandstdenable optional collection and heap-allocation support.
The workspace uses Rust 2024 edition and is released under either the Apache-2.0 or MIT license.
§Installation
Add the runtime crate to your Cargo.toml:
[dependencies]
inplace-init = "0.1"The default feature set is empty. Enable alloc for heap helpers and alloc-backed defaults, or enable std for those features plus standard-library hash collections:
[dependencies]
inplace-init = { version = "0.1", features = ["alloc"] }§Quick start: initialize fields in a supplied slot
Derive Init on a struct to expose its fields to the field initializer macros. The <- operator takes an initializer for that field. The following example constructs Packet directly in MaybeUninit storage:
use core::mem::MaybeUninit;
use inplace_init::{init, init_in, Init, InitMode, InitReceipt, InitTyU32, InitTyU8};
#[derive(Init)]
struct Packet {
id: u32,
retries: u8,
}
let mut storage: MaybeUninit<Packet> = MaybeUninit::uninit();
let receipt: InitReceipt<'_, Packet, InitMode> = init_in(
&mut storage,
init!(Packet {
id <- InitTyU32::<42>,
retries <- InitTyU8::<3>,
}),
)
.unwrap();
assert_eq!(receipt.id, 42);
assert_eq!(receipt.retries, 3);init_in returns Result<InitReceipt<...>, E>. The receipt borrows the destination exclusively and drops the value when it is dropped. In movable mode, call receipt.into_inner() to move the value out, or use InitStorage when an owning container is more convenient.
§Typestate builder
The derive generates a Type::pin_init() builder entry point, and also generates Type::init() when the struct has no #[pin] fields. Each setter consumes the builder and marks one field as initialized in the type system. A field without a default must be set before build() is available; a field with a declared default may be overridden. Named-field setters use the field names; tuple-struct setters are named field_0, field_1, and so on.
use inplace_init::{Init, InitStorage, InitTyU32, InitTyU8, InitValue};
#[derive(Init)]
struct Request {
#[init(default = InitTyU32::<30>)]
timeout_ms: u32,
attempts: u8,
}
let storage: InitStorage<Request> = InitStorage::<Request>::uninit();
let request: InitValue<Request> = storage
.initialize(Request::init().attempts(InitTyU8::<2>).build())
.unwrap();
assert_eq!(request.timeout_ms, 30);
assert_eq!(request.attempts, 2);#[init(default)] uses the field type’s InitDefault implementation. #[init(default = initializer)] stores the supplied initializer as the initial builder state; that expression must be an infallible initializer for the field’s address mode. Use the setter for a fallible initializer. Both forms can be replaced by the field setter. For a pinned builder, Type::pin_init() selects pinned initialization for #[pin] fields and movable initialization for other fields.
build() completes the type-state description and returns an initializer; it does not construct the value by itself. Pass that initializer to InitStorage::initialize, init_in, or a heap extension method.
Builder fields may use different error types. The generated initializer preserves them in a generated, documentation-hidden error enum with one FieldN variant per field. The macro form’s optional ? ErrorType instead selects one shared error type for its field initializers.
§Pinned and self-referential values
Mark fields whose invariants require a stable address with #[pin]. pin_init_local! creates local pinned storage and a receipt whose destructor runs before the backing storage leaves scope. The @target expression writes the final address into a raw-pointer field:
use core::{marker::PhantomPinned, pin::Pin};
use inplace_init::{Init, pin_init_local};
#[derive(Init)]
struct SelfRef {
#[pin]
self_ptr: *const SelfRef,
#[pin]
_pin: PhantomPinned,
}
pin_init_local! {
let mut value = SelfRef {
self_ptr <- @target,
_pin <- default,
};
}
let target: Pin<&SelfRef> = value.as_pin_ref();
let target: &SelfRef = target.get_ref();
assert_eq!(target.self_ptr, target as *const SelfRef);@target is available only in pin_init! and pin_init_local!, and only for raw *const Target or *mut Target fields. The target struct must derive Init. Pinned fields must be initialized with <- syntax through a pinned entry point.
With the alloc feature, PinInitBoxExt::pin_box() constructs a pinned initializer in its final heap allocation and returns Pin<Box<T>>:
#[cfg(feature = "alloc")]
fn pinned_box_example() {
use core::{marker::PhantomPinned, pin::Pin};
use inplace_init::{Init, PinInitBoxExt, pin_init};
use std::boxed::Box;
#[derive(Init)]
struct SelfRef {
#[pin]
self_ptr: *const SelfRef,
#[pin]
_pin: PhantomPinned,
}
let value: Pin<Box<SelfRef>> = pin_init!(SelfRef {
self_ptr <- @target,
_pin <- default,
})
.pin_box()
.unwrap();
let target: &SelfRef = value.as_ref().get_ref();
assert_eq!(target.self_ptr, target as *const SelfRef);
}
#[cfg(feature = "alloc")]
pinned_box_example();The code block defines its own SelfRef type and runs when the alloc feature is enabled. For lower-level control, pin_init_in accepts Pin<&mut MaybeUninit<T>> and is unsafe: the caller must keep that storage pinned until the receipt is dropped. If the receipt is forgotten, the storage must not be moved, reused, or exposed for another initialization.
§Initializer macro syntax
| Form | Purpose |
|---|---|
init!(Type { field <- initializer, ... }) | Movable field-by-field initializer. |
init!(Type { field <- initializer, ... } ? ErrorType) | Same, with the chosen error type for the initializer. |
init!(Type { .. }) | Calls InitDefault for the whole target in its destination slot. |
init!(Type { field <- default, ... }) | Calls InitDefault for an individual field in its destination slot. |
pin_init!(Type { ... }) | Produces a pinned field-by-field initializer. |
pin_init_local! { let mut name = Type { ... }; } | Creates local pinned storage and binds its receipt. |
Field selectors are identifiers for named fields and numeric indices for tuple-struct fields. Explicit field initialization must name each field exactly once; duplicate, missing, or unknown fields fail at compile time. The whole-value .. form cannot be combined with per-field entries. Field-oriented initialization needs #[derive(Init)]; whole-value default initialization only needs an appropriate InitDefault implementation.
Without ? ErrorType, initializer expressions use core::convert::Infallible. With an explicit error type, each field initializer must use that error type; map different errors with Init::map_err or the initializer’s .map_err(...) method. The same error suffix is supported by pin_init!. In pin_init_local!, the ? form propagates the selected error from the enclosing function.
init! produces a movable initializer and rejects @target. pin_init! produces an initializer for use with pin_init_in or PinInitBoxExt::pin_box. pin_init_local! handles the local storage and pinning entry point for you.
§Defaults
The built-in InitDefault implementations are designed to construct values in their destination slots:
- In every address mode:
(),Option<T>(asNone),PhantomData<T>, andPhantomPinned. - Arrays
[T; N]and two-element tuples(A, B), when their elements implementInitDefaultfor the same mode. Array initialization cleans up completed elements if a later element panics. bool,char, and the signed and unsigned integer types, withfalse,'\0', or zero as their defaults.InitTyBool,InitTyChar,InitTyI8throughInitTyI128/InitTyIsize, andInitTyU8throughInitTyU128/InitTyUsizeare const-generic initializers for explicit scalar values.- With
alloc:String,Vec,VecDeque,BinaryHeap,LinkedList,BTreeMap,BTreeSet,Rc<T>, andArc<T>. - With
std:HashMapandHashSet.
InitDefaultValue<T> adapts T: Default by calling Default::default() and writing the resulting value. It is available for movable initialization. Use a direct InitDefault implementation when construction must happen at the destination address, including for pinned values.
§Storage and initializer APIs
| API | Description |
|---|---|
Init<T, E, Mode> | Unsafe trait for an initializer. Implementations must return one complete value and clean up all partial state on errors or panic. |
InitDefault<T, Mode> | Unsafe trait for constructing a default value directly in a slot. |
InitSlot<'slot, T, Mode> | One-use access to uninitialized storage; write constructs a complete value and returns its receipt. |
InitReceipt<'slot, T, Mode> | Owns the initialized value’s drop responsibility. Movable mode supports DerefMut and into_inner; pinned mode exposes as_pin_ref and as_pin_mut. |
init_in | Safe entry point for movable initialization in &mut MaybeUninit<T>. |
pin_init_in | Unsafe entry point for pinned initialization in Pin<&mut MaybeUninit<T>>. |
InitStorage<T> / InitValue<T> | Owned movable storage and its initialized value wrapper. InitValue dereferences to T and can return it with into_inner. |
InitFn<T, E, F> | Safe adapter for a no-argument closure FnOnce() -> Result<T, E> in movable mode; a successful value is written into the target slot. |
InitFnRaw<T, E, F, Mode> | Unsafe closure adapter for advanced custom initialization directly in a slot. |
InitRaw / RawInit | Adapter for initializers that need direct raw-pointer access. Implementing InitRaw is unsafe. |
An initializer must retain receipts for completed fields until every later field succeeds. This is what lets the generated macro and builder clean up earlier fields if a later initializer returns an error or panics. Implement Init or InitDefault only when you can uphold their full unsafe contracts.
§Feature flags
| Feature | Effect |
|---|---|
| (default) | no_std runtime with no allocation-backed defaults. |
alloc | Enables alloc collection defaults, Rc / Arc defaults, InitBoxExt::init_box(), and PinInitBoxExt::pin_box(). |
std | Enables alloc and the HashMap / HashSet default implementations. |
§Constraints
#[derive(Init)]supports structs, including named-field, tuple, and unit structs; it does not support enums or unions.#[derive(Init)]rejects#[repr(packed)], because field projection requires correctly aligned fields.- Field macro invocations must initialize every target field exactly once. Use
..for whole-value defaults ordefaultfor individual field defaults. - A pinned field requires
pin_init!,pin_init_local!, or the generated pinned builder and an initializer compatible with the pinned address mode. pin_init_inis unsafe because storage stability is a caller responsibility. The conveniencepin_init_local!andpin_box()APIs manage their backing storage.
§License
Licensed under either of Apache License, Version 2.0 or MIT License, at your option.
§Inplace-Init
Inplace-Init 可在调用方提供的存储中直接构造 Rust 值,支持普通初始化、在稳定固定地址上的初始化,以及逐字段构造并在初始化器失败或 panic 时回滚。
在线文档 · English documentation · English README
§功能概览
InitSlot为初始化器提供对一个独占、尚未初始化目标槽位的一次性访问。InitReceipt跟踪已初始化的值并独占其析构责任。凭证析构时会析构该值;普通模式下也可以通过into_inner移出值。Init与InitDefault分别描述自定义初始化和原位默认构造。#[derive(Init)]会生成类型状态 builder,以及字段初始化宏所需的字段元数据。init!、pin_init!和pin_init_local!提供字段初始化语法。- 运行时 crate 默认是
no_std。alloc和std可启用可选的集合与堆分配支持。
工作区使用 Rust 2024 edition,采用 Apache-2.0 或 MIT 双许可证。
§安装
在 Cargo.toml 中添加运行时 crate:
[dependencies]
inplace-init = "0.1"默认不启用任何 feature。启用 alloc 可使用堆分配辅助 API 和依赖 alloc 的默认实现;启用 std 则会同时启用这些能力以及标准库哈希集合的默认实现:
[dependencies]
inplace-init = { version = "0.1", features = ["alloc"] }§快速开始:在指定槽位中初始化字段
在结构体上派生 Init,即可让字段初始化宏访问其字段。<- 后面是该字段的初始化器。下面的例子直接在 MaybeUninit 存储中构造 Packet:
use core::mem::MaybeUninit;
use inplace_init::{init, init_in, Init, InitMode, InitReceipt, InitTyU32, InitTyU8};
#[derive(Init)]
struct Packet {
id: u32,
retries: u8,
}
let mut storage: MaybeUninit<Packet> = MaybeUninit::uninit();
let receipt: InitReceipt<'_, Packet, InitMode> = init_in(
&mut storage,
init!(Packet {
id <- InitTyU32::<42>,
retries <- InitTyU8::<3>,
}),
)
.unwrap();
assert_eq!(receipt.id, 42);
assert_eq!(receipt.retries, 3);init_in 返回 Result<InitReceipt<...>, E>。凭证会独占借用目标存储,并在析构时析构值。普通模式下可以调用 receipt.into_inner() 移出值;也可以使用 InitStorage 管理拥有型存储。
§类型状态 builder
derive 会生成 Type::pin_init() builder 入口;只有结构体没有 #[pin] 字段时才会同时生成 Type::init()。每个 setter 都会消耗当前 builder,并在类型系统中标记一个字段已完成初始化。没有默认值的字段必须先设置,build() 才可用;声明了默认值的字段可以通过 setter 覆盖。具名字段的 setter 与字段同名;tuple struct 的 setter 名为 field_0、field_1 等。
use inplace_init::{Init, InitStorage, InitTyU32, InitTyU8, InitValue};
#[derive(Init)]
struct Request {
#[init(default = InitTyU32::<30>)]
timeout_ms: u32,
attempts: u8,
}
let storage: InitStorage<Request> = InitStorage::<Request>::uninit();
let request: InitValue<Request> = storage
.initialize(Request::init().attempts(InitTyU8::<2>).build())
.unwrap();
assert_eq!(request.timeout_ms, 30);
assert_eq!(request.attempts, 2);#[init(default)] 使用字段类型对应的 InitDefault 实现。#[init(default = initializer)] 将给定初始化器设为 builder 的初始字段状态;表达式必须是适用于该字段地址模式的不可失败初始化器。可失败的初始化器应通过 setter 设置。两种默认都可以通过字段 setter 替换。对于固定地址 builder,Type::pin_init() 会为 #[pin] 字段选择固定模式,为其他字段选择普通模式。
build() 只完成类型状态描述并返回一个初始化器,本身不会构造目标值。将其传给 InitStorage::initialize、init_in 或堆分配扩展方法即可执行初始化。
Builder 的各字段可以使用不同错误类型。生成的初始化器会通过一个隐藏文档的错误枚举保留这些错误,每个字段对应一个 FieldN 变体。宏形式的可选 ? ErrorType 则为各字段初始化器指定同一个错误类型。
§固定地址与自引用值
需要稳定地址的字段应标记 #[pin]。pin_init_local! 会创建局部固定存储,并返回一个凭证;该凭证先于底层存储析构。@target 会将目标最终地址写入裸指针字段:
use core::{marker::PhantomPinned, pin::Pin};
use inplace_init::{Init, pin_init_local};
#[derive(Init)]
struct SelfRef {
#[pin]
self_ptr: *const SelfRef,
#[pin]
_pin: PhantomPinned,
}
pin_init_local! {
let mut value = SelfRef {
self_ptr <- @target,
_pin <- default,
};
}
let target: Pin<&SelfRef> = value.as_pin_ref();
let target: &SelfRef = target.get_ref();
assert_eq!(target.self_ptr, target as *const SelfRef);@target 仅能用于 pin_init! 和 pin_init_local!,字段类型必须是 *const Target 或 *mut Target。目标结构体需要派生 Init。带 #[pin] 的字段必须通过固定地址入口使用 <- 语法初始化。
启用 alloc 后,PinInitBoxExt::pin_box() 会在最终堆地址上构造固定地址初始化器,并返回 Pin<Box<T>>:
#[cfg(feature = "alloc")]
fn pinned_box_example() {
use core::{marker::PhantomPinned, pin::Pin};
use inplace_init::{Init, PinInitBoxExt, pin_init};
use std::boxed::Box;
#[derive(Init)]
struct SelfRef {
#[pin]
self_ptr: *const SelfRef,
#[pin]
_pin: PhantomPinned,
}
let value: Pin<Box<SelfRef>> = pin_init!(SelfRef {
self_ptr <- @target,
_pin <- default,
})
.pin_box()
.unwrap();
let target: &SelfRef = value.as_ref().get_ref();
assert_eq!(target.self_ptr, target as *const SelfRef);
}
#[cfg(feature = "alloc")]
pinned_box_example();代码块内定义了独立的 SelfRef 类型,并会在启用 alloc feature 时运行。若需要更底层的控制,pin_init_in 接受 Pin<&mut MaybeUninit<T>>,但它是 unsafe API:调用方必须确保凭证析构前存储始终固定。如果凭证被遗忘,存储不得移动、复用,也不得再次暴露为初始化槽位。
§初始化宏语法
| 形式 | 用途 |
|---|---|
init!(Type { field <- initializer, ... }) | 普通地址模式的逐字段初始化器。 |
init!(Type { field <- initializer, ... } ? ErrorType) | 同上,并指定初始化器的错误类型。 |
init!(Type { .. }) | 在目标槽位中对整个目标调用 InitDefault。 |
init!(Type { field <- default, ... }) | 在字段自己的目标槽位中调用 InitDefault。 |
pin_init!(Type { ... }) | 生成固定地址的逐字段初始化器。 |
pin_init_local! { let mut name = Type { ... }; } | 创建局部固定存储并绑定其凭证。 |
具名字段使用标识符选择,tuple struct 字段使用数字索引。显式逐字段初始化必须恰好列出每个字段一次;重复、遗漏或未知字段都会在编译期报错。整个对象的 .. 不能与逐字段条目混用。逐字段初始化需要 #[derive(Init)];整个对象的默认初始化只需要合适的 InitDefault 实现。
省略 ? ErrorType 时,初始化器错误类型为 core::convert::Infallible。指定错误类型后,各字段初始化器必须使用相同错误类型;若错误类型不同,可通过 Init::map_err 或初始化器的 .map_err(...) 统一。pin_init! 也支持相同的错误类型后缀。在 pin_init_local! 中,带 ? 的形式会把所选错误传播到外层函数。
init! 生成普通模式初始化器,不接受 @target。pin_init! 生成的初始化器可交给 pin_init_in 或 PinInitBoxExt::pin_box。pin_init_local! 会代为管理局部存储和固定初始化入口。
§默认初始化
内置 InitDefault 实现会尝试在目标槽位中直接构造值:
- 所有地址模式均支持:
(),Option<T>(默认值为None)、PhantomData<T>和PhantomPinned。 - 当元素在相同地址模式下实现
InitDefault时,支持数组[T; N]和二元 tuple(A, B)。若数组后续元素初始化时 panic,已完成的元素会被清理。 - 支持
bool、char以及有符号和无符号整数类型,默认值分别为false、'\0'或零。InitTyBool、InitTyChar、InitTyI8至InitTyI128/InitTyIsize,以及InitTyU8至InitTyU128/InitTyUsize是对应类型的 const 泛型显式值初始化器。 - 启用
alloc:支持String、Vec、VecDeque、BinaryHeap、LinkedList、BTreeMap、BTreeSet、Rc<T>和Arc<T>。 - 启用
std:支持HashMap和HashSet。
InitDefaultValue<T> 会调用 Default::default(),再将得到的值写入目标;它要求 T: Default,并提供普通地址模式的适配。如果值必须在目标地址构造(包括固定地址值),应使用直接的 InitDefault 实现。
§存储与初始化器 API
| API | 说明 |
|---|---|
Init<T, E, Mode> | unsafe 初始化器 trait。实现必须产生一个完整值,并在错误或 panic 时清理所有部分状态。 |
InitDefault<T, Mode> | unsafe trait,用于在槽位中直接构造默认值。 |
InitSlot<'slot, T, Mode> | 对未初始化存储的一次性访问;write 构造完整值并返回其凭证。 |
InitReceipt<'slot, T, Mode> | 独占已初始化值的析构责任。普通模式支持 DerefMut 和 into_inner;固定模式提供 as_pin_ref 和 as_pin_mut。 |
init_in | 普通初始化的安全入口,接受 &mut MaybeUninit<T>。 |
pin_init_in | 固定地址初始化的 unsafe 入口,接受 Pin<&mut MaybeUninit<T>>。 |
InitStorage<T> / InitValue<T> | 拥有型普通存储及其初始化后返回的值容器。InitValue 可解引用为 T,也可通过 into_inner 取出值。 |
InitFn<T, E, F> | 在普通可移动模式下安全地适配无参数闭包 FnOnce() -> Result<T, E>;成功返回的值会写入目标槽位。 |
InitFnRaw<T, E, F, Mode> | 将闭包包装为直接写入槽位的高级初始化器;创建时需要 unsafe。 |
InitRaw / RawInit | 为需要直接操作裸指针的初始化器提供适配。实现 InitRaw 是 unsafe 操作。 |
初始化多个字段时,必须保留已完成字段的凭证,直到后续字段也成功。这样生成的宏和 builder 才能在后续初始化返回错误或 panic 时清理已完成的字段。只有能够履行完整 unsafe 契约时,才应实现 Init 或 InitDefault。
§功能开关
| Feature | 效果 |
|---|---|
| (默认) | no_std 运行时,不启用依赖动态分配的默认实现。 |
alloc | 启用 alloc 集合默认实现、Rc / Arc 默认实现、InitBoxExt::init_box() 和 PinInitBoxExt::pin_box()。 |
std | 启用 alloc,并提供 HashMap / HashSet 默认实现。 |
§限制
#[derive(Init)]支持具名字段结构体、tuple struct 和 unit struct;不支持 enum 或 union。#[derive(Init)]拒绝#[repr(packed)],因为字段投影要求字段地址满足对齐要求。- 字段初始化宏必须恰好初始化目标的每个字段。整个目标使用
..,单个字段使用default。 - 固定字段必须通过
pin_init!、pin_init_local!或生成的固定模式 builder 初始化,并使用符合固定地址模式的初始化器。 pin_init_in是 unsafe API,因为存储是否稳定由调用方负责。pin_init_local!和pin_box()会管理其底层存储。
§许可证
本项目可由你选择使用 Apache License 2.0 或 MIT License。
Re-exports§
pub use init::Init;pub use init::InitDefault;pub use init::InitDefaultValue;pub use init::InitFn;pub use init::InitFnRaw;pub use init::InitMode;pub use init::InitRaw;pub use init::InitReceipt;pub use init::InitSlot;pub use init::InitTyBool;pub use init::InitTyChar;pub use init::InitTyI8;pub use init::InitTyI16;pub use init::InitTyI32;pub use init::InitTyI64;pub use init::InitTyI128;pub use init::InitTyIsize;pub use init::InitTyU8;pub use init::InitTyU16;pub use init::InitTyU32;pub use init::InitTyU64;pub use init::InitTyU128;pub use init::InitTyUsize;pub use init::MapInitError;pub use init::PinInitMode;pub use init::RawInit;
Macros§
- init
- 为结构体创建原地初始化器;
Type { .. }会在目标槽位调用InitDefault。 Create an in-place initializer for a struct;Type { .. }callsInitDefaultin its target slot. - pin_
init - 为结构体创建固定地址初始化器;字段可用
default在字段槽位中构造默认值。 Create a pinned initializer for a struct; fields can usedefaultto construct in their slots. - pin_
init_ local - 在当前作用域创建固定存储;返回的凭证在存储之前析构。 Create pinned storage in the current scope; its receipt is dropped before the storage.
Structs§
- Init
Default Value - 使用
Default::default()按值构造的默认初始化器。 A default initializer that constructs a value by callingDefault::default()by value. - InitFn
- 将按值构造闭包适配为普通可移动模式的初始化器。 Adapts a by-value construction closure into a movable-mode initializer.
- Init
FnRaw - 由闭包直接在目标槽位中构造值的原始初始化器包装类型。 Raw initializer wrapper for closures that construct a value directly in the target slot.
- Init
Mode - 普通初始化的地址模式;初始化完成后,值可从其存储位置移出。 Marker for movable initialization; after initialization, the value may be moved out of its storage.
- Init
Receipt - 独占已初始化值及其析构责任的凭证。 A receipt that exclusively owns an initialized value and responsibility for dropping it.
- Init
Slot - 独占一个未初始化目标地址的一次性凭证。 A one-use token that exclusively owns an uninitialized target address.
- Init
Storage - 可消费的普通初始化存储。 Consumable storage for movable initialization.
- Init
TyBool - 使用编译期
bool常量构造值的初始化器。 / An initializer that constructs a value from a compile-timeboolconstant. - Init
TyChar - 使用编译期
char常量构造值的初始化器。 / An initializer that constructs a value from a compile-timecharconstant. - Init
TyI8 - 使用编译期
i8常量构造值的初始化器。 / An initializer that constructs a value from a compile-timei8constant. - Init
TyI16 - 使用编译期
i16常量构造值的初始化器。 / An initializer that constructs a value from a compile-timei16constant. - Init
TyI32 - 使用编译期
i32常量构造值的初始化器。 / An initializer that constructs a value from a compile-timei32constant. - Init
TyI64 - 使用编译期
i64常量构造值的初始化器。 / An initializer that constructs a value from a compile-timei64constant. - Init
TyI128 - 使用编译期
i128常量构造值的初始化器。 / An initializer that constructs a value from a compile-timei128constant. - Init
TyIsize - 使用编译期
isize常量构造值的初始化器。 / An initializer that constructs a value from a compile-timeisizeconstant. - Init
TyU8 - 使用编译期
u8常量构造值的初始化器。 / An initializer that constructs a value from a compile-timeu8constant. - Init
TyU16 - 使用编译期
u16常量构造值的初始化器。 / An initializer that constructs a value from a compile-timeu16constant. - Init
TyU32 - 使用编译期
u32常量构造值的初始化器。 / An initializer that constructs a value from a compile-timeu32constant. - Init
TyU64 - 使用编译期
u64常量构造值的初始化器。 / An initializer that constructs a value from a compile-timeu64constant. - Init
TyU128 - 使用编译期
u128常量构造值的初始化器。 / An initializer that constructs a value from a compile-timeu128constant. - Init
TyUsize - 使用编译期
usize常量构造值的初始化器。 / An initializer that constructs a value from a compile-timeusizeconstant. - Init
Value - 由普通初始化存储返回、拥有完整值的容器。 An owning container returned by ordinary initialization storage.
- MapInit
Error - 错误映射后的初始化器。 An initializer wrapper that converts the underlying initializer’s error.
- PinInit
Mode - 固定地址初始化的模式标记;初始化器必须在目标的最终地址构造值。 Marker for pinned initialization; the initializer must construct the value at its final address.
- RawInit
- 将高级原始地址初始化器接入安全初始化入口。
Adapts an advanced raw-pointer initializer to the safe
Initinterface.
Traits§
- Init
- 在一次性槽位中构造一个值。 An initializer that constructs a value in a one-use target slot.
- Init
Default - 在目标槽位中直接构造默认值。 Constructs a default value directly in the target slot.
- InitRaw
- 直接操作原始地址的高级初始化器。 An advanced initializer that writes directly through a raw target pointer.
Functions§
- init_in
- 在
MaybeUninit存储中初始化可移动值,并返回负责析构该值的凭证。 Initialize a movable value inMaybeUninitstorage and return the receipt that drops it. - pin_
init_ ⚠in - 在固定地址的
MaybeUninit存储中初始化值,并返回固定析构凭证。 Initialize a value in pinnedMaybeUninitstorage and return a pinned destruction receipt.
Derive Macros§
- Init
- 为结构体生成类型状态构造器;字段可用
#[init(default)]或#[init(default = 初始化器)]声明默认初始化方式。 Generate a typestate builder for a struct; fields may declare defaults with#[init(default)]or#[init(default = initializer)].