pub struct Environment {
pub(crate) robj: Robj,
}Fields§
§robj: RobjImplementations§
Source§impl Environment
impl Environment
Sourcepub fn new_with_parent(parent: Environment) -> Self
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);
}Sourcepub fn new_with_capacity(parent: Environment, capacity: usize) -> Self
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);
}Sourcepub fn from_pairs<NV>(parent: Environment, names_and_values: NV) -> Self
pub fn from_pairs<NV>(parent: Environment, names_and_values: NV) -> Self
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);
}Sourcepub fn call(&self) -> Option<Robj>
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.
Sourcepub fn parent(&self) -> Option<Environment>
pub fn parent(&self) -> Option<Environment>
Get the enclosing (parent) environment.
Sourcepub fn names(&self) -> impl Iterator<Item = &'static str>
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"]);
}Sourcepub fn keys(&self) -> impl Iterator<Item = &'static str>
pub fn keys(&self) -> impl Iterator<Item = &'static str>
An alias for Environment::names.
Sourcepub fn values(&self) -> impl Iterator<Item = Robj> + '_
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)]);
}Sourcepub fn contains_key<K: AsRef<str>>(&self, key: K) -> bool
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"));
}Sourcepub fn insert<K: AsRef<str>, V: Into<Robj>>(
&mut self,
key: K,
value: V,
) -> Result<Option<Robj>>
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")));
}Sourcepub fn remove<K: AsRef<str>>(&mut self, key: K) -> Result<Option<Robj>>
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));
}Sourcepub fn set_local<K: Into<Robj>, V: Into<Robj>>(&self, key: K, value: V)
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")));
}Sourcepub fn local<K: Into<Robj>>(&self, key: K) -> Result<Robj>
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
impl Clone for Environment
Source§impl Conversions for Environment
as_*()
impl Conversions for Environment
as_*()
Source§fn as_language(&self) -> Option<Language>
fn as_language(&self) -> Option<Language>
Source§fn as_pairlist(&self) -> Option<Pairlist>
fn as_pairlist(&self) -> Option<Pairlist>
Source§fn as_expressions(&self) -> Option<Expressions>
fn as_expressions(&self) -> Option<Expressions>
Source§fn as_environment(&self) -> Option<Environment>
fn as_environment(&self) -> Option<Environment>
Source§fn as_function(&self) -> Option<Function>
fn as_function(&self) -> Option<Function>
Source§fn as_promise(&self) -> Option<Promise>
fn as_promise(&self) -> Option<Promise>
Source§impl Debug for Environment
impl Debug for Environment
Source§impl From<&Environment> for Robj
impl From<&Environment> for Robj
Source§fn from(val: &Environment) -> Self
fn from(val: &Environment) -> Self
Make an robj from a wrapper.
Source§impl From<Environment> for Robj
impl From<Environment> for Robj
Source§fn from(val: Environment) -> Self
fn from(val: Environment) -> Self
Make an robj from a wrapper.
Source§impl GetSexp for Environment
impl GetSexp for Environment
Source§fn as_robj_mut(&mut self) -> &mut Robj
fn as_robj_mut(&mut self) -> &mut Robj
Source§impl Length for Environment
len() and is_empty()
impl Length for Environment
len() and is_empty()
Source§impl Operators for Environment
dollar() etc.
impl Operators for Environment
dollar() etc.
Source§impl PartialEq for Environment
impl PartialEq for Environment
Source§impl Rinternals for Environment
find_var() etc.
impl Rinternals for Environment
find_var() etc.
Source§fn is_logical(&self) -> bool
fn is_logical(&self) -> bool
Source§fn is_complex(&self) -> bool
fn is_complex(&self) -> bool
Source§fn is_expressions(&self) -> bool
fn is_expressions(&self) -> bool
Source§fn is_environment(&self) -> bool
fn is_environment(&self) -> bool
Source§fn is_promise(&self) -> bool
fn is_promise(&self) -> bool
Source§fn is_external_pointer(&self) -> bool
fn is_external_pointer(&self) -> bool
Source§fn get_current_srcref(val: i32) -> Robj
fn get_current_srcref(val: i32) -> Robj
Source§fn get_src_filename(&self) -> Robj
fn get_src_filename(&self) -> Robj
Source§fn as_character_vector(&self) -> Robj
fn as_character_vector(&self) -> Robj
Source§fn coerce_vector(&self, sexptype: SEXPTYPE) -> Robj
fn coerce_vector(&self, sexptype: SEXPTYPE) -> Robj
Source§fn pair_to_vector_list(&self) -> Robj
fn pair_to_vector_list(&self) -> Robj
Source§fn vector_to_pair_list(&self) -> Robj
fn vector_to_pair_list(&self) -> Robj
Source§fn as_character_factor(&self) -> Robj
fn as_character_factor(&self) -> Robj
Source§fn duplicate(&self) -> Robj
fn duplicate(&self) -> Robj
Source§fn find_function<K: TryInto<Symbol, Error = Error>>(
&self,
key: K,
) -> Result<Robj>
fn find_function<K: TryInto<Symbol, Error = Error>>( &self, key: K, ) -> Result<Robj>
Source§fn find_var<K: TryInto<Symbol, Error = Error>>(&self, key: K) -> Result<Robj>
fn find_var<K: TryInto<Symbol, Error = Error>>(&self, key: K) -> Result<Robj>
Source§fn xlengthgets(&self, new_len: usize) -> Result<Robj>
fn xlengthgets(&self, new_len: usize) -> Result<Robj>
Source§fn alloc_vector(sexptype: SEXPTYPE, len: usize) -> Robj
fn alloc_vector(sexptype: SEXPTYPE, len: usize) -> Robj
Source§fn is_function(&self) -> bool
fn is_function(&self) -> bool
Source§fn is_integer(&self) -> bool
fn is_integer(&self) -> bool
Source§fn is_language(&self) -> bool
fn is_language(&self) -> bool
Source§fn is_pairlist(&self) -> bool
fn is_pairlist(&self) -> bool
Source§fn is_number(&self) -> bool
fn is_number(&self) -> bool
Source§fn is_primitive(&self) -> bool
fn is_primitive(&self) -> bool
Source§fn is_user_binop(&self) -> bool
fn is_user_binop(&self) -> bool
Source§fn is_vector_atomic(&self) -> bool
fn is_vector_atomic(&self) -> bool
Source§fn is_vector_list(&self) -> bool
fn is_vector_list(&self) -> bool
Source§fn is_vectorizable(&self) -> bool
fn is_vectorizable(&self) -> bool
fn is_missing_arg(&self) -> bool
fn is_package_env(&self) -> bool
fn package_env_name(&self) -> Robj
fn is_namespace_env(&self) -> bool
fn namespace_env_spec(&self) -> Robj
Source§fn is_altinteger(&self) -> bool
fn is_altinteger(&self) -> bool
true if this is an integer ALTREP object.Source§fn is_altreal(&self) -> bool
fn is_altreal(&self) -> bool
true if this is an real ALTREP object.Source§fn is_altlogical(&self) -> bool
fn is_altlogical(&self) -> bool
true if this is an logical ALTREP object.Source§fn is_altstring(&self) -> bool
fn is_altstring(&self) -> bool
true if this is an integer ALTREP object.Source§fn is_altlist(&self) -> bool
fn is_altlist(&self) -> bool
true if this is an integer ALTREP object.Source§impl Serialize for Environment
impl Serialize for Environment
Source§impl Slices for Environment
as_typed_slice_raw() etc.
impl Slices for Environment
as_typed_slice_raw() etc.
Source§unsafe fn as_typed_slice_raw<T>(&self) -> &[T]
unsafe fn as_typed_slice_raw<T>(&self) -> &[T]
Source§unsafe fn as_typed_slice_raw_mut<T>(&mut self) -> &mut [T]
unsafe fn as_typed_slice_raw_mut<T>(&mut self) -> &mut [T]
impl StructuralPartialEq for Environment
Source§impl TryFrom<&Robj> for Environment
impl TryFrom<&Robj> for Environment
Source§impl TryFrom<Robj> for Environment
impl TryFrom<Robj> for Environment
Auto Trait Implementations§
impl !Send for Environment
impl !Sync for Environment
impl Freeze for Environment
impl RefUnwindSafe for Environment
impl Unpin for Environment
impl UnsafeUnpin for Environment
impl UnwindSafe for Environment
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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