我有一个enum
上的迭代器,它的一个变体中有一个可变引用。现在我想将此引用移出self
并返回它。为了避免同时对同一个对象进行两次可变引用,我想将self
的枚举变体更改为没有引用的变体。下面是一个代码示例:
enum Test<'a> {
A(&'a mut usize),
B,
}
impl<'a> Iterator for Test<'a> {
type Item = &'a mut usize;
fn next(&mut self) -> Option<Self::Item> {
match *self {
Test::A(r) => Some(r), // TODO: return mutable reference and change self to B
Test::B => None,
}
}
}
fn main() {
let mut v = 1;
let mut it = Test::A(&mut v);
it.next();
}
问题与Change selector in match when selector is a mutable reference有关,但该解决方案在此处不起作用。
答案 0 :(得分:3)
我会使用swap
:
fn next(&mut self) -> Option<Self::Item> {
if let Test::A(_) = *self {
let mut to_swap = Test::B;
std::mem::swap(self, &mut to_swap);
match to_swap {
Test::A(r) => Some(r),
_ => unreachable!(), // never reached
}
} else {
None
}
}
您可以使用以下辅助函数:
impl<'a> Iterator for Test<'a> {
type Item = &'a mut usize;
fn next(&mut self) -> Option<Self::Item> {
if let Test::A(_) = *self {
let mut to_swap = Test::B;
std::mem::swap(self, &mut to_swap);
to_swap.consume_as_a()
} else {
None
}
}
}
impl<'a> Test<'a> {
fn consume_as_a(self) -> Option<&'a mut usize> {
match self {
Test::A(r) => Some(r),
_ => None,
}
}
}