Skip to main content

Environment

Struct Environment 

Source
pub struct Environment {
    pub(crate) robj: Robj,
}

Fields§

§robj: Robj

Implementations§

Source§

impl Environment

Source

pub fn new_with_parent(parent: Environment) -> Self

Create a new, empty environment.

use extendr_api::prelude::*;
test! {
    let env = Environment::new_with_parent(Environment::global());
    assert_eq!(env.len(), 0);
}
Source

pub fn new_with_capacity(parent: Environment, capacity: usize) -> Self

Create a new, empty environment with a reserved size.

This function will guess the hash table size if required. Use the Env{} wrapper for more detail.

use extendr_api::prelude::*;
test! {
    let env = Environment::new_with_capacity(Environment::global(), 5);
    env.set_local(sym!(a), 1);
    env.set_local(sym!(b), 2);
    assert_eq!(env.len(), 2);
}
Source

pub fn from_pairs<NV>(parent: Environment, names_and_values: NV) -> Self
where NV: IntoIterator, NV::Item: SymPair,

Make an R environment object.

use extendr_api::prelude::*;
use std::convert::TryInto;
test! {
    let names_and_values = (0..100).map(|i| (format!("n{}", i), i));
    let mut env = Environment::from_pairs(Environment::global(), names_and_values);
    assert_eq!(env.len(), 100);
}
Source

pub fn current() -> Self

Get the current evaluation environment.

Source

pub fn global() -> Self

Get the global environment.

Source

pub fn base() -> Self

Get the base environment.

Source

pub fn empty() -> Self

Get the empty environment.

Source

pub fn call(&self) -> Option<Robj>

Get the call associated with this environment. Mimics rlang’s frame_call(): evaluates sys.frames() in self, finds the matching frame index, then returns sys.call(i). Returns None if not called from a function frame or if the call cannot be determined.

Source

pub fn caller() -> Self

Get the caller’s environment (i.e. parent.frame()).

Source

pub fn parent(&self) -> Option<Environment>

Get the enclosing (parent) environment.

Source

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

Get the names in an environment, sorted.

use extendr_api::prelude::*;
test! {
   let names_and_values : std::collections::HashMap<_, _> = (0..4).map(|i| (format!("n{}", i), r!(i))).collect();
   let env = Environment::from_pairs(Environment::global(), names_and_values);
   assert_eq!(env.names().collect::<Vec<_>>(), vec!["n0", "n1", "n2", "n3"]);
}
Source

pub fn keys(&self) -> impl Iterator<Item = &'static str>

An alias for Environment::names.

Source

pub fn values(&self) -> impl Iterator<Item = Robj> + '_

Get the values in an environment, ordered by name.

use extendr_api::prelude::*;
test! {
   let env = Environment::from_pairs(Environment::global(), vec![("b", r!(2)), ("a", r!(1))]);
   assert_eq!(env.values().collect::<Vec<_>>(), vec![r!(1), r!(2)]);
}
Source

pub fn contains_key<K: AsRef<str>>(&self, key: K) -> bool

Returns true if the environment, but not its ancestors, has a binding for key.

use extendr_api::prelude::*;
test! {
    let mut env = Environment::new_with_parent(Environment::global());
    env.insert("x", 1)?;
    assert!(env.contains_key("x"));
    assert!(!env.contains_key("y"));
}
Source

pub fn insert<K: AsRef<str>, V: Into<Robj>>( &mut self, key: K, value: V, ) -> Result<Option<Robj>>

Bind value to key in the environment, returning the previous value if there was one.

Fails if the binding is locked, or if the environment is locked and key is new.

use extendr_api::prelude::*;
test! {
    let mut env = Environment::new_with_parent(Environment::global());
    assert_eq!(env.insert("x", "harry"), Ok(None));
    assert_eq!(env.insert("x", "fred"), Ok(Some(r!("harry"))));
    assert_eq!(env.local(sym!(x)), Ok(r!("fred")));
}
Source

