How can I convert the following tail-recursive function to an iterative function?
fn main() {}
fn test<'a, K: Bound, V>(b: &K, x: &'a mut BoundNode<K, V>) -> &'a mut BoundNode<K, V> {
match *x {
BoundNode::Branch { ref mut left, ref mut right, .. } => test(
b,
if left.bound().cost(b) <= right.bound().cost(b) {
left
} else {
right
},
),
BoundNode::Leaf { .. } => x,
}
}
#[derive(Clone, Debug)]
enum BoundNode<K, V> {
Leaf {
bound: K,
node: V,
},
Branch {
bound: K,
left: Box<BoundNode<K, V>>,
right: Box<BoundNode<K, V>>,
},
}
impl<K, V> BoundNode<K, V> {
fn bound(&self) -> &K {
match self {
BoundNode::Leaf { bound, .. } => bound,
BoundNode::Branch { bound, .. } => bound,
}
}
}
pub trait Bound {
type Distance: PartialOrd;
fn join(&self, &Self) -> Self;
fn cost(&self, &Self) -> Self::Distance;
}