2 unstable releases

0.1.0 May 5, 2022
0.0.0 Apr 15, 2022

#5 in #maybe-uninit

MIT/Apache

5KB
56 lines

Place

Placement new in Rust

A simple wrapper around MaybeUninit that allows one to simply and safely initialize a struct field-by-field

Usage

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:

Placement new in Rust

No runtime deps