pub fn remove<K: AsRef<str>>(&mut self, key: K) -> Result<Option<Robj>>

Remove key from the environment, returning its value if there was one.

Fails if the environment is locked.

use extendr_api::prelude::*;
test! {
    let mut env = Environment::new_with_parent(Environment::global());
    env.insert("x", 1)?;
    assert_eq!(env.remove("x"), Ok(Some(r!(1))));
    assert_eq!(env.remove("x"), Ok(None));
}
Source

pub fn set_local<K: Into<Robj>, V: Into<Robj>>(&self, key: K, value: V)

Set or define a variable in an environment.

use extendr_api::prelude::*;
test! {
    let env = Environment::new_with_parent(Environment::global());
    env.set_local(sym!(x), "harry");
    env.set_local(sym!(x), "fred");
    assert_eq!(env.local(sym!(x)), Ok(r!("fred")));
}
Source

pub fn local<K: Into<Robj>>(&self, key: K) -> Result<Robj>

Get a variable from an environment, but not its ancestors.

use extendr_api::prelude::*;
test! {
    let env = Environment::new_with_parent(Environment::global());
    env.set_local(sym!(x), "fred");
    assert_eq!(env.local(sym!(x)), Ok(r!("fred")));
}

Trait Implementations§

Source§

impl Clone for Environment

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Conversions for Environment

as_*()

Source§

fn as_symbol(&self) -> Option<Symbol>

Convert a symbol object to a Symbol wrapper. Read more
Source§

fn as_char(&self) -> Option<Rstr>

Convert a CHARSXP object to a Rstr wrapper. Read more
Source§

fn as_raw(&self) -> Option<Raw>

Convert a raw object to a Rstr wrapper. Read more
Source§

fn as_language(&self) -> Option<Language>

Convert a language object to a Language wrapper. Read more
Source§

fn as_pairlist(&self) -> Option<Pairlist>

Convert a pair list object (LISTSXP) to a Pairlist wrapper. Read more
Source§

fn as_list(&self) -> Option<List>

Convert a list object (VECSXP) to a List wrapper. Read more
Source§

fn as_expressions(&self) -> Option<Expressions>

Convert an expression object (EXPRSXP) to a Expr wrapper. Read more
Source§

fn as_environment(&self) -> Option<Environment>

Convert an environment object (ENVSXP) to a Env wrapper. Read more
Source§

fn as_function(&self) -> Option<Function>

Convert a function object (CLOSXP) to a Function wrapper. Read more
Source§

fn as_promise(&self) -> Option<Promise>

Get a wrapper for a promise.
Source§

impl Debug for Environment

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<&Environment> for Robj

Source§

fn from(val: &Environment) -> Self

Make an robj from a wrapper.

Source§

impl From<Environment> for Robj

Source§

fn from(val: Environment) -> Self

Make an robj from a wrapper.

Source§

impl GetSexp for Environment

Source§

unsafe fn get(&self) -> SEXP

Get a copy of the underlying SEXP. Read more
Source§

unsafe fn get_mut(&mut self) -> SEXP

Safety Read more
Source§

fn as_robj(&self) -> &Robj

Get a reference to a Robj for this type.
Source§

fn as_robj_mut(&mut self) -> &mut Robj

Get a mutable reference to a Robj for this type.
Source§

impl Length for Environment

len() and is_empty()

Source§

fn len(&self) -> usize

Get the extended length of the object. Read more
Source§

fn is_empty(&self) -> bool

Returns true if the Robj contains no elements. Read more
Source§

impl Operators for Environment

dollar() etc.

Source§

fn dollar<T>(&self, symbol: T) -> Result<Robj>
where T: AsRef<str>,

Do the equivalent of x$y Read more
Source§

fn slice<T>(&self, rhs: T) -> Result<Robj>
where T: Into<Robj>,

Do the equivalent of x[y] Read more
Source§

