From 9688a0ccef9d3b80958b2abdbf30bb821d44af04 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sat, 21 Nov 2020 00:15:03 +0800 Subject: [PATCH] feat: add main5 --- __ffi/c_layout/Cargo.lock | 34 ++++++++++++++++++++++++++++++++++ __ffi/c_layout/Cargo.toml | 3 ++- __ffi/c_layout/src/main.rs | 3 +++ __ffi/c_layout/src/main_5.rs | 24 ++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 __ffi/c_layout/src/main_5.rs diff --git a/__ffi/c_layout/Cargo.lock b/__ffi/c_layout/Cargo.lock index 808b026..e9aa941 100644 --- a/__ffi/c_layout/Cargo.lock +++ b/__ffi/c_layout/Cargo.lock @@ -22,6 +22,7 @@ version = "0.1.0" dependencies = [ "bincode", "serde", + "zerocopy", ] [[package]] @@ -73,8 +74,41 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "synstructure" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b834f2d66f734cb897113e34aaff2f1ab4719ca946f9a7358dba8f8064148701" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "unicode-xid", +] + [[package]] name = "unicode-xid" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" + +[[package]] +name = "zerocopy" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6580539ad917b7c026220c4b3f2c08d52ce54d6ce0dc491e66002e35388fab46" +dependencies = [ + "byteorder", + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d498dbd1fd7beb83c86709ae1c33ca50942889473473d287d56ce4770a18edfb" +dependencies = [ + "proc-macro2", + "syn", + "synstructure", +] diff --git a/__ffi/c_layout/Cargo.toml b/__ffi/c_layout/Cargo.toml index af17453..61258df 100644 --- a/__ffi/c_layout/Cargo.toml +++ b/__ffi/c_layout/Cargo.toml @@ -8,4 +8,5 @@ edition = "2018" [dependencies] serde = { version = "1.0", features = ["derive"] } -bincode = "*" \ No newline at end of file +bincode = "*" +zerocopy = "0.3.0" diff --git a/__ffi/c_layout/src/main.rs b/__ffi/c_layout/src/main.rs index 84c74a8..7852d4d 100644 --- a/__ffi/c_layout/src/main.rs +++ b/__ffi/c_layout/src/main.rs @@ -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(); } \ No newline at end of file diff --git a/__ffi/c_layout/src/main_5.rs b/__ffi/c_layout/src/main_5.rs new file mode 100644 index 0000000..7694eb3 --- /dev/null +++ b/__ffi/c_layout/src/main_5.rs @@ -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::new_slice(h.as_bytes()); + println!("{:?}", h2.unwrap().deref()[0]); +}