Process

Struct Process 

Source
pub struct Process { /* private fields */ }
Expand description

A builder for configuring and spawning a process.

This provides an ergonomic API for spawning processes while keeping the stream type (broadcast vs single subscriber) explicit at the spawn callsite.

§Examples

use tokio_process_tools::*;
use tokio::process::Command;

// Simple case with auto-derived name
let process = Process::new(Command::new("ls"))
    .spawn_broadcast()?;

// With explicit name (no allocation when using string literal)
let process = Process::new(Command::new("server"))
    .name("my-server")
    .spawn_single_subscriber()?;

// With custom capacities
let process = Process::new(Command::new("cargo"))
    .name("test-runner")
    .stdout_capacity(512)
    .stderr_capacity(512)
    .spawn_broadcast()?;

Implementations§

Source§

impl Process

Source

pub fn new(cmd: Command) -> Self

Creates a new process builder from a tokio command.

If no name is explicitly set via Process::name, the name will be auto-derived from the command’s program name.

§Examples
use tokio_process_tools::*;
use tokio::process::Command;

let process = Process::new(Command::new("ls"))
    .spawn_broadcast()?;
Source

pub fn name(self, name: impl Into<ProcessName>) -> Self

Sets how the process should be named.

You can provide either an explicit name or configure automatic name generation. The name is used for logging and debugging purposes.

§Examples
use tokio_process_tools::*;
use tokio::process::Command;

// Explicit name
let process = Process::new(Command::new("server"))
    .name(ProcessName::Explicit("my-server".into()))
    .spawn_broadcast()?;

// Auto-generated with arguments
let mut cmd = Command::new("cargo");
cmd.arg("test");
let process = Process::new(cmd)
    .name(ProcessName::Auto(AutoName::Using(AutoNameSettings::program_with_args())))
    .spawn_broadcast()?;
Source

pub fn with_name(self, name: impl Into<Cow<'static, str>>) -> Self

Convenience method to set an explicit process name.

This is a shorthand for .name(ProcessName::Explicit(...)).

§Examples
use tokio_process_tools::*;
use tokio::process::Command;

// Static name (no allocation)
let process = Process::new(Command::new("server"))
    .with_name("my-server")
    .spawn_broadcast()?;

// Dynamic name (allocates)
let id = 42;
let process = Process::new(Command::new("worker"))
    .with_name(format!("worker-{id}"))
    .spawn_single_subscriber()?;
Source

pub fn with_auto_name(self, mode: AutoName) -> Self

Convenience method to configure automatic name generation.

This is a shorthand for .name(ProcessName::Auto(...)).

§Examples
use tokio_process_tools::*;
use tokio::process::Command;

let mut cmd = Command::new("server");
cmd.arg("--database").arg("sqlite");
cmd.env("S3_ENDPOINT", "127.0.0.1:9000");

let process = Process::new(cmd)
    .with_auto_name(AutoName::Using(AutoNameSettings::program_with_env_and_args()))
    .spawn_broadcast()?;
Source

pub fn stdout_chunk_size(self, chunk_size: NumBytes) -> Self

Sets the stdout chunk size.

This controls the size of the buffer used when reading from the process’s stdout stream. Default is DEFAULT_CHUNK_SIZE.

§Examples
use tokio_process_tools::*;
use tokio::process::Command;

let process = Process::new(Command::new("server"))
    .stdout_chunk_size(32.kilobytes())
    .spawn_broadcast()?;
Source

pub fn stderr_chunk_size(self, chunk_size: NumBytes) -> Self

Sets the stderr chunk size.

This controls the size of the buffer used when reading from the process’s stderr stream. Default is DEFAULT_CHUNK_SIZE.

§Examples
use tokio_process_tools::*;
use tokio::process::Command;

let process = Process::new(Command::new("server"))
    .stderr_chunk_size(32.kilobytes())
    .spawn_broadcast()?;
Source

pub fn chunk_sizes(self, chunk_size: NumBytes) -> Self

Sets the stdout and stderr chunk sizes.

This controls the size of the buffers used when reading from the process’s stdout and stderr streams. Default is DEFAULT_CHUNK_SIZE.

§Examples
use tokio_process_tools::*;
use tokio::process::Command;

let process = Process::new(Command::new("server"))
    .chunk_sizes(32.kilobytes())
    .spawn_broadcast()?;
Source

pub fn stdout_capacity(self, capacity: usize) -> Self

Sets the stdout channel capacity.

This controls how many chunks can be buffered before backpressure is applied. Default is DEFAULT_CHANNEL_CAPACITY.

§Examples
use tokio_process_tools::*;
use tokio::process::Command;

let process = Process::new(Command::new("server"))
    .stdout_capacity(512)
    .spawn_broadcast()?;
Source

pub fn stderr_capacity(self, capacity: usize) -> Self

Sets the stderr channel capacity.

This controls how many chunks can be buffered before backpressure is applied. Default is DEFAULT_CHANNEL_CAPACITY.

§Examples
use tokio_process_tools::*;
use tokio::process::Command;

let process = Process::new(Command::new("server"))
    .stderr_capacity(256)
    .spawn_broadcast()?;
Source

pub fn capacities(self, capacity: usize) -> Self

Sets the stdout and stderr channel capacity.

This controls how many chunks can be buffered before backpressure is applied. Default is DEFAULT_CHANNEL_CAPACITY.

§Examples
use tokio_process_tools::*;
use tokio::process::Command;

let process = Process::new(Command::new("server"))
    .capacities(256)
    .spawn_broadcast()?;
Source

pub fn spawn_broadcast( self, ) -> Result<ProcessHandle<BroadcastOutputStream>, SpawnError>

Spawns the process with broadcast output streams.

Broadcast streams support multiple concurrent consumers of stdout/stderr, which is useful when you need to inspect, collect, and process output simultaneously. This comes with slightly higher memory overhead due to cloning.

§Examples
use tokio_process_tools::*;
use tokio::process::Command;

let mut process = Process::new(Command::new("ls"))
    .spawn_broadcast()?;

// Multiple consumers can read the same output
let _logger = process.stdout().inspect_lines(|line| {
    println!("{}", line);
    tokio_process_tools::Next::Continue
}, Default::default());

let _collector = process.stdout().collect_lines_into_vec(Default::default());
Source

pub fn spawn_single_subscriber( self, ) -> Result<ProcessHandle<SingleSubscriberOutputStream>, SpawnError>

Spawns the process with single subscriber output streams.

Single subscriber streams are more efficient (lower memory, no cloning) but only allow one consumer of stdout/stderr at a time. Use this when you only need to either inspect OR collect output, not both simultaneously.

§Examples
use tokio_process_tools::*;
use tokio::process::Command;

let process = Process::new(Command::new("ls"))
    .spawn_single_subscriber()?;

// Only one consumer allowed
let collector = process.stdout().collect_lines_into_vec(Default::default());

Auto Trait Implementations§

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.

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