From 91deb67de92c4deaf24ec16c690c4887c796ccff Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Thu, 21 Dec 2023 16:00:10 +0200 Subject: refactor(nix-compat/store_path): use StorePathRef::to_absolute_path Keep the method around in StorePath for convenience, but move the implementation to StorePathRef. Change-Id: Ie1844fa01ce6529dc1a58907563c95c3112c831d Reviewed-on: https://cl.tvl.fyi/c/depot/+/10387 Autosubmit: flokli Tested-by: BuildkiteCI Reviewed-by: edef --- tvix/nix-compat/src/store_path/mod.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'tvix/nix-compat/src/store_path/mod.rs') diff --git a/tvix/nix-compat/src/store_path/mod.rs b/tvix/nix-compat/src/store_path/mod.rs index fa172d1031..e23002bfb9 100644 --- a/tvix/nix-compat/src/store_path/mod.rs +++ b/tvix/nix-compat/src/store_path/mod.rs @@ -126,18 +126,18 @@ impl StorePath { } } - /// Converts the [StorePath] to an absolute store path string. + /// 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 { - format!("{}{}", STORE_DIR_WITH_SLASH, self) + let sp_ref: StorePathRef = self.into(); + sp_ref.to_absolute_path() } } /// Like [StorePath], but without a heap allocation for the name. /// Used by [StorePath] for parsing. /// -/// TODO(edef): migrate most methods here #[derive(Debug, Eq, PartialEq)] pub struct StorePathRef<'a> { digest: [u8; DIGEST_SIZE], @@ -211,6 +211,13 @@ impl<'a> StorePathRef<'a> { name: validate_name(&s[ENCODED_DIGEST_SIZE + 1..])?, }) } + + /// 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 { + format!("{}{}", STORE_DIR_WITH_SLASH, self) + } } /// NAME_CHARS contains `true` for bytes that are valid in store path names, @@ -358,13 +365,12 @@ mod tests { let example_nix_path_str = "00bgd045z0d4icpbc2yyz4gx48ak44la-net-tools-1.60_p20170221182432"; let nixpath_expected = - StorePath::from_bytes(example_nix_path_str.as_bytes()).expect("must parse"); + StorePathRef::from_bytes(example_nix_path_str.as_bytes()).expect("must parse"); let nixpath_actual = StorePathRef::from_absolute_path( "/nix/store/00bgd045z0d4icpbc2yyz4gx48ak44la-net-tools-1.60_p20170221182432".as_bytes(), ) - .expect("must parse") - .to_owned(); + .expect("must parse"); assert_eq!(nixpath_expected, nixpath_actual); -- cgit 1.4.1