Skip to content

Commit

Permalink
runtime: Implement Value::from_iter() utility
Browse files Browse the repository at this point in the history
  • Loading branch information
shesek committed Oct 25, 2024
1 parent 67a1875 commit 6b3a60f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 17 deletions.
6 changes: 0 additions & 6 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,6 @@ macro_rules! impl_simple_to_value {
}
};
}
macro_rules! impl_simple_iter_to_value {
($src:ty, $var:tt, $expr:expr) => {
impl_simple_to_value!($src, $var, Array($expr.map(Into::into).collect()));
};
}

macro_rules! add_tags {
($struct:ident, $tags:tt, $($field:ident),+) => {
$tags.extend([$(
Expand Down
4 changes: 4 additions & 0 deletions src/runtime/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,10 @@ impl Value {
pub fn function(func: impl Into<Function>) -> Self {
Value::Function(func.into())
}

pub fn from_iter<T: Into<Value>>(iter: impl IntoIterator<Item = T>) -> Value {
Array(iter.into_iter().map(Into::into).collect()).into()
}
}

// Parse & evaluate the string code in the default global scope to produce a Value
Expand Down
2 changes: 1 addition & 1 deletion src/stdlib/btc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ impl_simple_to_value!(Sequence, seq, seq.to_consensus_u32());
impl_simple_to_value!(AbsLockTime, time, time.to_consensus_u32());
impl_simple_to_value!(OutPoint, outpoint, (outpoint.txid, outpoint.vout));
impl_simple_to_value!(SignedAmount, amt, amt.to_sat());
impl_simple_iter_to_value!(Witness, wit, wit.to_vec().into_iter());
impl_simple_to_value!(Witness, wit, wit.to_vec());
impl_simple_to_value!(
bitcoin::transaction::TxOut,
txout,
Expand Down
20 changes: 10 additions & 10 deletions src/stdlib/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,12 @@ pub mod fns {
///
/// singles(PubKey<Multi>|SecKey<Multi>|Descriptor<Multi>) -> Array<PubKey|SecKey|Descriptor>
pub fn singles(args: Array, _: &ScopeRef) -> Result<Value> {
Ok(Value::array(match args.arg_into()? {
Value::PubKey(pk) => pk.into_single_keys().into_iter().map(Into::into).collect(),
Value::SecKey(sk) => sk.into_single_keys().into_iter().map(Into::into).collect(),
Value::Descriptor(desc) => desc
.into_single_descriptors()?
.into_iter()
.map(Into::into)
.collect(),
Ok(match args.arg_into()? {
Value::PubKey(pk) => Value::from_iter(pk.into_single_keys()),
Value::SecKey(sk) => Value::from_iter(sk.into_single_keys()),
Value::Descriptor(desc) => Value::from_iter(desc.into_single_descriptors()?),
other => bail!(Error::InvalidValue(other.into())),
}))
})
}
}

Expand Down Expand Up @@ -205,7 +201,11 @@ impl_simple_to_value!(
);
impl_simple_to_value!(bip32::Fingerprint, fp, fp.to_bytes().to_vec());
impl_simple_to_value!(bip32::ChildNumber, cn, u32::from(cn));
impl_simple_iter_to_value!(bip32::DerivationPath, path, path.into_iter().copied());
impl_simple_to_value!(
bip32::DerivationPath,
path,
Value::from_iter(path.into_iter().copied())
);
impl_simple_to_value!(
secp256k1::PublicKey,
key,
Expand Down

0 comments on commit 6b3a60f

Please sign in to comment.