Struct viro::Viro

source ·
pub struct Viro { /* private fields */ }
Expand description

An environment.

This uses a hard-coded set of environment variables to determine whether a variable should be a “scalar”, having a singular value, or a “vector”, having multiple values. It also checks what separators are used for particular variables, and excludes certain variables which are not intended to be passed around.

For example, PATH is a vector which separates its values using semicolons on Windows and colons on other platforms; and the SHELL variable is one that should not be included.

The currently known variables are:

KeyBehavior
_Excluded
GOPATHSeparated by colons
GOPATHSeparated by colons
GPG_TTYExcluded
HOMEExcluded
LD_LIBRARY_PATHSeparated by colons; may also be separated by semicolons
LD_PRELOADSeparated by colons; may also be separated by spaces
LS_COLORSTerminated by colons
MANPATHSeparated by colons
NOTIFY_SOCKETExcluded
PATHSeparated by semicolons on windows, colons on other platforms
PERL5LIBSeparated by colons
PERLLIBSeparated by colons
PWDExcluded
PYTHONPATHSeparated by colons
SHELLExcluded
SHLVLExcluded
USERExcluded

Implementations§

source§

impl Viro

source

pub fn new() -> Viro

Makes an empty environment.

§Examples
use viro::Viro;

let mut env = Viro::new();

let mut ser = env.write_bytes();
assert_eq!(ser, &[]);
source

pub fn insert<S: AsRef<[u8]> + Into<Vec<u8>>, T: AsRef<[u8]> + Into<Vec<u8>>>( &mut self, key: S, val: T )

Dumps a key-value pair into the environment.

This does not perform any validation on the keys or values, accepting all possible byte sequences. If the key contains a \x3D byte (equals sign), or the key or value contain a \0 (NUL) byte, the resulting serialization will be incorrect.

§Examples
use viro::Viro;

let mut env = Viro::new();
env.insert("PATH", "/bin:/sbin");
env.insert("PATH", "/sbin:/bin:/usr/bin");
env.insert("SHELL", "/bin/bash");

let ser = env.write_bytes();
assert_eq!(ser, &b"PATH=/bin:/sbin:/usr/bin\0"[..]);
source

pub fn remove<S: AsRef<[u8]>>(&mut self, key: S)

Removes a key-value pair from the environment.

This completely removes the key from the serialized format.

§Examples
use viro::Viro;

let mut env = Viro::new();
env.insert("PATH", "/bin:/sbin");
env.remove("PATH");
env.insert("PATH", "/sbin:/bin:/usr/bin");

let ser = env.write_bytes();
assert_eq!(ser, &b"PATH=/sbin:/bin:/usr/bin\0"[..]);
source

pub fn process<S: AsRef<[u8]>>(&mut self, item: S)

Processes a serialized item.

This is equivalent to insert if the item contains an equals sign, and remove if the item does not contain an equal sign.

Like insert, this does not check for NUL bytes in the item, and including them will result in the serialization being incorrect.

§Examples
use viro::Viro;

let mut env = Viro::new();
env.process("PATH=/bin:/sbin");
env.process("PATH");
env.process("PATH=/sbin:/bin:/usr/bin");

let ser = env.write_bytes();
assert_eq!(ser, &b"PATH=/sbin:/bin:/usr/bin\0"[..]);
source

pub fn read<R: ?Sized + SyncBufRead>(&mut self, reader: &mut R) -> Result<()>

Available on crate feature sync only.

Reads an environment from a reader.

The interprets the input as a NUL-delimited list. If an item contains an equal sign, it is treated as a key-value pair to be included in the environment. If it does not, it is treated as a variable to be removed.

§Examples
use viro::Viro;

let mut env = Viro::new();
let mut data = &b"PATH=/bin:/sbin\0PATH\0PATH=/sbin:/bin:/usr/bin"[..];
env.read(&mut data)?;

let ser = env.write_bytes();
assert_eq!(ser, &b"PATH=/sbin:/bin:/usr/bin\0"[..]);
source

pub async fn read_async<R: ?Sized + AsyncBufRead>( &mut self, reader: Pin<&mut R> ) -> Result<()>

Available on crate feature async only.

Async version of read.

§Examples
use std::pin::Pin;
use viro::Viro;

let mut env = Viro::new();
let mut data = &b"PATH=/bin:/sbin\0PATH\0PATH=/sbin:/bin:/usr/bin"[..];
env.read_async(Pin::new(&mut data)).await?;

let ser = env.write_bytes();
assert_eq!(ser, &b"PATH=/sbin:/bin:/usr/bin\0"[..]);
source

pub fn read_bytes(&mut self, bytes: &[u8])

Reads the environment from a byte slice.

§Examples
use viro::Viro;

let mut env = Viro::new();
let mut data = &b"PATH=/bin:/sbin\0PATH\0PATH=/sbin:/bin:/usr/bin"[..];
env.read_bytes(data);

