pub trait RMakeIter<T, I>where
I: RIterator<Item = T>,{
// Required method
fn make_iter(&self) -> I;
}Expand description
Adapter trait for creating iterator wrappers from collections.
This trait provides a way to create an RIterator wrapper from a collection.
Since ExternalPtr methods receive &self, this trait clones the underlying
data to create an independent iterator.
§Type Parameters
T: The element type yielded by the iteratorI: The iterator type returned (must implementRIterator)
§Design Note
The returned iterator is independent from the source collection. Modifications
to the original collection after calling make_iter() won’t affect the
iterator’s output.
§Example
ⓘ
use std::cell::RefCell;
#[derive(ExternalPtr)]
struct MyVec(Vec<i32>);
#[derive(ExternalPtr)]
struct MyVecIter(RefCell<std::vec::IntoIter<i32>>);
impl RIterator for MyVecIter {
type Item = i32;
fn next(&self) -> Option<i32> {
self.0.borrow_mut().next()
}
fn size_hint(&self) -> (i64, Option<i64>) {
let (lo, hi) = self.0.borrow().size_hint();
(lo as i64, hi.map(|h| h as i64))
}
}
impl RMakeIter<i32, MyVecIter> for MyVec {
fn make_iter(&self) -> MyVecIter {
MyVecIter(RefCell::new(self.0.clone().into_iter()))
}
}
#[miniextendr]
impl RMakeIter<i32, MyVecIter> for MyVec {}In R (note: expose next as next_item since next is reserved):
v <- MyVec$new(c(1L, 2L, 3L))
it <- v$make_iter() # Create iterator
it$next_item() # 1L
it$next_item() # 2L
v$to_vec() # c(1L, 2L, 3L) - original unchanged