Struct spinoso_env::Memory[][src]

pub struct Memory { /* fields omitted */ }

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.


Fetching an environment variable:

let env = Memory::new();
// `Memory` backends start out 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")?, Some(Cow::Borrowed(&b"spinoso_env::Memory"[..])));


impl Memory[src]

#[must_use]pub fn new() -> Self[src]

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.


let env = Memory::new();

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.


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


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

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

Sets the environment variable name to value.

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


let mut env = Memory::new();
env.put(b"RUBY", Some(b"Artichoke"))?;
assert_eq!(env.get(b"RUBY")?, Some(Cow::Borrowed(&b"Artichoke"[..])));
env.put(b"RUBY", None)?;
assert_eq!(env.get(b"RUBY")?, None);


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.

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

Serialize the environ to a HashMap.

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


let env = Memory::new();
let map = env.to_map()?;


This function is infallible.

Trait Implementations

impl Clone for Memory[src]

impl Debug for Memory[src]

impl Default for Memory[src]

impl Eq for Memory[src]

impl PartialEq<Memory> for Memory[src]

impl StructuralEq for Memory[src]

impl StructuralPartialEq for Memory[src]

Auto Trait Implementations

impl RefUnwindSafe for Memory

impl Send for Memory

impl Sync for Memory

impl Unpin for Memory

impl UnwindSafe for Memory

Blanket Implementations

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

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

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

impl<T> From<T> for T[src]

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

impl<T> ToOwned for T where
    T: Clone

type Owned = T

The resulting type after obtaining ownership.

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

type Error = Infallible

The type returned in the event of a conversion error.

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

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

The type returned in the event of a conversion error.