-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathzeroing.rs
More file actions
37 lines (31 loc) · 847 Bytes
/
zeroing.rs
File metadata and controls
37 lines (31 loc) · 847 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#![cfg_attr(RUSTC_USE_FEATURE, feature(lint_reasons))]
#![cfg_attr(RUSTC_USE_FEATURE, feature(raw_ref_op))]
use std::marker::PhantomPinned;
use pin_init::*;
const MARKS: usize = 64;
#[pin_data]
#[derive(Zeroable)]
pub struct Foo {
buf: [u8; 1024 * 1024],
marks: [*mut u8; MARKS],
pos: usize,
#[pin]
_pin: PhantomPinned,
}
impl Foo {
pub fn new() -> impl PinInit<Self> {
pin_init!(&this in Self {
marks: {
let ptr = this.as_ptr();
// SAFETY: project from the NonNull<Foo> to the buf field
let ptr = unsafe { &raw mut (*ptr).buf }.cast::<u8>();
[ptr; MARKS]},
..Zeroable::init_zeroed()
})
}
}
#[test]
#[cfg(any(feature = "std", feature = "alloc"))]
fn test() {
let _ = Box::pin_init(Foo::new()).unwrap();
}