Skip to main content

RMakeIter

Trait RMakeIter 

Source
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 iterator
  • I: The iterator type returned (must implement [RIterator])

§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>>);

// `MyVecIter` implements `RIterator` as in the `RIterator` example.

#[miniextendr(env)]
impl miniextendr_api::adapter_traits::RMakeIter<i32, MyVecIter> for MyVec {
    fn make_iter(&self) -> MyVecIter {
        MyVecIter(RefCell::new(self.0.clone().into_iter()))
    }
}

In R (next is a reserved word, so the method is next_item):

v <- MyVec$new(c(1L, 2L, 3L))
it <- v$RMakeIter$make_iter()   # Create iterator
it$RIterator$next_item()        # 1L
it$RIterator$next_item()        # 2L

Required Methods§

Source

fn make_iter(&self) -> I

Create a new iterator wrapper.

The iterator is independent from this collection (typically by cloning the underlying data).

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§