2 个不稳定版本
| 0.1.0 | 2022年5月5日 |
|---|---|
| 0.0.0 | 2022年4月15日 |
#7 在 #maybe-uninit
5KB
56 行
Place
Rust 中的 Placement new
一个简单的 MaybeUninit 包装器,允许通过逐字段安全地初始化结构体
用法
use place::place;
use std::mem::MaybeUninit;
struct MyCoolStruct {
b: bool,
u: u32,
}
let mut buf = MaybeUninit::uninit();
let x: &mut MyCoolStruct = place!(
buf,
MyCoolStruct {
b: true,
u: 69420,
}
);
// SAFETY: buf has been initialized above
unsafe { buf.assume_init_drop() };
lib.rs:
Rust 中的 Placement new