let ser = env.write_bytes();
assert_eq!(ser, &b"PATH=/sbin:/bin:/usr/bin\0"[..]);
source

pub fn read_stdin(&mut self) -> Result<()>

Available on crate feature sync-stdio only.

Reads the environment from stdin.

§Examples
use viro::Viro;

let mut env = Viro::new();
env.read_stdin()?;
source

pub async fn read_stdin_async(&mut self) -> Result<()>

Available on crate feature async-stdio only.

Async version of read_stdin.

§Examples
use viro::Viro;

let mut env = Viro::new();
env.read_stdin_async().await?;
source

pub fn serialized(&mut self) -> ViroIter<'_>

Creates an iterator over the serialized byte-slices of an environment.

This returns an iterator to avoid allocating any new memory; simply concatenate the values returned by this iterator to get the full serialized version. Sorts the variables in byte order before serializing.

The format is exactly the same as the one for read, although every item in the resulting NUL-delimited list will contain an equals sign; variables that would be removed are simply omitted.

The actual separation of the output may change in the future, although this is unlikely. You should only rely on the concatenation of the iterands, and not their values.

§Examples
use viro::Viro;

let mut env = Viro::new();
env.insert("PATH", "/bin:/sbin:/usr/bin");
env.insert("SHELL", "/bin/bash");
env.insert("LD_LIBRARY_PATH", "/lib");

let mut ser = Vec::new();
for slice in env.serialized() {
    ser.extend_from_slice(slice);
}
assert_eq!(ser, &b"LD_LIBRARY_PATH=/lib\0PATH=/bin:/sbin:/usr/bin\0"[..]);
source

pub fn write<W: ?Sized + SyncWrite>(&mut self, writer: &mut W) -> Result<()>

Available on crate feature sync only.

Writes the environment to a writer and flushes it.

§Examples
use viro::Viro;

let mut env = Viro::new();
env.insert("PATH", "/bin:/sbin:/usr/bin");
env.insert("SHELL", "/bin/bash");

let mut ser = Vec::new();
env.write(&mut ser)?;
assert_eq!(ser, &b"PATH=/bin:/sbin:/usr/bin\0"[..]);
source

pub async fn write_async<'w, W: ?Sized + AsyncWrite>( &'w mut self, writer: Pin<&'w mut W> ) -> Result<()>

Available on crate feature async only.

Async version of write.

§Examples
use std::pin::Pin;
use viro::Viro;

let mut env = Viro::new();
env.insert("PATH", "/bin:/sbin:/usr/bin");
env.insert("SHELL", "/bin/bash");

let mut ser = Vec::new();
env.write_async(Pin::new(&mut ser)).await?;
assert_eq!(ser, &b"PATH=/bin:/sbin:/usr/bin\0"[..]);
source

pub fn write_bytes(&mut self) -> Vec<u8>

Writes the environment to a byte vector.

§Examples
use viro::Viro;

let mut env = Viro::new();
env.insert("PATH", "/bin:/sbin:/usr/bin");
env.insert("SHELL", "/bin/bash");

let mut ser = env.write_bytes();
assert_eq!(ser, &b"PATH=/bin:/sbin:/usr/bin\0"[..]);
source

pub fn write_stdout(&mut self) -> Result<()>

Available on crate feature sync-stdio only.

Writes the environment out to stdout.

§Examples
use viro::Viro;

let mut env = Viro::new();
env.insert("PATH", "/bin:/sbin:/usr/bin");
env.insert("SHELL", "/bin/bash");

env.write_stdout()?;
source

pub async fn write_stdout_async(&mut self) -> Result<()>

Available on crate feature async-stdio only.

Async version of write_stdout.

§Examples
use viro::Viro;

let mut env = Viro::new();
env.insert("PATH", "/bin:/sbin:/usr/bin");
env.insert("SHELL", "/bin/bash");

env.write_stdout_async().await?;
source

pub fn write_stderr(&mut self) -> Result<()>

Available on crate feature sync-stdio only.

Writes the environment out to stderr.

§Examples
use viro::Viro;

let mut env = Viro::new();
env.insert("PATH", "/bin:/sbin:/usr/bin");
env.insert("SHELL", "/bin/bash");

env.write_stderr()?;
source

pub async fn write_stderr_async(&mut self) -> Result<()>

Available on crate feature async-stdio only.

Async version of write_stderr.

§Examples
use viro::Viro;

let mut env = Viro::new();
env.insert("PATH", "/bin:/sbin:/usr/bin");
env.insert("SHELL", "/bin/bash");

env.write_stderr_async().await?;

Trait Implementations§

source§

impl Clone for Viro

source§

fn clone(&self) -> Viro

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Viro

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Viro

source§

fn default() -> Viro

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

Auto Trait Implementations§

§

impl RefUnwindSafe for Viro

§

impl Send for Viro

§

impl Sync for Viro

§

impl Unpin for Viro

§

impl UnwindSafe for Viro

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more