2个版本
0.1.1 | 2021年4月25日 |
---|---|
0.1.0 | 2021年4月24日 |
#1840 在 Rust模式
29KB
192 行
project-uninit
为包装在MaybeUninit<_>
中的结构体提供安全(和不安全)访问和初始化字段的Rust宏
此crate使用Rust 1.51中引入的ptr::addr_of!
和ptr::addr_of_mut!
宏来避免未定义行为。
查看文档获取更多信息示例。
示例
逐个字段初始化结构体
use core::mem::MaybeUninit;
use project_uninit::partial_init;
#[derive(PartialEq, Eq, Debug)]
struct Inner { value1: u8, value2: (i32, bool) }
#[derive(PartialEq, Eq, Debug)]
struct MyStruct { name: &'static str, inner: Inner }
let mut target = MaybeUninit::<MyStruct>::uninit();
let name: &mut &str = partial_init!(target => name = "Foo");
assert_eq!(*name, "Foo");
*name = "Bar";
let (value1, value2_0): (&mut u8, &mut i32) = partial_init!(target => {
inner => value1: 0xff,
inner => value2 => 0: 1000,
});
assert_eq!(*value1, 0xff);
assert_eq!(*value2_0, 1000);
*value2_0 *= 2;
let value2_1: &mut bool = partial_init!(target => inner => value2 => 1 = true);
assert_eq!(*value2_1, true);
assert_eq!(unsafe { target.assume_init() }, MyStruct {
name: "Bar",
inner: Inner { value1: 0xff, value2: (2000, true) },
});
获取MaybeUninit<_>
结构体字段的引用
use project_uninit::project_uninit;
#[derive(PartialEq, Eq, Debug)]
struct Person { name: &'static str, age: u32 }
let person = MaybeUninit::new(Person {
name: "Alice",
age: 22,
});
let (name, age): (&MaybeUninit<&str>, &MaybeUninit<u32>) = project_uninit!(
person => { name, age }
);
assert_eq!(unsafe { name.assume_init() }, "Alice");
assert_eq!(unsafe { age.assume_init() }, 22);
获取MaybeUninit<_>
结构体字段的可变引用
use project_uninit::project_uninit_mut;
let mut person = MaybeUninit::new(Person {
name: "Alice",
age: 22,
});
let (name, age): (&mut MaybeUninit<&str>, &mut MaybeUninit<u32>) = project_uninit_mut!(
person => { name, age }
);
*name = MaybeUninit::new("Alicia");
*age = MaybeUninit::new(24);
assert_eq!(unsafe { person.assume_init() }, Person {
name: "Alicia",
age: 24,
});