Skip to main content

NamedList

Struct NamedList 

Source
pub struct NamedList {
    list: List,
    index: HashMap<String, usize>,
}
Expand description

A named list with O(1) name-based element lookup.

Wraps a List and builds a HashMap<String, usize> index of element names on construction. Use this when you need to access multiple elements by name from the same list — each lookup is O(1) instead of O(n).

§When to Use

PatternType
Single named lookupList::get_named is fine
Multiple named lookupsNamedList (O(n) build + O(1) per lookup)
Positional access onlyList — no indexing overhead

§Name Handling

  • NA and empty-string names are excluded from the index
  • If duplicate names exist, the last occurrence wins
  • Positional access via get_index is always available

Fields§

§list: List§index: HashMap<String, usize>

Implementations§

Source§

impl NamedList

Source

pub fn as_data_frame(&self) -> Result<DataFrameView, DataFrameError>

Promote this named list to a DataFrameView.

Validates that all columns have equal length, then sets the class attribute to "data.frame" and adds compact integer row.names.

§Errors

Returns DataFrameError::UnequalLengths if columns differ in length.

Source§

impl NamedList

Source

pub fn new(list: List) -> Option<Self>

Build a NamedList from a List, indexing all non-empty, non-NA names.

Returns None if the list has no names attribute.

Source

pub fn get<T>(&self, name: &str) -> Option<T>
where T: TryFromSexp<Error = SexpError>,

Get an element by name with O(1) lookup, converting to type T.

Returns None if the name is not found or conversion fails.

Source

pub fn get_raw(&self, name: &str) -> Option<SEXP>

Get a raw SEXP element by name with O(1) lookup.

Source

pub fn get_index<T>(&self, idx: isize) -> Option<T>
where T: TryFromSexp<Error = SexpError>,

Get element at 0-based index and convert to type T.

Falls through to List::get_index — no name lookup involved.

Source

pub fn contains(&self, name: &str) -> bool

Check if a name exists in the index.

Source

pub fn len(&self) -> isize

Number of elements in the list (including unnamed ones).

Source

pub fn is_empty(&self) -> bool

Returns true if the list is empty.

Source

pub fn named_len(&self) -> usize

Number of indexed (named) elements.

Source

pub fn as_list(&self) -> List

Get the underlying List.

Source

pub fn into_list(self) -> List

Consume and return the underlying List.

Source

pub fn names(&self) -> impl Iterator<Item = &str>

Iterate over indexed names (unordered).

Source

pub fn entries(&self) -> impl Iterator<Item = (&str, usize)>

Iterate over (name, position) pairs (unordered).

Trait Implementations§

Source§

impl IntoR for NamedList

Source§

type Error = Infallible

The error type for fallible conversions. Read more
Source§

fn try_into_sexp(self) -> Result<SEXP, Self::Error>

Try to convert this value to an R SEXP. Read more
Source§

unsafe fn try_into_sexp_unchecked(self) -> Result<SEXP, Self::Error>

Try to convert to SEXP without thread safety checks. Read more
Source§

fn into_sexp(self) -> SEXP

Convert this value to an R SEXP, panicking on error. Read more
Source§

unsafe fn into_sexp_unchecked(self) -> SEXP
where Self: Sized,

Convert to SEXP without thread safety checks, panicking on error. Read more
Source§

impl TryFromSexp for NamedList

Source§

type Error = SexpError

The error type returned when conversion fails.
Source§

fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error>

Attempt to convert an R SEXP to this Rust type. Read more
Source§

unsafe fn try_from_sexp_unchecked(sexp: SEXP) -> Result<Self, Self::Error>

Convert from SEXP without thread safety checks. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.