pub struct ServerEnv { /* private fields */ }Expand description
Environment variable overrides for a server process.
Holds at most one key per variable as the host compares names (exactly on
Unix, case-insensitively as Windows compares names): the table is indexed by the
host’s comparison of the name, so a lookup or a write cannot meet two
spellings of one variable and mcpls resolves the same value the child
process sees. The host is fixed at construction, so a table built for one
host cannot be read or extended as another. Keys come in through
Self::from_entries, which rejects collisions, or Self::insert,
which replaces them. Neither lets the table outgrow what the host can pass
to a process: HostOs::max_env_entry_bytes for one entry and
MAX_ENV_TOTAL_BYTES for all.
The host is not serialized: a deserialized table is always for
HostOs::CURRENT, and converting into a map drops it. Other hosts exist
so tests can model them (Windows name folding on any build).
§Examples
use mcpls_core::config::{EnvKey, EnvValue, HostOs, ServerEnv};
let mut env = ServerEnv::new(HostOs::Windows);
env.insert(EnvKey::new("Path").unwrap(), EnvValue::new("/a").unwrap())?;
assert_eq!(env.get("PATH"), Some("/a"));
env.insert(EnvKey::new("PATH").unwrap(), EnvValue::new("/b").unwrap())?;
assert_eq!(env.len(), 1);
let mut other = ServerEnv::new(HostOs::Other);
other.insert(EnvKey::new("Path").unwrap(), EnvValue::new("/a").unwrap())?;
assert_eq!(other.get("PATH"), None);Implementations§
Source§impl ServerEnv
impl ServerEnv
Sourcepub const fn new(host: HostOs) -> Self
pub const fn new(host: HostOs) -> Self
An empty table whose names compare as host compares them.
Sourcepub fn from_entries(
entries: impl IntoIterator<Item = (EnvKey, EnvValue)>,
host: HostOs,
) -> Result<Self, InvalidServerEnv>
pub fn from_entries( entries: impl IntoIterator<Item = (EnvKey, EnvValue)>, host: HostOs, ) -> Result<Self, InvalidServerEnv>
Builds the table from entries, as host compares names.
§Errors
InvalidServerEnv::Duplicate when two keys name one variable on
host, InvalidServerEnv::TooLarge when an entry or the table is
too large to pass to a process there.
Sourcepub fn get(&self, key: &str) -> Option<&str>
pub fn get(&self, key: &str) -> Option<&str>
The value of key, looked up as the table’s host compares names.
Sourcepub fn contains_key(&self, key: &str) -> bool
pub fn contains_key(&self, key: &str) -> bool
Whether key is set, as the table’s host compares names.
Sourcepub fn insert(
&mut self,
key: EnvKey,
value: EnvValue,
) -> Result<(), EnvTooLarge>
pub fn insert( &mut self, key: EnvKey, value: EnvValue, ) -> Result<(), EnvTooLarge>
Sets key, replacing the spelling of it that the table’s host treats
as the same variable.
§Errors
EnvTooLarge when the entry is over HostOs::max_env_entry_bytes
or the table would be over MAX_ENV_TOTAL_BYTES, the sizes past which
a spawn fails with E2BIG; the table is left as it was.
Trait Implementations§
Source§impl Default for ServerEnv
An empty table for HostOs::CURRENT.
impl Default for ServerEnv
An empty table for HostOs::CURRENT.
impl Eq for ServerEnv
Source§impl From<ServerEnv> for BTreeMap<EnvKey, String>
The entries without the table’s host, which is not serialized.
impl From<ServerEnv> for BTreeMap<EnvKey, String>
The entries without the table’s host, which is not serialized.
impl StructuralPartialEq for ServerEnv
Auto Trait Implementations§
impl Freeze for ServerEnv
impl RefUnwindSafe for ServerEnv
impl Send for ServerEnv
impl Sync for ServerEnv
impl Unpin for ServerEnv
impl UnsafeUnpin for ServerEnv
impl UnwindSafe for ServerEnv
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§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.