feat: add main5

This commit is contained in:
2020-11-21 00:15:03 +08:00
parent 30dc4a1416
commit 9688a0ccef
4 changed files with 63 additions and 1 deletions

View File

@@ -2,6 +2,7 @@ mod main_1;
mod main_2;
mod main_3;
mod main_4;
mod main_5;
fn main() {
println!("{}", "-".repeat(88));
@@ -12,4 +13,6 @@ fn main() {
main_3::run_main();
println!("{}", "-".repeat(88));
main_4::run_main();
println!("{}", "-".repeat(88));
main_5::run_main();
}

View File

@@ -0,0 +1,24 @@
use zerocopy::FromBytes;
use zerocopy::AsBytes;
// use zerocopy::Unaligned;
use zerocopy::LayoutVerified;
use std::ops::Deref;
#[derive(FromBytes, AsBytes, Debug)]
#[repr(C)]
struct Header {
name: [u8; 4],
size: u32,
reserved: u64,
}
pub fn run_main() {
let h = Header {
name: [1,2,3,4],
size: 0xabcdef,
reserved: 0x0123456789abcdef,
};
println!("{:?}", h.as_bytes());
let h2: Option<LayoutVerified<&[u8], [Header]>> = LayoutVerified::new_slice(h.as_bytes());
println!("{:?}", h2.unwrap().deref()[0]);
}