fn index<T>(&self, rhs: T) -> Result<Robj>
where T: Into<Robj>,

Do the equivalent of x[[y]] Read more
Source§

fn tilde<T>(&self, rhs: T) -> Result<Robj>
where T: Into<Robj>,

Do the equivalent of x ~ y Read more
Source§

fn double_colon<T>(&self, rhs: T) -> Result<Robj>
where T: Into<Robj>,

Do the equivalent of x :: y Read more
Source§

fn call(&self, args: Pairlist) -> Result<Robj>

Do the equivalent of x(a, b, c) Read more
Source§

impl PartialEq for Environment

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Rinternals for Environment

find_var() etc.

Source§

fn is_null(&self) -> bool

Return true if this is the null object.
Source§

fn is_symbol(&self) -> bool

Return true if this is a symbol.
Source§

fn is_logical(&self) -> bool

Return true if this is a boolean (logical) vector
Source§

fn is_real(&self) -> bool

Return true if this is a real (f64) vector.
Source§

fn is_complex(&self) -> bool

Return true if this is a complex vector.
Source§

fn is_expressions(&self) -> bool

Return true if this is an expression.
Source§

fn is_environment(&self) -> bool

Return true if this is an environment.
Source§

fn is_promise(&self) -> bool

Return true if this is an environment.
Source§

fn is_string(&self) -> bool

Return true if this is a string.
Source§

fn is_object(&self) -> bool

Return true if this is an object (ie. has a class attribute).
Source§

fn is_s4(&self) -> bool

Return true if this is a S4 object.
Source§

fn is_external_pointer(&self) -> bool

Return true if this is an expression.
Source§

fn get_current_srcref(val: i32) -> Robj

Get the source ref.
Source§

fn get_src_filename(&self) -> Robj

Get the source filename.
Source§

fn as_character_vector(&self) -> Robj

Convert to a string vector.
Source§

fn coerce_vector(&self, sexptype: SEXPTYPE) -> Robj

Convert to vectors of many kinds.
Source§

fn pair_to_vector_list(&self) -> Robj

Convert a pairlist (LISTSXP) to a vector list (VECSXP).
Source§

fn vector_to_pair_list(&self) -> Robj

Convert a vector list (VECSXP) to a pair list (LISTSXP)
Source§

fn as_character_factor(&self) -> Robj

Convert a factor to a string vector.
Source§

fn alloc_matrix(sexptype: SEXPTYPE, rows: i32, cols: i32) -> Robj

Allocate a matrix object.
Source§

fn duplicate(&self) -> Robj

Do a deep copy of this object. Note that clone() only adds a reference.
Source§

fn find_function<K: TryInto<Symbol, Error = Error>>( &self, key: K, ) -> Result<Robj>

Find a function in an environment ignoring other variables. Read more
Source§

fn find_var<K: TryInto<Symbol, Error = Error>>(&self, key: K) -> Result<Robj>

Find a variable in an environment. Read more
Source§

fn ncols(&self) -> usize

Number of columns of a matrix
Source§

fn nrows(&self) -> usize

Number of rows of a matrix
Source§

fn xlengthgets(&self, new_len: usize) -> Result<Robj>

Source§

fn alloc_vector(sexptype: SEXPTYPE, len: usize) -> Robj

Allocated an owned object of a certain type.
Source§

fn conformable(a: &Robj, b: &Robj) -> bool

Return true if two arrays have identical dims.
Source§

fn is_array(&self) -> bool

Return true if this is an array.
Source§

fn is_factor(&self) -> bool

Return true if this is factor.
Source§

fn is_frame(&self) -> bool

Return true if this is a data frame.
Source§

fn is_function(&self) -> bool

Return true if this is a function or a primitive (CLOSXP, BUILTINSXP or SPECIALSXP)
Source§

fn is_integer(&self) -> bool

Return true if this is an integer vector (INTSXP) but not a factor.
Source§

