Skip to content

Commit 6b3a60f

Browse files
committed
runtime: Implement Value::from_iter() utility
1 parent 67a1875 commit 6b3a60f

File tree

4 files changed

+15
-17
lines changed

4 files changed

+15
-17
lines changed

src/macros.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,6 @@ macro_rules! impl_simple_to_value {
8686
}
8787
};
8888
}
89-
macro_rules! impl_simple_iter_to_value {
90-
($src:ty, $var:tt, $expr:expr) => {
91-
impl_simple_to_value!($src, $var, Array($expr.map(Into::into).collect()));
92-
};
93-
}
94-
9589
macro_rules! add_tags {
9690
($struct:ident, $tags:tt, $($field:ident),+) => {
9791
$tags.extend([$(

src/runtime/value.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,10 @@ impl Value {
384384
pub fn function(func: impl Into<Function>) -> Self {
385385
Value::Function(func.into())
386386
}
387+
388+
pub fn from_iter<T: Into<Value>>(iter: impl IntoIterator<Item = T>) -> Value {
389+
Array(iter.into_iter().map(Into::into).collect()).into()
390+
}
387391
}
388392

389393
// Parse & evaluate the string code in the default global scope to produce a Value

src/stdlib/btc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ impl_simple_to_value!(Sequence, seq, seq.to_consensus_u32());
475475
impl_simple_to_value!(AbsLockTime, time, time.to_consensus_u32());
476476
impl_simple_to_value!(OutPoint, outpoint, (outpoint.txid, outpoint.vout));
477477
impl_simple_to_value!(SignedAmount, amt, amt.to_sat());
478-
impl_simple_iter_to_value!(Witness, wit, wit.to_vec().into_iter());
478+
impl_simple_to_value!(Witness, wit, wit.to_vec());
479479
impl_simple_to_value!(
480480
bitcoin::transaction::TxOut,
481481
txout,

src/stdlib/keys.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,12 @@ pub mod fns {
143143
///
144144
/// singles(PubKey<Multi>|SecKey<Multi>|Descriptor<Multi>) -> Array<PubKey|SecKey|Descriptor>
145145
pub fn singles(args: Array, _: &ScopeRef) -> Result<Value> {
146-
Ok(Value::array(match args.arg_into()? {
147-
Value::PubKey(pk) => pk.into_single_keys().into_iter().map(Into::into).collect(),
148-
Value::SecKey(sk) => sk.into_single_keys().into_iter().map(Into::into).collect(),
149-
Value::Descriptor(desc) => desc
150-
.into_single_descriptors()?
151-
.into_iter()
152-
.map(Into::into)
153-
.collect(),
146+
Ok(match args.arg_into()? {
147+
Value::PubKey(pk) => Value::from_iter(pk.into_single_keys()),
148+
Value::SecKey(sk) => Value::from_iter(sk.into_single_keys()),
149+
Value::Descriptor(desc) => Value::from_iter(desc.into_single_descriptors()?),
154150
other => bail!(Error::InvalidValue(other.into())),
155-
}))
151+
})
156152
}
157153
}
158154

@@ -205,7 +201,11 @@ impl_simple_to_value!(
205201
);
206202
impl_simple_to_value!(bip32::Fingerprint, fp, fp.to_bytes().to_vec());
207203
impl_simple_to_value!(bip32::ChildNumber, cn, u32::from(cn));
208-
impl_simple_iter_to_value!(bip32::DerivationPath, path, path.into_iter().copied());
204+
impl_simple_to_value!(
205+
bip32::DerivationPath,
206+
path,
207+
Value::from_iter(path.into_iter().copied())
208+
);
209209
impl_simple_to_value!(
210210
secp256k1::PublicKey,
211211
key,

0 commit comments

Comments
 (0)