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

@@ -22,6 +22,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"bincode", "bincode",
"serde", "serde",
"zerocopy",
] ]
[[package]] [[package]]
@@ -73,8 +74,41 @@ dependencies = [
"unicode-xid", "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]] [[package]]
name = "unicode-xid" name = "unicode-xid"
version = "0.2.1" version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" 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",
]

View File

@@ -8,4 +8,5 @@ edition = "2018"
[dependencies] [dependencies]
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
bincode = "*" bincode = "*"
zerocopy = "0.3.0"

View File

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