fn is_language(&self) -> bool

Return true if this is a language object (LANGSXP).
Source§

fn is_pairlist(&self) -> bool

Return true if this is NILSXP or LISTSXP.
Source§

fn is_matrix(&self) -> bool

Return true if this is a matrix.
Source§

fn is_list(&self) -> bool

Return true if this is NILSXP or VECSXP.
Source§

fn is_number(&self) -> bool

Return true if this is INTSXP, LGLSXP or REALSXP but not a factor.
Source§

fn is_primitive(&self) -> bool

Return true if this is a primitive function BUILTINSXP, SPECIALSXP.
Source§

fn is_ts(&self) -> bool

Return true if this is a time series vector (see tsp).
Source§

fn is_user_binop(&self) -> bool

Return true if this is a user defined binop.
Source§

fn is_vector(&self) -> bool

Return true if this is a vector.
Source§

fn is_vector_atomic(&self) -> bool

Return true if this is an atomic vector.
Source§

fn is_vector_list(&self) -> bool

Return true if this is a vector list.
Source§

fn is_vectorizable(&self) -> bool

Return true if this is can be made into a vector.
Source§

fn is_raw(&self) -> bool

Return true if this is RAWSXP.
Source§

fn is_char(&self) -> bool

Return true if this is CHARSXP.
Source§

fn is_missing_arg(&self) -> bool

Source§

fn is_package_env(&self) -> bool

Source§

fn package_env_name(&self) -> Robj

Source§

fn is_namespace_env(&self) -> bool

Source§

fn namespace_env_spec(&self) -> Robj

Source§

fn is_altrep(&self) -> bool

Returns true if this is an ALTREP object.
Source§

fn is_altinteger(&self) -> bool

Returns true if this is an integer ALTREP object.
Source§

fn is_altreal(&self) -> bool

Returns true if this is an real ALTREP object.
Source§

fn is_altlogical(&self) -> bool

Returns true if this is an logical ALTREP object.
Source§

fn is_altraw(&self) -> bool

Returns true if this is a raw ALTREP object.
Source§

fn is_altstring(&self) -> bool

Returns true if this is an integer ALTREP object.
Source§

fn is_altlist(&self) -> bool

Returns true if this is an integer ALTREP object.
Source§

fn deparse(&self) -> Result<String>

Generate a text representation of this object.
Source§

impl Serialize for Environment

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Slices for Environment

as_typed_slice_raw() etc.

Source§

unsafe fn as_typed_slice_raw<T>(&self) -> &[T]

Get an immutable slice to this object’s data. Read more
Source§

unsafe fn as_typed_slice_raw_mut<T>(&mut self) -> &mut [T]

Get a mutable slice to this object’s data. Read more
Source§

impl StructuralPartialEq for Environment

Source§

impl TryFrom<&Robj> for Environment

Source§

fn try_from(robj: &Robj) -> Result<Self>

Make a wrapper from a robj if it matches.

Source§

type Error = Error

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

impl TryFrom<Robj> for Environment

Source§

fn try_from(robj: Robj) -> Result<Self>

Make a wrapper from a robj if it matches.

Source§

type Error = Error

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

impl Types for Environment

rtype() and rany()

Source§

fn rtype(&self) -> Rtype

Get the type of an R object. Read more
Source§

fn as_any(&self) -> Rany<'_>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> IntoRobj for T
where Robj: From<T>,

§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<R> Save for R
where R: GetSexp,

Source§

fn save<P: AsRef<Path>>( &self, path: &P, format: PstreamFormat, version: i32, hook: Option<WriteHook>, ) -> Result<()>

Save an object in the R data format. version should probably be 3.
Source§

fn to_writer<W: Write>( &self, writer: &mut W, format: PstreamFormat, version: i32, hook: Option<WriteHook>, ) -> Result<()>

Save an object in the R data format to a Write trait. version should probably be 3.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.