Path: blob/master/rust/pin-init/examples/big_struct_in_place.rs
29278 views
// SPDX-License-Identifier: Apache-2.0 OR MIT12use pin_init::*;34// Struct with size over 1GiB5#[derive(Debug)]6#[allow(dead_code)]7pub struct BigStruct {8buf: [u8; 1024 * 1024 * 1024],9a: u64,10b: u64,11c: u64,12d: u64,13managed_buf: ManagedBuf,14}1516#[derive(Debug)]17pub struct ManagedBuf {18buf: [u8; 1024 * 1024],19}2021impl ManagedBuf {22pub fn new() -> impl Init<Self> {23init!(ManagedBuf { buf <- init_zeroed() })24}25}2627fn main() {28#[cfg(any(feature = "std", feature = "alloc"))]29{30// we want to initialize the struct in-place, otherwise we would get a stackoverflow31let buf: Box<BigStruct> = Box::init(init!(BigStruct {32buf <- init_zeroed(),33a: 7,34b: 186,35c: 7789,36d: 34,37managed_buf <- ManagedBuf::new(),38}))39.unwrap();40println!("{}", core::mem::size_of_val(&*buf));41}42}434445