spinoso_env

Struct Memory

Source
pub struct Memory { /* private fields */ }
Expand description

A hash-like accessor for environment variables using a fake in-memory store.

Memory offers the same API as other environment variable backends in this crate, but does not access or mutate the underlying system.

This backend is suitable for running in untrusted environments such as a Wasm binary, testing environments, or in embedded contexts.

§Examples

Fetching an environment variable:

let env = Memory::new();
// `Memory` backends are initially empty.
assert_eq!(env.get(b"PATH"), Ok(None));

Setting an environment variable:

let mut env = Memory::new();
env.put(b"ENV_BACKEND", Some(b"spinoso_env::Memory"))?;
assert_eq!(
    env.get(b"ENV_BACKEND")?.as_deref(),
    Some(&b"spinoso_env::Memory"[..])
);

Implementations§

Source§

impl Memory

Source

pub fn new() -> Self

Constructs a new, empty ENV Memory backend.

The resulting environment variable accessor has no access to the underlying host operating system. The returned accessor uses a virtual environment.

§Examples
let env = Memory::new();
Source

pub fn get<'a>( &'a self, name: &[u8], ) -> Result<Option<Cow<'a, [u8]>>, ArgumentError>

Retrieves the value for environment variable name.

Returns None if the named variable does not exist.

§Examples
let env = Memory::new();
assert_eq!(env.get(b"PATH"), Ok(None));
§Errors

If name contains a NUL byte, e.g. b'\0', an error is returned.

Source

pub fn put(&mut self, name: &[u8], value: Option<&[u8]>) -> Result<(), Error>

Sets the environment variable name to value.

If the value given is None the environment variable is deleted.

§Examples
let mut env = Memory::new();

env.put(b"RUBY", Some(b"Artichoke"))?;
assert_eq!(env.get(b"RUBY")?.as_deref(), Some(&b"Artichoke"[..]));

env.put(b"RUBY", None)?;
assert_eq!(env.get(b"RUBY")?, None);
§Errors

If name contains a NUL byte, e.g. b'\0', an argument error is returned.

If name contains an ‘=’ byte, e.g. b'=', an EINVAL error is returned.

If value is Some and contains a NUL byte, e.g. b'\0', an argument error is returned.

Source

pub fn to_map(&self) -> Result<HashMap<Vec<u8>, Vec<u8>>, ArgumentError>

Serialize the environ to a HashMap.

Map keys are environment variable names and map values are environment variable values.

§Examples
let env = Memory::new();
let map = env.to_map()?;
assert!(map.is_empty());
§Errors

This function is infallible.

Trait Implementations§

Source§

impl Clone for Memory

Source§

fn clone(&self) -> Memory

Returns a copy of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Memory

Source§

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

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

impl Default for Memory

Source§

fn default() -> Memory

Returns the “default value” for a type. Read more
Source§

impl PartialEq for Memory

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Memory

Source§

impl StructuralPartialEq for Memory

Auto Trait Implementations§

§

impl Freeze for Memory

§

impl RefUnwindSafe for Memory

§

impl Send for Memory

§

impl Sync for Memory

§

impl Unpin for Memory

§

impl UnwindSafe for Memory

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, dst: *mut u8)

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