Skip to main content

RExtend

Trait RExtend 

Source
pub trait RExtend<T> {
    // Required method
    fn extend_from_vec(&self, items: Vec<T>);

    // Provided methods
    fn extend_from_slice(&self, items: &[T])
       where T: Clone { ... }
    fn len(&self) -> i64 { ... }
    fn is_empty(&self) -> bool { ... }
}
Expand description

Adapter trait for std::iter::Extend.

Provides collection extension operations for R, allowing Rust collections to be extended with R vectors. Since extension requires mutation, the wrapper type should use interior mutability (e.g., RefCell).

§Methods

  • extend_from_vec(items) - Extend the collection with items from a vector
  • extend_from_slice(items) - Extend from a slice (for Clone items)

§Example

use std::cell::RefCell;

#[derive(ExternalPtr)]
struct MyVec(RefCell<Vec<i32>>);

impl MyVec {
    fn new() -> Self {
        Self(RefCell::new(Vec::new()))
    }
}

impl RExtend<i32> for MyVec {
    fn extend_from_vec(&self, items: Vec<i32>) {
        self.0.borrow_mut().extend(items);
    }
}

#[miniextendr]
impl RExtend<i32> for MyVec {}

In R:

v <- MyVec$new()
v$extend_from_vec(c(1L, 2L, 3L))  # Add items
v$extend_from_vec(c(4L, 5L))      # Add more items

§Design Note

Like RIterator, RExtend does NOT have a blanket impl because Extend::extend() requires &mut self, but R’s ExternalPtr pattern provides &self. Users must implement this trait manually using interior mutability (RefCell, Mutex, etc.).

Required Methods§

Source

fn extend_from_vec(&self, items: Vec<T>)

Extend the collection with items from a vector.

The items are moved into the collection.

Provided Methods§

Source

fn extend_from_slice(&self, items: &[T])
where T: Clone,

Extend the collection with cloned items from a slice.

Default implementation clones items into a Vec and calls extend_from_vec.

Skipped from trait ABI because &[T] doesn’t have TryFromSexp.

Source

fn len(&self) -> i64

Get the current length of the collection.

Optional - returns -1 if not implemented.

Source

fn is_empty(&self) -> bool

Check if the collection is empty.

Returns false when length is unknown.

Implementors§