Struct spinoso_env::System[][src]

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

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();

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.

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.

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.