Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/rust/pin-init/examples/big_struct_in_place.rs
29278 views
1
// SPDX-License-Identifier: Apache-2.0 OR MIT
2
3
use pin_init::*;
4
5
// Struct with size over 1GiB
6
#[derive(Debug)]
7
#[allow(dead_code)]
8
pub 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)]
18
pub struct ManagedBuf {
19
buf: [u8; 1024 * 1024],
20
}
21
22
impl ManagedBuf {
23
pub fn new() -> impl Init<Self> {
24
init!(ManagedBuf { buf <- init_zeroed() })
25
}
26
}
27
28
fn 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
}
44
45