Skip to main content

RToVec

Trait RToVec 

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

    // Provided method
    fn r_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

  • r_to_vec() (to_vec() in R) - Collect all elements into a vector (cloning elements)
  • r_len() (len() in R) - Get the number of elements
  • r_is_empty() (is_empty() in R) - 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 miniextendr_api::adapter_traits::RToVec;
use std::collections::HashSet;

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

// `&MySet` doesn't iterate, so the blanket impl doesn't cover the wrapper:
// the impl forwards to the set, which it does cover. The trait is generic,
// so the impl spells out each method; R names come from the trait.
#[miniextendr(env)]
impl miniextendr_api::adapter_traits::RToVec<i32> for MySet {
    fn r_to_vec(&self) -> Vec<i32> {
        self.0.r_to_vec()
    }

    fn r_len(&self) -> i64 {
        self.0.r_len()
    }

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

In R:

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

Required Methods§

Source

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

Collect all elements into a vector.

Elements are cloned from the collection.

Source

fn r_len(&self) -> i64

Get the number of elements in the collection.

Provided Methods§

Source

fn r_is_empty(&self) -> bool

Check if the collection is empty.

Dyn Compatibility§

This trait is dyn compatible.

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

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,