Skip to main content

RToVec

Trait RToVec 

Source
pub trait RToVec<T> {
    // Required methods
    fn to_vec(&self) -> Vec<T>;
    fn len(&self) -> i64;

    // Provided method
    fn is_empty(&self) -> bool { ... }
}
Expand description

Adapter trait for collections that can be converted to vectors.

This is the complement to RFromIter: while RFromIter creates collections from vectors, RToVec extracts vectors from collections.

§Methods

  • to_vec() - Collect all elements into a vector (cloning elements)
  • len() - Get the number of elements
  • is_empty() - Check if the collection is empty

§Design Note

Unlike Rust’s IntoIterator::into_iter() which consumes the collection, this trait borrows the collection and clones elements. This is necessary because R’s ExternalPtr pattern provides &self, not owned self.

For consuming iteration, use RIterator with interior mutability.

§Example

use std::collections::HashSet;

#[derive(ExternalPtr)]
struct MySet(HashSet<i32>);

// RToVec is automatically available via blanket impl
#[miniextendr]
impl RToVec<i32> for MySet {}

In R:

set <- MySet$new(...)
vec <- set$to_vec()    # Get all elements as vector
set$len()              # Number of elements
set$is_empty()         # Check if empty

Required Methods§

Source

fn to_vec(&self) -> Vec<T>

Collect all elements into a vector.

Elements are cloned from the collection.

Source

fn len(&self) -> i64

Get the number of elements in the collection.

Provided Methods§

Source

fn is_empty(&self) -> bool

Check if the collection is empty.

Implementors§

Source§

impl<C, T> RToVec<T> for C
where T: Clone, for<'a> &'a C: IntoIterator<Item = &'a T>, for<'a> <&'a C as IntoIterator>::IntoIter: ExactSizeIterator,