[−][src]Struct spinoso_env::System
system-env
only.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!(matches!(ENV.get(b"PATH"), Ok(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"), Ok(String::from("spinoso_env::System")));
Implementations
impl System
[src]
#[must_use]pub const fn new() -> Self
[src]
Constructs a new, default ENV System
backend.
The resulting environment variable accessor has access to the underlying host operating system.
Examples
const ENV: System = System::new();
pub fn get(
self,
name: &[u8]
) -> Result<Option<Cow<'static, [u8]>>, ArgumentError>
[src]
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!(matches!(ENV.get(b"PATH"), Ok(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.
pub fn put(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.
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")?, Some(Cow::Borrowed(&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.
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.
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
impl Clone for System
[src]
impl Copy for System
[src]
impl Debug for System
[src]
impl Default for System
[src]
impl Eq for System
[src]
impl Hash for System
[src]
pub fn hash<__H: Hasher>(&self, state: &mut __H)
[src]
pub fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
impl Ord for System
[src]
pub fn cmp(&self, other: &System) -> Ordering
[src]
#[must_use]pub fn max(self, other: Self) -> Self
1.21.0[src]
#[must_use]pub fn min(self, other: Self) -> Self
1.21.0[src]
#[must_use]pub fn clamp(self, min: Self, max: Self) -> Self
1.50.0[src]
impl PartialEq<System> for System
[src]
impl PartialOrd<System> for System
[src]
pub fn partial_cmp(&self, other: &System) -> Option<Ordering>
[src]
pub fn lt(&self, other: &System) -> bool
[src]
pub fn le(&self, other: &System) -> bool
[src]
pub fn gt(&self, other: &System) -> bool
[src]
pub fn ge(&self, other: &System) -> bool
[src]
impl StructuralEq for System
[src]
impl StructuralPartialEq for System
[src]
Auto Trait Implementations
impl RefUnwindSafe for System
[src]
impl Send for System
[src]
impl Sync for System
[src]
impl Unpin for System
[src]
impl UnwindSafe for System
[src]
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,