[][src]Struct spinoso_env::System

pub struct System { /* fields omitted */ }
This is supported on crate feature 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]

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]

impl Ord for System[src]

impl PartialEq<System> for System[src]

impl PartialOrd<System> for System[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]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

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

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.