From 0eb73977efa184711647849791ebdb4da2cc65a8 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Wed, 26 Nov 2025 22:11:43 -0500 Subject: [PATCH] Add zerocopy instance to raw bindings struct This will come in handy later, when we integrate harmonia. It is a very stable thing to do. --- Cargo.lock | 21 +++++++++++++++++++++ nix-bindings-store-sys/Cargo.toml | 1 + nix-bindings-store-sys/build.rs | 17 +++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 01fe2eb..f3d9634 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -316,6 +316,7 @@ dependencies = [ "bindgen", "nix-bindings-util-sys", "pkg-config", + "zerocopy", ] [[package]] @@ -611,3 +612,23 @@ name = "wit-bindgen" version = "0.46.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f17a85883d4e6d00e8a97c586de764dabcc06133f7f1d55dce5cdc070ad7fe59" + +[[package]] +name = "zerocopy" +version = "0.8.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ea879c944afe8a2b25fef16bb4ba234f47c694565e97383b36f3a878219065c" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf955aa904d6040f70dc8e9384444cb1030aed272ba3cb09bbc4ab9e7c1f34f5" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/nix-bindings-store-sys/Cargo.toml b/nix-bindings-store-sys/Cargo.toml index 3bc2bb5..f9f5ee2 100644 --- a/nix-bindings-store-sys/Cargo.toml +++ b/nix-bindings-store-sys/Cargo.toml @@ -13,6 +13,7 @@ path = "src/lib.rs" [dependencies] nix-bindings-util-sys = { path = "../nix-bindings-util-sys", version = "0.2.1" } +zerocopy = { version = "0.8", features = ["derive"] } [build-dependencies] bindgen = "0.69" diff --git a/nix-bindings-store-sys/build.rs b/nix-bindings-store-sys/build.rs index bafdfae..8702756 100644 --- a/nix-bindings-store-sys/build.rs +++ b/nix-bindings-store-sys/build.rs @@ -9,6 +9,22 @@ impl bindgen::callbacks::ParseCallbacks for StripNixPrefix { } } +#[derive(Debug)] +struct AddZerocopyDerives {} +impl bindgen::callbacks::ParseCallbacks for AddZerocopyDerives { + fn add_derives(&self, info: &bindgen::callbacks::DeriveInfo<'_>) -> Vec { + if info.name == "store_path_hash_part" { + vec![ + "zerocopy::FromBytes".to_string(), + "zerocopy::IntoBytes".to_string(), + "zerocopy::Immutable".to_string(), + ] + } else { + vec![] + } + } +} + fn main() { println!("cargo:rerun-if-changed=include/nix-c-store.h"); println!("cargo:rustc-link-lib=nixstorec"); @@ -29,6 +45,7 @@ fn main() { .clang_args(args) .parse_callbacks(Box::new(bindgen::CargoCallbacks::new())) .parse_callbacks(Box::new(StripNixPrefix)) + .parse_callbacks(Box::new(AddZerocopyDerives {})) // Blocklist symbols from nix-bindings-util-sys .blocklist_file(".*nix_api_util\\.h") .generate()