diff --git a/__ffi/c_layout/Cargo.lock b/__ffi/c_layout/Cargo.lock index 5f2e2cf..808b026 100644 --- a/__ffi/c_layout/Cargo.lock +++ b/__ffi/c_layout/Cargo.lock @@ -1,5 +1,80 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +[[package]] +name = "bincode" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f30d3a39baa26f9651f17b375061f3233dde33424a8b72b0dbe93a68a0bc896d" +dependencies = [ + "byteorder", + "serde", +] + +[[package]] +name = "byteorder" +version = "1.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" + [[package]] name = "c_layout" version = "0.1.0" +dependencies = [ + "bincode", + "serde", +] + +[[package]] +name = "proc-macro2" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "serde" +version = "1.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b88fa983de7720629c9387e9f517353ed404164b1e482c970a90c1a4aaf7dc1a" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbd1ae72adb44aab48f325a02444a5fc079349a8d804c1fc922aed3f7454c74e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "syn" +version = "1.0.48" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc371affeffc477f42a221a1e4297aedcea33d47d19b61455588bd9d8f6b19ac" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "unicode-xid" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" diff --git a/__ffi/c_layout/Cargo.toml b/__ffi/c_layout/Cargo.toml index 921e22b..af17453 100644 --- a/__ffi/c_layout/Cargo.toml +++ b/__ffi/c_layout/Cargo.toml @@ -7,3 +7,5 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +serde = { version = "1.0", features = ["derive"] } +bincode = "*" \ No newline at end of file diff --git a/__ffi/c_layout/src/main.rs b/__ffi/c_layout/src/main.rs index c5e7c35..84c74a8 100644 --- a/__ffi/c_layout/src/main.rs +++ b/__ffi/c_layout/src/main.rs @@ -1,6 +1,7 @@ mod main_1; mod main_2; mod main_3; +mod main_4; fn main() { println!("{}", "-".repeat(88)); @@ -9,4 +10,6 @@ fn main() { main_2::run_main(); println!("{}", "-".repeat(88)); main_3::run_main(); + println!("{}", "-".repeat(88)); + main_4::run_main(); } \ No newline at end of file diff --git a/__ffi/c_layout/src/main_3.rs b/__ffi/c_layout/src/main_3.rs index 1509caa..f6463b8 100644 --- a/__ffi/c_layout/src/main_3.rs +++ b/__ffi/c_layout/src/main_3.rs @@ -20,7 +20,7 @@ fn parse>(path: P) -> Result<()> { unsafe { mem::transmute(h) } }; - println!("{:#?}", header); + println!("{:?}", header); Ok(()) } fn write>(path: P, h: Header) -> Result<()> { diff --git a/__ffi/c_layout/src/main_4.rs b/__ffi/c_layout/src/main_4.rs new file mode 100644 index 0000000..bacf25e --- /dev/null +++ b/__ffi/c_layout/src/main_4.rs @@ -0,0 +1,25 @@ +use std::mem; +use std::io::Read; +use serde::Deserialize; + +#[derive(Deserialize, Debug)] +#[repr(C)] +struct Header { + name: [u8; 4], + size: u32, + reserved: u64, +} + +const HEADER: &[u8]= &[1, 2, 3, 4, 239, 205, 171, 0, 239, 205, 171, 137, 103, 69, 35, 1]; + +pub fn run_main() { + let header: Header = { + let mut encoded = [0u8; mem::size_of::
()]; + + let mut reader = HEADER; + reader.read_exact(&mut encoded).unwrap(); + bincode::deserialize(&encoded[..]).unwrap() + }; + + println!("{:?}", header); +} \ No newline at end of file