Skip to main content

Mojo struct

Pipe

struct Pipe

Create a pipe for interprocess communication.

Example usage:

pipe().write_bytes("TEST".as_bytes())

Fields

  • fd_in (Optional[FileDescriptor]): File descriptor for pipe input.
  • fd_out (Optional[FileDescriptor]): File descriptor for pipe output.

Implemented traits

AnyType, ImplicitlyDestructible

comptime members

__del__is_trivial

comptime __del__is_trivial = False

Methods

__init__

__init__(out self, in_close_on_exec: Bool = True, out_close_on_exec: Bool = True)

Initializes a new Pipe.

Args:

  • in_close_on_exec (Bool): Close the read side of pipe if an exec syscall is issued in the process.
  • out_close_on_exec (Bool): Close the write side of pipe if an exec syscall is issued in the process.

Raises:

Error: If the pipe could not be created or configured.

__del__

__del__(deinit self)

Ensures pipes input and output file descriptors are closed, when the object is destroyed.

set_input_only

set_input_only(mut self)

Close the output descriptor/ channel for this side of the pipe.

set_output_only

set_output_only(mut self)

Close the input descriptor/ channel for this side of the pipe.

write_bytes

write_bytes(mut self, bytes: Span[Byte, origin])

Writes a span of bytes to the pipe.

Args:

  • bytes (Span): The byte span to write to this pipe.

Raises:

Error: If called on a read-only pipe.

read_bytes

read_bytes(mut self, buffer: Span[Byte, origin]) -> UInt

Read a number of bytes from this pipe.

Args:

  • buffer (Span): Span[Byte] of length n where to store read bytes. n = number of bytes to read.

Returns:

UInt: Actual number of bytes read.

Raises:

Error: If the pipe is in write-only mode.

Was this page helpful?