Skip to main content

WSLCommand

Struct WSLCommand 

Source
pub struct WSLCommand<'a> { /* private fields */ }
Expand description

The WSLCommand struct represents a command that can be executed within the WSL environment. A prepared command to be executed inside WSL.

WSLCommand is a builder-style abstraction around the WSL Plugin API execution functions (ExecuteBinary / ExecuteBinaryInDistribution).

Instances of WSLCommand are created through ApiV1::new_command, ensuring they are always tied to a valid API handle and session.

§Key points

  • The program path is a Linux path (UTF-8, Unix-style) represented by Utf8UnixPath.
  • Arguments are stored as Cow<'a, str> to minimize allocations.
  • argv[0] can be overridden; otherwise it defaults to the program path.
  • The execution target can be the system context or a specific distribution.

§Argument semantics

The underlying WSL Plugin API expects a NULL-terminated argv array:

argv[0] = program name
argv[1..] = user arguments

This type exposes:

§Examples

§Basic execution

let stream = api
    .new_command(SessionID::from(0), "/bin/cat")
    .with_arg("/proc/version")
    .execute()?;

§Overriding argv[0]

Some programs inspect argv[0] to determine their behavior (for example busybox).

let stream = api
    .new_command(session_id, "/bin/busybox")
    .with_arg0("sh")
    .with_args(["-c", "echo hello"])
    .execute()?;

§Executing in a user distribution

let distro: UserDistributionID = "3B6F3C1E-9B4A-4F2C-8E7A-2A9C6D4E1F52".parse().unwrap();
let stream = api
    .new_command(SessionID::from(0), "/bin/echo")
    .with_distribution_id(DistributionID::User(distro))
    .with_arg("hello")
    .execute()?;

§Notes

  • WSLCommand::execute consumes the command and returns a connected TcpStream to the process stdin/stdout.
  • stderr is forwarded to dmesg on the Linux side.
  • This type performs no validation of the Linux path or arguments beyond UTF-8 handling.

Implementations§

Source§

impl<'a> WSLCommand<'a>

Source

pub fn get_path(&self) -> &Utf8UnixPath

Returns the program path as a Utf8UnixPath.

Source

pub fn get_arg0(&self) -> &str

Returns argv[0].

If arg0 has not been overridden, this returns the program path string.

Source

pub const fn is_standard_arg_0(&self) -> bool

Returns true if argv[0] is the default value (the program path).

Source

pub fn reset_arg0(&mut self) -> &mut Self

Resets argv[0] to its default value (the program path).

Source

pub fn arg0<T: Into<Cow<'a, str>>>(&mut self, arg0: T) -> &mut Self

Sets argv[0] (builder-style, by mutable reference).

Source

pub fn with_arg0<T: Into<Cow<'a, str>>>(self, arg0: T) -> Self

Sets argv[0] (builder-style, by value).

Source

pub fn arg<T: Into<Cow<'a, str>>>(&mut self, arg: T) -> &mut Self

Pushes one argument (argv[n], n >= 1), builder-style by mutable reference.

Source

pub fn with_arg<T: Into<Cow<'a, str>>>(self, arg: T) -> Self

Pushes one argument (argv[n], n >= 1), builder-style by value.

Source

pub fn args<I>(&mut self, args: I) -> &mut Self
where I: IntoIterator, I::Item: Into<Cow<'a, str>>,

Extends arguments from an iterator, builder-style by mutable reference.

Source

pub fn with_args<I>(self, args: I) -> Self
where I: IntoIterator, I::Item: Into<Cow<'a, str>>,

Extends arguments from an iterator, builder-style by value.

Source

pub fn get_args(&self) -> impl ExactSizeIterator<Item = &str>

Returns an iterator over user-provided arguments (argv[1..]).

Source

pub fn argv(&self) -> impl Iterator<Item = &str>

Returns an iterator over the full argv (argv[0] + argv[1..]).

Source

pub fn clear_args(&mut self) -> &mut Self

Clears user-provided arguments (argv[1..]).

Source

pub fn truncate_args(&mut self, i: usize) -> &mut Self

Truncates user-provided arguments (argv[1..]) to length i.

Source

pub fn distribution_id(&mut self, distribution_id: DistributionID) -> &mut Self

Sets the distribution target (builder-style by mutable reference).

Source

pub fn with_distribution_id(self, distribution_id: DistributionID) -> Self

Sets the distribution target (builder-style by value).

Source

pub fn reset_distribution_id(&mut self) -> &mut Self

Resets the distribution target to DistributionID::System.

Source

pub const fn get_distribution_id(&self) -> DistributionID

Returns the current distribution target.

Source

pub fn prepare(&self) -> PreparedWSLCommand<'a>

Prepare a WSLCommand for execution, encoding the path and arguments into C-compatible formats.

Trait Implementations§

Source§

impl<'a> Clone for WSLCommand<'a>

Source§

fn clone(&self) -> WSLCommand<'a>

Returns a duplicate 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<'a> Debug for WSLCommand<'a>

Source§

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

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

impl<'a> From<&WSLCommand<'a>> for PreparedWSLCommand<'a>

Source§

fn from(value: &WSLCommand<'a>) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<WSLCommand<'a>> for PreparedWSLCommand<'a>

Source§

fn from(value: WSLCommand<'a>) -> Self

Converts to this type from the input type.
Source§

impl WSLCommandExecution for WSLCommand<'_>

Source§

fn execute(&self) -> ApiResult<TcpStream>

Executes the command via the underlying WSL Plugin API. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for WSLCommand<'a>

§

impl<'a> RefUnwindSafe for WSLCommand<'a>

§

impl<'a> Send for WSLCommand<'a>

§

impl<'a> Sync for WSLCommand<'a>

§

impl<'a> Unpin for WSLCommand<'a>

§

impl<'a> UnsafeUnpin for WSLCommand<'a>

§

impl<'a> UnwindSafe for WSLCommand<'a>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

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

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

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,

Source§

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>,

Source§

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>,

Source§

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.
Source§

impl<T> WithSubscriber for T

Source§

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
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

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