From ca6eb8608e826640957540f3984fe3fa5c3008c4 Mon Sep 17 00:00:00 2001 From: Tiziano Santoro Date: Fri, 6 Mar 2020 12:39:30 +0000 Subject: [PATCH] Automatically derive Eq and Hash for Label (#676) Ref #630 --- oak/server/rust/oak_abi/build.rs | 5 ++++- oak/server/rust/oak_abi/src/label.rs | 15 --------------- 2 files changed, 4 insertions(+), 16 deletions(-) diff --git a/oak/server/rust/oak_abi/build.rs b/oak/server/rust/oak_abi/build.rs index 535f25fa153..a47bb966a77 100644 --- a/oak/server/rust/oak_abi/build.rs +++ b/oak/server/rust/oak_abi/build.rs @@ -25,5 +25,8 @@ fn main() { // https://doc.rust-lang.org/cargo/reference/build-scripts.html#rerun-if-changed println!("cargo:rerun-if-changed={}", oak_api_path.to_str().unwrap()); - prost_build::compile_protos(&[oak_api_path, policy_path], &[proto_dir]).unwrap(); + prost_build::Config::new() + .type_attribute(".oak.policy", "#[derive(Eq,Hash)]") + .compile_protos(&[oak_api_path, policy_path], &[proto_dir]) + .unwrap(); } diff --git a/oak/server/rust/oak_abi/src/label.rs b/oak/server/rust/oak_abi/src/label.rs index af2abb5be02..dd0f33411dc 100644 --- a/oak/server/rust/oak_abi/src/label.rs +++ b/oak/server/rust/oak_abi/src/label.rs @@ -40,21 +40,6 @@ pub trait Label: Sized { fn can_flow_to(&self, other: &Self) -> bool; } -// Implement the necessary traits on `Tag` so that it may be used in `HashSet`. - -impl Eq for crate::proto::policy::Tag {} - -#[allow(clippy::derive_hash_xor_eq)] -impl std::hash::Hash for crate::proto::policy::Tag { - fn hash(&self, state: &mut H) { - // As a quick solution, we simply serialize the message and hash the resulting `Vec`. - let mut bytes = Vec::new(); - self.encode(&mut bytes) - .expect("could not serialize to bytes"); - bytes.hash(state); - } -} - impl Label for crate::proto::policy::Label { fn serialize(&self) -> Vec { let mut bytes = Vec::new();