diff options
author | Florian Klink <flokli@flokli.de> | 2024-10-15T23·51+0300 |
---|---|---|
committer | flokli <flokli@flokli.de> | 2024-11-12T11·55+0000 |
commit | 1428ea4e191aa897a0a23e2c90f997f0b65aae6d (patch) | |
tree | 6a093f91b2deff805bf08715edced90d490716d4 /tvix/nix-compat/src/store_path | |
parent | 14744713276870cf0380e31bee01124e1a2f4cb0 (diff) |
refactor(nix-compat/store_path): use AsRef<str> r/8913
Implement PartialEq/Eq ourselves instead of deriving, by proxying to name.as_ref() (and digest of course). Also implement Hash on our own, clippy doesn't like this to be derived, while Eq/PartialEq is not. Change-Id: Idbe289a23ba3bc8dabf893d4d8752792ae2778c3 Reviewed-on: https://cl.tvl.fyi/c/depot/+/12744 Tested-by: BuildkiteCI Reviewed-by: edef <edef@edef.eu> Autosubmit: flokli <flokli@flokli.de>
Diffstat (limited to 'tvix/nix-compat/src/store_path')
-rw-r--r-- | tvix/nix-compat/src/store_path/mod.rs | 60 | ||||
-rw-r--r-- | tvix/nix-compat/src/store_path/utils.rs | 8 |
2 files changed, 44 insertions, 24 deletions
diff --git a/tvix/nix-compat/src/store_path/mod.rs b/tvix/nix-compat/src/store_path/mod.rs index 56c10dc41417..13265048641b 100644 --- a/tvix/nix-compat/src/store_path/mod.rs +++ b/tvix/nix-compat/src/store_path/mod.rs @@ -2,8 +2,7 @@ use crate::nixbase32; use data_encoding::{DecodeError, BASE64}; use serde::{Deserialize, Serialize}; use std::{ - fmt::{self, Display}, - ops::Deref, + fmt, path::Path, str::{self, FromStr}, }; @@ -51,21 +50,40 @@ pub enum Error { /// /// A [StorePath] does not encode any additional subpath "inside" the store /// path. -#[derive(Clone, Debug, PartialEq, Eq, Hash)] -pub struct StorePath<S> -where - S: std::cmp::Eq + std::cmp::PartialEq, -{ +#[derive(Clone, Debug)] +pub struct StorePath<S> { digest: [u8; DIGEST_SIZE], name: S, } + +impl<S> PartialEq for StorePath<S> +where + S: AsRef<str>, +{ + fn eq(&self, other: &Self) -> bool { + self.digest() == other.digest() && self.name().as_ref() == other.name().as_ref() + } +} + +impl<S> Eq for StorePath<S> where S: AsRef<str> {} + +impl<S> std::hash::Hash for StorePath<S> +where + S: AsRef<str>, +{ + fn hash<H: std::hash::Hasher>(&self, state: &mut H) { + state.write(&self.digest); + state.write(self.name.as_ref().as_bytes()); + } +} + /// Like [StorePath], but without a heap allocation for the name. /// Used by [StorePath] for parsing. pub type StorePathRef<'a> = StorePath<&'a str>; impl<S> StorePath<S> where - S: std::cmp::Eq + Deref<Target = str>, + S: AsRef<str>, { pub fn digest(&self) -> &[u8; DIGEST_SIZE] { &self.digest @@ -78,14 +96,14 @@ where pub fn as_ref(&self) -> StorePathRef<'_> { StorePathRef { digest: self.digest, - name: &self.name, + name: self.name.as_ref(), } } pub fn to_owned(&self) -> StorePath<String> { StorePath { digest: self.digest, - name: self.name.to_string(), + name: self.name.as_ref().to_string(), } } @@ -183,17 +201,14 @@ where /// Returns an absolute store path string. /// That is just the string representation, prefixed with the store prefix /// ([STORE_DIR_WITH_SLASH]), - pub fn to_absolute_path(&self) -> String - where - S: Display, - { + pub fn to_absolute_path(&self) -> String { format!("{}{}", STORE_DIR_WITH_SLASH, self) } } impl<S> PartialOrd for StorePath<S> where - S: std::cmp::PartialEq + std::cmp::Eq, + S: AsRef<str>, { fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { Some(self.cmp(other)) @@ -204,7 +219,7 @@ where /// of the nixbase32-encoded string. impl<S> Ord for StorePath<S> where - S: std::cmp::PartialEq + std::cmp::Eq, + S: AsRef<str>, { fn cmp(&self, other: &Self) -> std::cmp::Ordering { self.digest.iter().rev().cmp(other.digest.iter().rev()) @@ -223,7 +238,7 @@ impl<'a, 'b: 'a> FromStr for StorePath<String> { impl<'a, 'de: 'a, S> Deserialize<'de> for StorePath<S> where - S: std::cmp::Eq + Deref<Target = str> + From<&'a str>, + S: AsRef<str> + From<&'a str>, { fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where @@ -245,7 +260,7 @@ where impl<S> Serialize for StorePath<S> where - S: std::cmp::Eq + Deref<Target = str> + Display, + S: AsRef<str>, { fn serialize<SR>(&self, serializer: SR) -> Result<SR::Ok, SR::Error> where @@ -305,13 +320,18 @@ pub(crate) fn validate_name(s: &(impl AsRef<[u8]> + ?Sized)) -> Result<&str, Err impl<S> fmt::Display for StorePath<S> where - S: fmt::Display + std::cmp::Eq, + S: AsRef<str>, { /// The string representation of a store path starts with a digest (20 /// bytes), [crate::nixbase32]-encoded, followed by a `-`, /// and ends with the name. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}-{}", nixbase32::encode(&self.digest), self.name) + write!( + f, + "{}-{}", + nixbase32::encode(&self.digest), + self.name.as_ref() + ) } } diff --git a/tvix/nix-compat/src/store_path/utils.rs b/tvix/nix-compat/src/store_path/utils.rs index a0dc21160e7a..63b6969464d8 100644 --- a/tvix/nix-compat/src/store_path/utils.rs +++ b/tvix/nix-compat/src/store_path/utils.rs @@ -50,7 +50,7 @@ pub fn build_text_path<'a, S, SP, I, C>( ) -> Result<StorePath<SP>, BuildStorePathError> where S: AsRef<str>, - SP: std::cmp::Eq + std::ops::Deref<Target = str> + std::convert::From<&'a str>, + SP: AsRef<str> + std::convert::From<&'a str>, I: IntoIterator<Item = S>, C: AsRef<[u8]>, { @@ -69,7 +69,7 @@ pub fn build_ca_path<'a, S, SP, I>( ) -> Result<StorePath<SP>, BuildStorePathError> where S: AsRef<str>, - SP: std::cmp::Eq + std::ops::Deref<Target = str> + std::convert::From<&'a str>, + SP: AsRef<str> + std::convert::From<&'a str>, I: IntoIterator<Item = S>, { // self references are only allowed for CAHash::Nar(NixHash::Sha256(_)). @@ -129,7 +129,7 @@ pub fn build_output_path<'a, SP>( output_path_name: &'a str, ) -> Result<StorePath<SP>, Error> where - SP: std::cmp::Eq + std::ops::Deref<Target = str> + std::convert::From<&'a str>, + SP: AsRef<str> + std::convert::From<&'a str>, { build_store_path_from_fingerprint_parts( &(String::from("output:") + output_name), @@ -154,7 +154,7 @@ fn build_store_path_from_fingerprint_parts<'a, SP>( name: &'a str, ) -> Result<StorePath<SP>, Error> where - SP: std::cmp::Eq + std::ops::Deref<Target = str> + std::convert::From<&'a str>, + SP: AsRef<str> + std::convert::From<&'a str>, { let fingerprint = format!( "{ty}:sha256:{}:{STORE_DIR}:{name}", |