File Index
Files and directories
Sign in
FILE

big_struct_in_place.rs

File preview with syntax highlighting.

linux-6.19.14/rust/pin-init/examples/big_struct_in_place.rs RUST
 1// SPDX-License-Identifier: Apache-2.0 OR MIT
 2
 3use pin_init::*;
 4
 5// Struct with size over 1GiB
 6#[derive(Debug)]
 7#[allow(dead_code)]
 8pub struct BigStruct {
 9    buf: [u8; 1024 * 1024 * 1024],
10    a: u64,
11    b: u64,
12    c: u64,
13    d: u64,
14    managed_buf: ManagedBuf,
15}
16
17#[derive(Debug)]
18pub struct ManagedBuf {
19    buf: [u8; 1024 * 1024],
20}
21
22impl ManagedBuf {
23    pub fn new() -> impl Init<Self> {
24        init!(ManagedBuf { buf <- init_zeroed() })
25    }
26}
27
28fn main() {
29    #[cfg(any(feature = "std", feature = "alloc"))]
30    {
31        // we want to initialize the struct in-place, otherwise we would get a stackoverflow
32        let buf: Box<BigStruct> = Box::init(init!(BigStruct {
33            buf <- init_zeroed(),
34            a: 7,
35            b: 186,
36            c: 7789,
37            d: 34,
38            managed_buf <- ManagedBuf::new(),
39        }))
40        .unwrap();
41        println!("{}", core::mem::size_of_val(&*buf));
42    }
43}
~/ Read-only file preview.