refactor(nix-compat/store_path): make StorePath generic on S

Similar to how cl/12253 already did this for `Signature`, we apply the
same logic to `StorePath`.

`StorePathRef<'a>'` is now a `StorePath<&'a str>`, and there's less
redundant code for the two different implementation.

`.as_ref()` returns a `StorePathRef<'_>`, `.to_owned()` gives a
`StorePath<String>` (for now).

I briefly thought about only publicly exporting `StorePath<String>`
as `StorePath`, but the diff is not too large and this will make it
easier to gradually introduce more flexibility in which store paths to
accept.

Also, remove some silliness in `StorePath::from_absolute_path_full`,
which now doesn't allocate anymore.

Change-Id: Ife8843857a1a0a3a99177ca997649fd45b8198e6
Reviewed-on: https://cl.tvl.fyi/c/depot/+/12258
Autosubmit: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
Reviewed-by: Connor Brewster <cbrewster@hey.com>
This commit is contained in:
Florian Klink 2024-08-20 16:52:07 +03:00 committed by clbot
parent 413135b925
commit 2beabe968c
15 changed files with 301 additions and 315 deletions

View file

@ -6,7 +6,7 @@
use crate::aterm::escape_bytes;
use crate::derivation::{ca_kind_prefix, output::Output};
use crate::nixbase32;
use crate::store_path::{StorePath, StorePathRef, STORE_DIR_WITH_SLASH};
use crate::store_path::{StorePath, STORE_DIR_WITH_SLASH};
use bstr::BString;
use data_encoding::HEXLOWER;
@ -34,7 +34,10 @@ pub(crate) trait AtermWriteable {
fn aterm_write(&self, writer: &mut impl Write) -> std::io::Result<()>;
}
impl AtermWriteable for StorePathRef<'_> {
impl<S> AtermWriteable for StorePath<S>
where
S: std::cmp::Eq + std::ops::Deref<Target = str>,
{
fn aterm_write(&self, writer: &mut impl Write) -> std::io::Result<()> {
write_char(writer, QUOTE)?;
writer.write_all(STORE_DIR_WITH_SLASH.as_bytes())?;
@ -46,13 +49,6 @@ impl AtermWriteable for StorePathRef<'_> {
}
}
impl AtermWriteable for StorePath {
fn aterm_write(&self, writer: &mut impl Write) -> std::io::Result<()> {
let r: StorePathRef = self.into();
r.aterm_write(writer)
}
}
impl AtermWriteable for String {
fn aterm_write(&self, writer: &mut impl Write) -> std::io::Result<()> {
write_field(writer, self, true)
@ -179,7 +175,7 @@ pub(crate) fn write_input_derivations(
pub(crate) fn write_input_sources(
writer: &mut impl Write,
input_sources: &BTreeSet<StorePath>,
input_sources: &BTreeSet<StorePath<String>>,
) -> Result<(), io::Error> {
write_char(writer, BRACKET_OPEN)?;
write_array_elements(