pub struct Env {
env: HashMap<OsString, OsString>,
normalized_env: HashMap<String, String>,
case_insensitive_env: HashMap<String, String>,
}
Expand description
A snapshot of the environment variables available to GlobalContext
.
Currently, the GlobalContext
supports lookup of environment variables
through two different means:
GlobalContext::get_env
andGlobalContext::get_env_os
for process environment variables (similar tostd::env::var
andstd::env::var_os
),- Typed Config Value API via
GlobalContext::get
. This is only available forCARGO_
prefixed environment keys.
This type contains the env var snapshot and helper methods for both APIs.
Fields§
§env: HashMap<OsString, OsString>
A snapshot of the process’s environment variables.
normalized_env: HashMap<String, String>
Used in the typed Config value API for warning messages when config keys are given in the wrong format.
Maps from “normalized” (upper case and with “-” replaced by “_”) env keys to the actual keys in the environment. The normalized format is the one expected by Cargo.
This only holds env keys that are valid UTF-8, since super::ConfigKey
only supports UTF-8 keys.
In addition, this only holds env keys whose value in the environment is also valid UTF-8,
since the typed Config value API only supports UTF-8 values.
case_insensitive_env: HashMap<String, String>
Used to implement get_env
and get_env_os
on Windows, where env keys are case-insensitive.
Maps from uppercased env keys to the actual key in the environment.
For example, this might hold a pair ("PATH", "Path")
.
Currently only supports UTF-8 keys and values.
Implementations§
Source§impl Env
impl Env
Sourcepub(super) fn from_map(env: HashMap<String, String>) -> Self
pub(super) fn from_map(env: HashMap<String, String>) -> Self
Set the env directly from a HashMap
.
This should be used for debugging purposes only.
Sourcepub fn iter_str(&self) -> impl Iterator<Item = (&str, &str)>
pub fn iter_str(&self) -> impl Iterator<Item = (&str, &str)>
Returns all environment variables as an iterator, keeping only entries where both the key and value are valid UTF-8.
Sourcepub fn keys_str(&self) -> impl Iterator<Item = &str>
pub fn keys_str(&self) -> impl Iterator<Item = &str>
Returns all environment variable keys, filtering out keys that are not valid UTF-8.
Sourcepub fn get_env_os(&self, key: impl AsRef<OsStr>) -> Option<&OsStr>
pub fn get_env_os(&self, key: impl AsRef<OsStr>) -> Option<&OsStr>
Get the value of environment variable key
through the snapshot in
GlobalContext
.
This can be used similarly to std::env::var_os
.
On Windows, we check for case mismatch since environment keys are case-insensitive.
Sourcepub fn get_env(&self, key: impl AsRef<OsStr>) -> CargoResult<&str>
pub fn get_env(&self, key: impl AsRef<OsStr>) -> CargoResult<&str>
Get the value of environment variable key
through the self.env
snapshot.
This can be used similarly to std::env::var
.
On Windows, we check for case mismatch since environment keys are case-insensitive.
Sourcefn get_env_case_insensitive(&self, key: impl AsRef<OsStr>) -> Option<&OsStr>
fn get_env_case_insensitive(&self, key: impl AsRef<OsStr>) -> Option<&OsStr>
Performs a case-insensitive lookup of key
in the environment.
This is relevant on Windows, where environment variables are case-insensitive. Note that this only works on keys that are valid UTF-8 and it uses Unicode uppercase, which may differ from the OS’s notion of uppercase.
Sourcepub(super) fn get_str(&self, key: impl AsRef<OsStr>) -> Option<&str>
pub(super) fn get_str(&self, key: impl AsRef<OsStr>) -> Option<&str>
Get the value of environment variable key
as a &str
.
Returns None
if key
is not in self.env
or if the value is not valid UTF-8.
This is intended for use in private methods of GlobalContext
,
and does not check for env key case mismatch.
This is case-sensitive on Windows (even though environment keys on Windows are usually
case-insensitive) due to an unintended regression in 1.28 (via #5552).
This should only affect keys used for cargo’s config-system env variables (CARGO_
prefixed ones), which are currently all uppercase.
We may want to consider rectifying it if users report issues.
One thing that adds a wrinkle here is the unstable advanced-env option that requires
case-sensitive keys.
Do not use this for any other purposes.
Use Env::get_env_os
or Env::get_env
instead, which properly handle case
insensitivity on Windows.
Sourcepub(super) fn contains_key(&self, key: impl AsRef<OsStr>) -> bool
pub(super) fn contains_key(&self, key: impl AsRef<OsStr>) -> bool
Check if the environment contains key
.
This is intended for use in private methods of GlobalContext
,
and does not check for env key case mismatch.
See the docstring of Env::get_str
for more context.
Sourcepub(super) fn get_normalized(&self, key: &str) -> Option<&str>
pub(super) fn get_normalized(&self, key: &str) -> Option<&str>
Looks up a normalized key
in the normalized_env
.
Returns the corresponding (non-normalized) env key if it exists, else None
.
This is used by super::GlobalContext::check_environment_key_case_mismatch
.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Env
impl RefUnwindSafe for Env
impl Send for Env
impl Sync for Env
impl Unpin for Env
impl UnwindSafe for Env
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
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> WithSubscriber for T
impl<T> WithSubscriber for T
§fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where
S: Into<Dispatch>,
§fn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Layout§
Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...)
attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.
Size: 144 bytes