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
impl Memory
Sourcepub fn new() -> Self
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();Sourcepub fn put(&mut self, name: &[u8], value: Option<&[u8]>) -> Result<(), Error>
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.