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 r_len(&self) -> i64 { ... }
    fn r_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)
  • r_len() (len() in R) - Get the current length, or -1 if unknown
  • r_is_empty() (is_empty() in R) - Check if the collection is empty

§Example

ⓘ
use std::cell::RefCell;

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

#[miniextendr(env)]
impl MyVec {
    fn new() -> Self {
        Self(RefCell::new(Vec::new()))
    }
}

// The trait is generic, so the impl spells out each exported method and
// repeats the R names of the renamed ones.
#[miniextendr(env)]
impl miniextendr_api::adapter_traits::RExtend<i32> for MyVec {
    fn extend_from_vec(&self, items: Vec<i32>) {
        self.0.borrow_mut().extend(items);
    }

    fn r_len(&self) -> i64 {
        i64::try_from(self.0.borrow().len()).expect("length fits i64")
    }

    fn r_is_empty(&self) -> bool {
        self.0.borrow().is_empty()
    }
}

In R:

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

§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 r_len(&self) -> i64

Get the current length of the collection.

Optional - returns -1 if not implemented.

Source

fn r_is_empty(&self) -> bool

Check if the collection is empty.

Returns false when length is unknown.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§