refactor(tvix/glue): clone less

Prepare the NixAttr to return without an intermediate
Vec<(String, NixString)>, and without into_iter(), and send off the
unmoved Derivation struct to known_paths without having to clone it.

Change-Id: Icdb9f78938e998a27d0313c5d9ab15b93af5821d
Reviewed-on: https://cl.tvl.fyi/c/depot/+/11531
Tested-by: BuildkiteCI
Autosubmit: flokli <flokli@flokli.de>
Reviewed-by: edef <edef@edef.eu>
This commit is contained in:
Florian Klink 2024-04-28 16:43:32 +03:00 committed by clbot
parent 26b77b2cf3
commit 98b85b4580

View file

@ -474,38 +474,36 @@ pub(crate) mod derivation_builtins {
.calculate_derivation_path(name) .calculate_derivation_path(name)
.map_err(DerivationError::InvalidDerivation)?; .map_err(DerivationError::InvalidDerivation)?;
// TODO: avoid cloning // Assemble the attrset to return from this builtin.
known_paths.add_derivation(drv_path.clone(), drv.clone()); let out = Value::Attrs(Box::new(NixAttrs::from_iter(
drv.outputs
let mut new_attrs: Vec<(String, NixString)> = drv .iter()
.outputs
.into_iter()
.map(|(name, output)| { .map(|(name, output)| {
( (
name.clone(), name.clone(),
NixString::new_context_from( NixString::new_context_from(
NixContextElement::Single { NixContextElement::Single {
name, name: name.clone(),
derivation: drv_path.to_absolute_path(), derivation: drv_path.to_absolute_path(),
} }
.into(), .into(),
output.path.unwrap().to_absolute_path(), output.path.as_ref().unwrap().to_absolute_path(),
), ),
) )
}) })
.collect(); .chain(std::iter::once((
"drvPath".to_owned(),
new_attrs.push((
"drvPath".to_string(),
NixString::new_context_from( NixString::new_context_from(
NixContextElement::Derivation(drv_path.to_absolute_path()).into(), NixContextElement::Derivation(drv_path.to_absolute_path()).into(),
drv_path.to_absolute_path(), drv_path.to_absolute_path(),
), ),
)); ))),
)));
Ok(Value::Attrs(Box::new(NixAttrs::from_iter( // Register the Derivation in known_paths.
new_attrs.into_iter(), known_paths.add_derivation(drv_path, drv);
))))
Ok(out)
} }
#[builtin("toFile")] #[builtin("toFile")]