Environment v0.1.0

Source
Reading, setting and removing environment variables.

EnvironmentVariable

pub struct EnvironmentVariable {
    pub name: OsStringView;
    pub value: OsStringView;
}

One environment variable: its name and its value. Copy. Both views borrow storage the process owns for its whole life.

Source

Fields

NameTypeDescription
nameOsStringViewThe name, the units before the first =.
valueOsStringViewThe value, every unit after the first =, which may itself contain = and may be empty.

EnvironmentIterator

pub struct EnvironmentIterator {
    // private fields
}

A walk over the environment variables: those the process started with, in the order the system holds them, then those set since. Copy, holding only a position, so it allocates nothing.

Source

Methods

Iterate

pub func Iterate(self: &EnvironmentIterator) -> EnvironmentIterator

The walk itself, so that for variable in Environment() reads every variable.

Returns: a copy of this walk from its current position

Source

Implements Iterator

EnvironmentIterator conforms to Iterator.

Next

pub func Next(self: &var EnvironmentIterator) -> EnvironmentVariable?

The next variable, or none after the last.

Returns: the variable's name and value

Source

Environment

pub func Environment() -> EnvironmentIterator

Every environment variable of the process. Cannot fail and allocates nothing.

Source

Returns: a walk over the environment

Variable

pub func Variable(name: char8[..]) -> OsStringView?

The value of the environment variable name, or none when it is not set. name is UTF-8 text. It is matched exactly on the Unix systems and without regard to ASCII case on Windows, which is how each system matches names. A variable set or removed with SetVariable or RemoveVariable reads as it was last changed. When the starting environment names a variable twice, the first is the one returned, as the C library's getenv returns it. A variable set to the empty string is present, with an empty value. The view borrows storage the process owns for its whole life. Runs in time linear in the size of the environment and allocates nothing.

Source

Parameters

NameTypeDescription
namechar8[..]the variable's name

Returns: the variable's value, or none when no variable has that name

SetVariable

pub func SetVariable(
    name: char8[..],
    value: char8[..]
) -> ! IoError

Sets the environment variable name to value, for this process and every process it starts from now on. Both are UTF-8 text. The change is seen by Variable and Environment, by Command, and on Windows by the system; the C library's own environment on the Unix systems is not changed. Views handed out before stay valid.

Source

Parameters

NameTypeDescription
namechar8[..]the variable's name, which may not be empty or contain = or NUL
valuechar8[..]its new value, which may not contain NUL

Returns: nothing, or fails with InvalidInput for a name or value the environment cannot hold

RemoveVariable

pub func RemoveVariable(name: char8[..]) -> ! IoError

Removes the environment variable name, for this process and every process it starts from now on. Removing a variable that is not set does nothing. Views of its value handed out before stay valid.

Source

Parameters

NameTypeDescription
namechar8[..]the variable's name, which may not be empty or contain = or NUL

Returns: nothing, or fails with InvalidInput for a name the environment cannot hold