big_struct_in_place.rs 935 B

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