| use zerocopy_derive::*; | ||
| // The only valid value of this type are the bytes `0xC0C0`. | ||
| #[derive(TryFromBytes, KnownLayout, Immutable)] | ||
| #[repr(u16)] | ||
| pub enum C0C0 { | ||
| _XC0C0 = 0xC0C0, | ||
| } | ||
| #[derive(FromBytes, KnownLayout, Immutable)] | ||
| #[repr(C, align(2))] | ||
| pub struct Packet<Magic> { | ||
| magic_number: Magic, | ||
| mug_size: u8, | ||
| temperature: u8, | ||
| marshmallows: [[u8; 2]], | ||
| } | ||
| /// A packet begining with the magic number `0xC0C0`. | ||
| pub type CocoPacket = Packet<C0C0>; | ||
| /// A packet beginning with any two initialized bytes. | ||
| pub type LocoPacket = Packet<[u8; 2]>; |
| #[path = "formats/coco.rs"] | ||
| mod format; | ||
| #[unsafe(no_mangle)] | ||
| fn codegen_test(source: &[u8], count: usize) -> Option<&format::LocoPacket> { | ||
| zerocopy::FromBytes::ref_from_bytes_with_elems(source, count).ok() | ||
| } |
Sorry, the diff of this file is not supported yet
| #[path = "formats/coco.rs"] | ||
| mod format; | ||
| #[unsafe(no_mangle)] | ||
| fn codegen_test(source: &[u8]) -> Option<&format::LocoPacket> { | ||
| zerocopy::FromBytes::ref_from_bytes(source).ok() | ||
| } |
Sorry, the diff of this file is not supported yet
| #[path = "formats/coco.rs"] | ||
| mod format; | ||
| #[unsafe(no_mangle)] | ||
| fn codegen_test(source: &[u8], count: usize) -> Option<&format::LocoPacket> { | ||
| match zerocopy::FromBytes::ref_from_prefix_with_elems(source, count) { | ||
| Ok((packet, _rest)) => Some(packet), | ||
| _ => None, | ||
| } | ||
| } |
Sorry, the diff of this file is not supported yet
| #[path = "formats/coco.rs"] | ||
| mod format; | ||
| #[unsafe(no_mangle)] | ||
| fn codegen_test(source: &[u8]) -> Option<&format::LocoPacket> { | ||
| match zerocopy::FromBytes::ref_from_prefix(source) { | ||
| Ok((packet, _rest)) => Some(packet), | ||
| _ => None, | ||
| } | ||
| } |
Sorry, the diff of this file is not supported yet
| #[path = "formats/coco.rs"] | ||
| mod format; | ||
| #[unsafe(no_mangle)] | ||
| fn codegen_test(source: &[u8], count: usize) -> Option<&format::LocoPacket> { | ||
| match zerocopy::FromBytes::ref_from_suffix_with_elems(source, count) { | ||
| Ok((_rest, packet)) => Some(packet), | ||
| _ => None, | ||
| } | ||
| } |
Sorry, the diff of this file is not supported yet
| #[path = "formats/coco.rs"] | ||
| mod format; | ||
| #[unsafe(no_mangle)] | ||
| fn codegen_test(source: &[u8]) -> Option<&format::LocoPacket> { | ||
| match zerocopy::FromBytes::ref_from_suffix(source) { | ||
| Ok((_rest, packet)) => Some(packet), | ||
| _ => None, | ||
| } | ||
| } |
Sorry, the diff of this file is not supported yet
| use zerocopy_derive::*; | ||
| #[path = "formats/coco.rs"] | ||
| mod format; | ||
| #[derive(IntoBytes, KnownLayout, Immutable)] | ||
| #[repr(C, align(2))] | ||
| struct MinimalViableSource { | ||
| header: [u8; 4], | ||
| trailer: [[u8; 2]], | ||
| } | ||
| #[unsafe(no_mangle)] | ||
| fn codegen_test(source: &MinimalViableSource) -> &format::LocoPacket { | ||
| zerocopy::transmute_ref!(source) | ||
| } |
Sorry, the diff of this file is not supported yet
| #[path = "formats/coco.rs"] | ||
| mod format; | ||
| #[unsafe(no_mangle)] | ||
| fn codegen_test(source: &[u8], count: usize) -> Option<&format::CocoPacket> { | ||
| zerocopy::TryFromBytes::try_ref_from_bytes_with_elems(source, count).ok() | ||
| } |
Sorry, the diff of this file is not supported yet
| #[path = "formats/coco.rs"] | ||
| mod format; | ||
| #[unsafe(no_mangle)] | ||
| fn codegen_test(source: &[u8]) -> Option<&format::CocoPacket> { | ||
| zerocopy::TryFromBytes::try_ref_from_bytes(source).ok() | ||
| } |
Sorry, the diff of this file is not supported yet
| #[path = "formats/coco.rs"] | ||
| mod format; | ||
| #[unsafe(no_mangle)] | ||
| fn codegen_test(source: &[u8], count: usize) -> Option<&format::CocoPacket> { | ||
| match zerocopy::TryFromBytes::try_ref_from_prefix_with_elems(source, count) { | ||
| Ok((packet, _rest)) => Some(packet), | ||
| _ => None, | ||
| } | ||
| } |
Sorry, the diff of this file is not supported yet
| #[path = "formats/coco.rs"] | ||
| mod format; | ||
| #[unsafe(no_mangle)] | ||
| fn codegen_test(source: &[u8]) -> Option<&format::CocoPacket> { | ||
| match zerocopy::TryFromBytes::try_ref_from_prefix(source) { | ||
| Ok((packet, _rest)) => Some(packet), | ||
| _ => None, | ||
| } | ||
| } |
Sorry, the diff of this file is not supported yet
| #[path = "formats/coco.rs"] | ||
| mod format; | ||
| #[unsafe(no_mangle)] | ||
| fn codegen_test(source: &[u8], count: usize) -> Option<&format::CocoPacket> { | ||
| match zerocopy::TryFromBytes::try_ref_from_suffix_with_elems(source, count) { | ||
| Ok((_rest, packet)) => Some(packet), | ||
| _ => None, | ||
| } | ||
| } |
Sorry, the diff of this file is not supported yet
| #[path = "formats/coco.rs"] | ||
| mod format; | ||
| #[unsafe(no_mangle)] | ||
| fn codegen_test(source: &[u8]) -> Option<&format::CocoPacket> { | ||
| match zerocopy::TryFromBytes::try_ref_from_suffix(source) { | ||
| Ok((_rest, packet)) => Some(packet), | ||
| _ => None, | ||
| } | ||
| } |
Sorry, the diff of this file is not supported yet
| use zerocopy_derive::*; | ||
| #[path = "formats/coco.rs"] | ||
| mod format; | ||
| #[derive(IntoBytes, KnownLayout, Immutable)] | ||
| #[repr(C, align(2))] | ||
| struct MinimalViableSource { | ||
| header: [u8; 4], | ||
| trailer: [[u8; 2]], | ||
| } | ||
| #[unsafe(no_mangle)] | ||
| fn codegen_test(source: &MinimalViableSource) -> Option<&format::CocoPacket> { | ||
| zerocopy::try_transmute_ref!(source).ok() | ||
| } |
Sorry, the diff of this file is not supported yet
| /* | ||
| Copyright 2026 The Fuchsia Authors | ||
| Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0 | ||
| <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT | ||
| license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. | ||
| This file may not be copied, modified, or distributed except according to | ||
| those terms. | ||
| */ | ||
| .codegen-tabs { | ||
| display: grid; | ||
| grid-template-columns: repeat(3, minmax(200px, 1fr)); | ||
| grid-template-rows: auto 1fr; | ||
| column-gap: 1rem; | ||
| } | ||
| .codegen-tabs:not(:has(details[open]))::after { | ||
| grid-column: 1/-1; | ||
| content: 'Click one of the above headers to expand its contents.'; | ||
| font-style: italic; | ||
| } | ||
| .codegen-tabs details { | ||
| display: grid; | ||
| grid-column: 1 / -1; | ||
| grid-row: 1 / span 2; | ||
| grid-template-columns: subgrid; | ||
| grid-template-rows: subgrid; | ||
| } | ||
| .codegen-tabs summary { | ||
| display: grid; | ||
| grid-column: var(--n) / span 1; | ||
| grid-row: 1; | ||
| z-index: 1; | ||
| border-bottom: 2px solid var(--headings-border-bottom-color); | ||
| cursor: pointer; | ||
| } | ||
| .codegen-tabs details[open] :is(summary) { | ||
| background-color: var(--code-block-background-color); | ||
| border-bottom-color: var(--target-border-color); | ||
| } | ||
| .codegen-tabs details::details-content { | ||
| grid-column: 1 / -1; | ||
| grid-row: 2; | ||
| } | ||
| .codegen-tabs details:not([open])::details-content { | ||
| display: none; | ||
| } |
| // Copyright 2026 The Fuchsia Authors | ||
| // | ||
| // Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0 | ||
| // <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT | ||
| // license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. | ||
| // This file may not be copied, modified, or distributed except according to | ||
| // those terms. | ||
| #![cfg(__ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS)] | ||
| use std::{path::PathBuf, process::Command}; | ||
| fn run_codegen_test(bench_name: &str, target_cpu: &str, bless: bool) { | ||
| let manifest_path = env!("CARGO_MANIFEST_PATH"); | ||
| let target_dir = env!("CARGO_TARGET_DIR"); | ||
| let output = Command::new("cargo") | ||
| .args([ | ||
| "asm", | ||
| "-p", | ||
| "zerocopy", | ||
| "--manifest-path", | ||
| manifest_path, | ||
| "--target-dir", | ||
| target_dir, | ||
| "--bench", | ||
| bench_name, | ||
| "--target-cpu", | ||
| target_cpu, | ||
| "--mca", | ||
| "codegen_test", | ||
| ]) | ||
| .output() | ||
| .expect("failed to execute process"); | ||
| let actual_result = output.stdout; | ||
| if !(output.status.success()) { | ||
| panic!("{}", String::from_utf8_lossy(&output.stderr)); | ||
| } | ||
| let expected_file_path = { | ||
| let mut path: PathBuf = env!("CARGO_MANIFEST_DIR").into(); | ||
| path.push("benches"); | ||
| let file_name = format!("{bench_name}.{target_cpu}.mca"); | ||
| path.push(file_name); | ||
| path | ||
| }; | ||
| if bless { | ||
| std::fs::write(expected_file_path, &actual_result).unwrap(); | ||
| } else { | ||
| let expected_result = std::fs::read(expected_file_path).unwrap_or_default(); | ||
| if actual_result != expected_result { | ||
| let expected = String::from_utf8_lossy(&expected_result[..]); | ||
| panic!("Bless codegen tests with BLESS=1\nGot unexpected output:\n{}", expected); | ||
| } | ||
| } | ||
| } | ||
| #[test] | ||
| #[cfg_attr(miri, ignore)] | ||
| fn codegen() { | ||
| let bless = std::env::var("BLESS").is_ok(); | ||
| let paths = std::fs::read_dir("benches").unwrap(); | ||
| for path in paths { | ||
| let path = path.unwrap().path(); | ||
| if !path.extension().map(|s| s == "rs").unwrap_or(false) { | ||
| continue; | ||
| } | ||
| let path = path.file_stem().unwrap().to_str().unwrap(); | ||
| run_codegen_test(path, "x86-64", bless); | ||
| } | ||
| } |
| { | ||
| "git": { | ||
| "sha1": "ff5ab2dac376774f6f647a8e4b98feb64eaf0833" | ||
| "sha1": "dbef2724b5106bab506dd3ce0e53b4ac97d05c61" | ||
| }, | ||
| "path_in_vcs": "" | ||
| } |
+3
-3
@@ -90,3 +90,3 @@ # This file is automatically @generated by Cargo. | ||
| name = "zerocopy" | ||
| version = "0.8.40" | ||
| version = "0.8.41-alpha" | ||
| dependencies = [ | ||
@@ -103,5 +103,5 @@ "elain", | ||
| name = "zerocopy-derive" | ||
| version = "0.8.40" | ||
| version = "0.8.41-alpha" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "f65c489a7071a749c849713807783f70672b28094011623e200cb86dcb835953" | ||
| checksum = "065608df236bc2b0b7df6efb2784fd66b72fbff6422ea98c0656534695d41151" | ||
| dependencies = [ | ||
@@ -108,0 +108,0 @@ "proc-macro2", |
+66
-4
@@ -16,3 +16,3 @@ # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO | ||
| name = "zerocopy" | ||
| version = "0.8.40" | ||
| version = "0.8.41-alpha" | ||
| authors = [ | ||
@@ -67,2 +67,4 @@ "Joshua Liebow-Feeser <joshlf@google.com>", | ||
| "--generate-link-to-definition", | ||
| "--extend-css", | ||
| "rustdoc/style.css", | ||
| ] | ||
@@ -92,2 +94,6 @@ | ||
| [[test]] | ||
| name = "codegen" | ||
| path = "tests/codegen.rs" | ||
| [[test]] | ||
| name = "include" | ||
@@ -100,4 +106,60 @@ path = "tests/include.rs" | ||
| [[bench]] | ||
| name = "ref_from_bytes" | ||
| path = "benches/ref_from_bytes.rs" | ||
| [[bench]] | ||
| name = "ref_from_bytes_with_elems" | ||
| path = "benches/ref_from_bytes_with_elems.rs" | ||
| [[bench]] | ||
| name = "ref_from_prefix" | ||
| path = "benches/ref_from_prefix.rs" | ||
| [[bench]] | ||
| name = "ref_from_prefix_with_elems" | ||
| path = "benches/ref_from_prefix_with_elems.rs" | ||
| [[bench]] | ||
| name = "ref_from_suffix" | ||
| path = "benches/ref_from_suffix.rs" | ||
| [[bench]] | ||
| name = "ref_from_suffix_with_elems" | ||
| path = "benches/ref_from_suffix_with_elems.rs" | ||
| [[bench]] | ||
| name = "transmute_ref" | ||
| path = "benches/transmute_ref.rs" | ||
| [[bench]] | ||
| name = "try_ref_from_bytes" | ||
| path = "benches/try_ref_from_bytes.rs" | ||
| [[bench]] | ||
| name = "try_ref_from_bytes_with_elems" | ||
| path = "benches/try_ref_from_bytes_with_elems.rs" | ||
| [[bench]] | ||
| name = "try_ref_from_prefix" | ||
| path = "benches/try_ref_from_prefix.rs" | ||
| [[bench]] | ||
| name = "try_ref_from_prefix_with_elems" | ||
| path = "benches/try_ref_from_prefix_with_elems.rs" | ||
| [[bench]] | ||
| name = "try_ref_from_suffix" | ||
| path = "benches/try_ref_from_suffix.rs" | ||
| [[bench]] | ||
| name = "try_ref_from_suffix_with_elems" | ||
| path = "benches/try_ref_from_suffix_with_elems.rs" | ||
| [[bench]] | ||
| name = "try_transmute_ref" | ||
| path = "benches/try_transmute_ref.rs" | ||
| [dependencies.zerocopy-derive] | ||
| version = "=0.8.40" | ||
| version = "=0.8.41-alpha" | ||
| optional = true | ||
@@ -123,5 +185,5 @@ | ||
| [dev-dependencies.zerocopy-derive] | ||
| version = "=0.8.40" | ||
| version = "=0.8.41-alpha" | ||
| [target."cfg(any())".dependencies.zerocopy-derive] | ||
| version = "=0.8.40" | ||
| version = "=0.8.41-alpha" |
+93
-0
@@ -838,1 +838,94 @@ // Copyright 2023 The Fuchsia Authors | ||
| pub(crate) const unsafe fn __unsafe() {} | ||
| /// Generate a rustdoc-style header with `$name` as the HTML ID for the 'Code | ||
| /// Generation' section of documentation. | ||
| #[allow(unused)] | ||
| macro_rules! codegen_header { | ||
| ($name:expr) => { | ||
| concat!( | ||
| " | ||
| <h5 id='method.", | ||
| $name, | ||
| ".codegen'> | ||
| <a class='doc-anchor' href='#method.", | ||
| $name, | ||
| ".codegen'>§</a> | ||
| Code Generation | ||
| </h5> | ||
| " | ||
| ) | ||
| }; | ||
| } | ||
| /// Generate the HTML for the tabulated portion of the 'Code Generation' | ||
| /// documentation. Consumes the name of the format file and benchmark. | ||
| #[allow(unused)] | ||
| macro_rules! codegen_tabs { | ||
| (format = $format:expr, bench = $name:expr) => { | ||
| concat!( | ||
| " | ||
| <div class='codegen-tabs'> | ||
| <details name='tab-", | ||
| $name, | ||
| "' style='--n: 1'> | ||
| <summary> | ||
| <h6>Format</h6> | ||
| </summary> | ||
| <div> | ||
| ```ignore | ||
| ", | ||
| include_str!(concat!("../benches/formats/", $format, ".rs")), | ||
| "``` | ||
| \ | ||
| </div> | ||
| </details> | ||
| <details name='tab-", | ||
| $name, | ||
| "' style='--n: 2'> | ||
| <summary> | ||
| <h6>Benchmark</h6> | ||
| </summary> | ||
| <div> | ||
| ```ignore | ||
| ", | ||
| include_str!(concat!("../benches/", $name, ".rs")), | ||
| "``` | ||
| \ | ||
| </div> | ||
| </details> | ||
| <details name='tab-", | ||
| $name, | ||
| "' style='--n: 3'> | ||
| <summary> | ||
| <h6>Machine Code Analysis</h6> | ||
| </summary> | ||
| <div> | ||
| ### Replication | ||
| You may replicate this analysis on your device with [`cargo-show-asm`] by running: | ||
| [`cargo-show-asm`]: https://github.com/pacak/cargo-show-asm | ||
| ```bash | ||
| cargo asm --bench ", | ||
| $name, | ||
| " codegen_test --mca | ||
| ``` | ||
| ### Results | ||
| ```plain | ||
| ", | ||
| include_str!(concat!("../benches/", $name, ".x86-64.mca")), | ||
| "``` | ||
| \ | ||
| </div> | ||
| </details> | ||
| </div> | ||
| " | ||
| ) | ||
| }; | ||
| } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display