这是我所能得到的,使用rental,部分基于How can I store a Chars iterator in the same struct as the String it iterateing? 。这里的区别在于,get_iter
锁定成员的方法必须采用可变的自引用。
我与使用租赁无关:我对使用reffers或owning_ref 的解决方案同样满意。
出现PhantomData
在这里只是为了与被迭代的事物MyIter
具有正常的生命周期关系。MyIterable
我还尝试更改#[rental]
为并更改to#[rental(deref_mut_suffix)]
的返回类型,但这给了我源自宏的其他终身错误,我无法破译。MyIterable.get_iter
Box<Iterator<Item=i32> + 'a>
#[macro_use]
extern crate rental;
use std::marker::PhantomData;
pub struct MyIterable {}
impl MyIterable {
// In the real use-case I can't remove the 'mut'.
pub fn get_iter<'a>(&'a mut self) -> MyIter<'a> {
MyIter {
marker: PhantomData,
}
} …
Run Code Online (Sandbox Code Playgroud)