如何在Rust中的锁定结构成员上返回迭代器?

时间:2018-08-03 01:20:20

标签: iterator rust mutex

据我所知,使用rental部分基于How can I store a Chars iterator in the same struct as the String it is iterating on?。此处的区别在于,锁定成员的get_iter方法必须采用可变的自引用。

我不依赖使用租金:我对使用reffersowning_ref的解决方案感到满意。

这里出现PhantomData只是为了使MyIterMyIterable保持正常的生命周期关系,而事情要进行迭代。

我还尝试将#[rental]更改为#[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,
        }
    }
}

pub struct MyIter<'a> {
    marker: PhantomData<&'a MyIterable>,
}

impl<'a> Iterator for MyIter<'a> {
    type Item = i32;
    fn next(&mut self) -> Option<i32> {
        Some(42)
    }
}

use std::sync::Mutex;

rental! {
    mod locking_iter {
        pub use super::{MyIterable, MyIter};
        use std::sync::MutexGuard;

        #[rental]
        pub struct LockingIter<'a> {
            guard: MutexGuard<'a, MyIterable>,
            iter: MyIter<'guard>,
        }
    }
}

use locking_iter::LockingIter;

impl<'a> Iterator for LockingIter<'a> {
    type Item = i32;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        self.rent_mut(|iter| iter.next())
    }
}

struct Access {
    shared: Mutex<MyIterable>,
}

impl Access {
    pub fn get_iter<'a>(&'a self) -> Box<Iterator<Item = i32> + 'a> {
        Box::new(LockingIter::new(self.shared.lock().unwrap(), |mi| {
            mi.get_iter()
        }))
    }
}

fn main() {
    let access = Access {
        shared: Mutex::new(MyIterable {}),
    };
    let iter = access.get_iter();
    let contents: Vec<i32> = iter.take(2).collect();
    println!("contents: {:?}", contents);
}

1 个答案:

答案 0 :(得分:4)

正如用户rodrigo在评论中指出的那样,解决方案就是将#[rental]更改为#[rental_mut]