Skip to content

Commit 8d3719f

Browse files
committed
Make type interface more generic
Accept any type param which has the trait into_iter(). The includes Vec or anything that can iterate over &UTXOs.
1 parent b7da752 commit 8d3719f

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

src/branch_and_bound.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,13 @@ use crate::WeightedUtxo;
146146
//
147147
// If either 1 or 2 is true, we consider the current search path no longer viable to continue. In
148148
// such a case, backtrack to start a new search path.
149-
pub fn select_coins_bnb<Utxo: WeightedUtxo>(
149+
pub fn select_coins_bnb<'a, Utxo: WeightedUtxo, I: IntoIterator<Item = &'a Utxo>>(
150150
target: Amount,
151151
cost_of_change: Amount,
152152
fee_rate: FeeRate,
153153
long_term_fee_rate: FeeRate,
154-
weighted_utxos: &[Utxo],
155-
) -> Option<(u32, Vec<&Utxo>)> {
154+
weighted_utxos: I,
155+
) -> Option<(u32, Vec<&'a Utxo>)> {
156156
// Total_Tries in Core:
157157
// https://github.com/bitcoin/bitcoin/blob/1d9da8da309d1dbf9aef15eb8dc43b4a2dc3d309/src/wallet/coinselection.cpp#L74
158158
const ITERATION_LIMIT: u32 = 100_000;
@@ -173,7 +173,7 @@ pub fn select_coins_bnb<Utxo: WeightedUtxo>(
173173

174174
// Creates a tuple of (effective_value, waste, weighted_utxo)
175175
let mut w_utxos: Vec<(Amount, SignedAmount, &Utxo)> = weighted_utxos
176-
.iter()
176+
.into_iter()
177177
// calculate effective_value and waste for each w_utxo.
178178
.map(|wu| (wu.effective_value(fee_rate), wu.waste(fee_rate, long_term_fee_rate), wu))
179179
// remove utxos that either had an error in the effective_value or waste calculation.

src/lib.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -108,20 +108,20 @@ pub trait WeightedUtxo {
108108
/// - UTXO space was searched successfully however no match was found
109109
#[cfg(feature = "rand")]
110110
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
111-
pub fn select_coins<Utxo: WeightedUtxo>(
111+
pub fn select_coins<'a, Utxo: WeightedUtxo, I: IntoIterator<Item = &'a Utxo>>(
112112
target: Amount,
113113
cost_of_change: Amount,
114114
fee_rate: FeeRate,
115115
long_term_fee_rate: FeeRate,
116-
weighted_utxos: &[Utxo],
117-
) -> Option<(u32, Vec<&Utxo>)> {
118-
let bnb =
119-
select_coins_bnb(target, cost_of_change, fee_rate, long_term_fee_rate, weighted_utxos);
116+
weighted_utxos: I,
117+
) -> Option<(u32, Vec<&'a Utxo>)> {
118+
let v: Vec<&Utxo> = weighted_utxos.into_iter().collect();
119+
let result = select_coins_bnb(target, cost_of_change, fee_rate, long_term_fee_rate, v.clone());
120120

121-
if bnb.is_some() {
122-
bnb
121+
if result.is_some() {
122+
result
123123
} else {
124-
select_coins_srd(target, fee_rate, weighted_utxos, &mut thread_rng())
124+
select_coins_srd(target, fee_rate, v, &mut thread_rng())
125125
}
126126
}
127127

src/single_random_draw.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@ use crate::{WeightedUtxo, CHANGE_LOWER};
3333
/// - Not enough potential amount to meet the target
3434
/// - Target Amount is zero (no match possible)
3535
/// - Search was successful yet no match found
36-
pub fn select_coins_srd<'a, R: rand::Rng + ?Sized, Utxo: WeightedUtxo>(
36+
pub fn select_coins_srd<'a, R: rand::Rng + ?Sized, Utxo: WeightedUtxo, I: IntoIterator<Item = &'a Utxo>>(
3737
target: Amount,
3838
fee_rate: FeeRate,
39-
weighted_utxos: &'a [Utxo],
39+
weighted_utxos: I,
4040
rng: &mut R,
4141
) -> Option<(u32, Vec<&'a Utxo>)> {
4242
if target > Amount::MAX_MONEY {
4343
return None;
4444
}
4545

46-
let mut result: Vec<_> = weighted_utxos.iter().collect();
46+
let mut result: Vec<&Utxo> = weighted_utxos.into_iter().collect();
4747
let mut origin = result.to_owned();
4848
origin.shuffle(rng);
4949

0 commit comments

Comments
 (0)