Struct spinoso_env::System
source · [−]pub struct System { /* private fields */ }
system-env
only.Expand description
A hash-like accessor for environment variables using platform APIs.
System
is an accessor to the host system’s environment variables using the
functions provided by the Rust Standard Library in the
std::env
module.
Use of this ENV
backend allows Ruby code to access and modify the host
system. It is not appropriate to use this backend in embedded or untrusted
contexts.
Examples
Fetching an environment variable:
const ENV: System = System::new();
assert!(ENV.get(b"PATH")?.is_some());
Setting an environment variable:
const ENV: System = System::new();
ENV.put(b"ENV_BACKEND", Some(b"spinoso_env::System"))?;
assert_eq!(std::env::var("ENV_BACKEND").as_deref(), Ok("spinoso_env::System"));
Implementations
sourceimpl System
impl System
sourcepub const fn new() -> Self
pub const fn new() -> Self
Constructs a new, default ENV System
backend.
The resulting environment variable accessor has access to the host system via platform APIs.
Examples
const ENV: System = System::new();
sourcepub fn get(
self,
name: &[u8]
) -> Result<Option<Cow<'static, [u8]>>, ArgumentError>
pub fn get(
self,
name: &[u8]
) -> Result<Option<Cow<'static, [u8]>>, ArgumentError>
Retrieves the value for environment variable name
.
Returns None
if the named variable does not exist. If the retrieved
environment variable value cannot be converted from a platform string
to a byte vector, None
is returned.
Implementation notes
This method accesses the host system’s environment using env::var_os
.
Examples
const ENV: System = System::new();
assert!(ENV.get(b"PATH")?.is_some());
Errors
If name
contains a NUL byte, e.g. b'\0'
, an error is returned.
If the environment variable name or value cannot be converted from a byte vector to a platform string, an error is returned.
sourcepub fn put(self, name: &[u8], value: Option<&[u8]>) -> Result<(), Error>
pub fn put(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.
Implementation notes
This method accesses the host system’s environment using env::set_var
and env::remove_var
.
Examples
const ENV: System = System::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.
If the environment variable name or value cannot be converted from a byte vector to a platform string, an error is returned.
sourcepub fn to_map(self) -> Result<HashMap<Vec<u8>, Vec<u8>>, ArgumentError>
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.
Implementation notes
This method accesses the host system’s environment using env::vars_os
.
Examples
const ENV: System = System::new();
let map = ENV.to_map()?;
assert!(map.contains_key(&b"PATH"[..]));
Errors
If any environment variable name or value cannot be converted from a platform string to a byte vector, an error is returned.
Trait Implementations
sourceimpl Ord for System
impl Ord for System
sourceimpl PartialOrd<System> for System
impl PartialOrd<System> for System
sourcefn partial_cmp(&self, other: &System) -> Option<Ordering>
fn partial_cmp(&self, other: &System) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
impl Copy for System
impl Eq for System
impl StructuralEq for System
impl StructuralPartialEq for System
Auto Trait Implementations
impl RefUnwindSafe for System
impl Send for System
impl Sync for System
impl Unpin for System
impl UnwindSafe for System
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more