docs(tvix/nix-compat): rename as_narinfo() to to_narinfo()

This actually does a bit of allocation. There's two Vecs, one for
references and one for signatures.

We can get rid of the Vec at nar_hash.

Change-Id: Ie025309b6678f83f5b961d49ff75dcfc7da145a1
Reviewed-on: https://cl.tvl.fyi/c/depot/+/11395
Reviewed-by: raitobezarius <tvl@lahfa.xyz>
Tested-by: BuildkiteCI
This commit is contained in:
Florian Klink 2024-04-12 13:25:33 +03:00 committed by flokli
parent 3d7f80c0e3
commit 329c4a1ace
2 changed files with 13 additions and 5 deletions

View file

@ -173,12 +173,16 @@ impl PathInfo {
Ok(root_nix_path) Ok(root_nix_path)
} }
/// With self and a given StorePathRef, this reconstructs a /// With self and its store path name, this reconstructs a
/// [nix_compat::narinfo::NarInfo<'_>]. /// [nix_compat::narinfo::NarInfo<'_>].
/// It can be used to validate Signatures, or get back a (sparse) NarInfo /// It can be used to validate Signatures, or get back a (sparse) NarInfo
/// struct to prepare writing it out. /// struct to prepare writing it out.
/// ///
/// This doesn't allocate any new data. /// It assumes self to be validated first, and will only return None if the
/// `narinfo` field is unpopulated.
///
/// It does very little allocation (a Vec each for `signatures` and
/// `references`), the rest points to data owned elsewhere.
/// ///
/// Keep in mind this is not able to reconstruct all data present in the /// Keep in mind this is not able to reconstruct all data present in the
/// NarInfo<'_>, as some of it is not stored at all: /// NarInfo<'_>, as some of it is not stored at all:
@ -188,7 +192,7 @@ impl PathInfo {
/// ///
/// If you want to render it out to a string and be able to parse it back /// If you want to render it out to a string and be able to parse it back
/// in, at least URL *must* be set again. /// in, at least URL *must* be set again.
pub fn as_narinfo<'a>( pub fn to_narinfo<'a>(
&'a self, &'a self,
store_path: store_path::StorePathRef<'a>, store_path: store_path::StorePathRef<'a>,
) -> Option<nix_compat::narinfo::NarInfo<'_>> { ) -> Option<nix_compat::narinfo::NarInfo<'_>> {
@ -197,7 +201,11 @@ impl PathInfo {
Some(nix_compat::narinfo::NarInfo { Some(nix_compat::narinfo::NarInfo {
flags: Flags::empty(), flags: Flags::empty(),
store_path, store_path,
nar_hash: narinfo.nar_sha256.to_vec().try_into().unwrap(), nar_hash: narinfo
.nar_sha256
.as_ref()
.try_into()
.expect("invalid narhash"),
nar_size: narinfo.nar_size, nar_size: narinfo.nar_size,
references: narinfo references: narinfo
.reference_names .reference_names

View file

@ -424,7 +424,7 @@ CA: fixed:sha256:086vqwk2wl8zfs47sq2xpjc9k066ilmb8z6dn0q6ymwjzlm196cd"#
let path_info: PathInfo = (&narinfo_parsed).into(); let path_info: PathInfo = (&narinfo_parsed).into();
let mut narinfo_returned = path_info let mut narinfo_returned = path_info
.as_narinfo( .to_narinfo(
StorePathRef::from_bytes(b"pa10z4ngm0g83kx9mssrqzz30s84vq7k-hello-2.12.1.tar.gz") StorePathRef::from_bytes(b"pa10z4ngm0g83kx9mssrqzz30s84vq7k-hello-2.12.1.tar.gz")
.expect("invalid storepath"), .expect("invalid storepath"),
) )