pub struct System { /* private fields */ }
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§
source§impl 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§
source§impl Ord for System
impl Ord for System
source§impl PartialOrd for System
impl PartialOrd for System
impl Copy for System
impl Eq for System
impl StructuralPartialEq for System
Auto Trait Implementations§
impl Freeze for System
impl RefUnwindSafe for System
impl Send for System
impl Sync for System
impl Unpin for System
impl UnwindSafe for System
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)