tokio::net::unix::pipe

Struct OpenOptions

Source
pub struct OpenOptions { /* private fields */ }
Available on Unix and crate feature net only.
Expand description

Options and flags which can be used to configure how a FIFO file is opened.

This builder allows configuring how to create a pipe end from a FIFO file. Generally speaking, when using OpenOptions, you’ll first call new, then chain calls to methods to set each option, then call either open_receiver or open_sender, passing the path of the FIFO file you are trying to open. This will give you a io::Result with a pipe end inside that you can further operate on.

§Examples

Opening a pair of pipe ends from a FIFO file:

use tokio::net::unix::pipe;

const FIFO_NAME: &str = "path/to/a/fifo";

let rx = pipe::OpenOptions::new().open_receiver(FIFO_NAME)?;
let tx = pipe::OpenOptions::new().open_sender(FIFO_NAME)?;

Opening a Sender on Linux when you are sure the file is a FIFO:

use tokio::net::unix::pipe;
use nix::{unistd::mkfifo, sys::stat::Mode};

// Our program has exclusive access to this path.
const FIFO_NAME: &str = "path/to/a/new/fifo";

mkfifo(FIFO_NAME, Mode::S_IRWXU)?;
let tx = pipe::OpenOptions::new()
    .read_write(true)
    .unchecked(true)
    .open_sender(FIFO_NAME)?;

Implementations§

Source§

impl OpenOptions

Source

pub fn new() -> OpenOptions

Creates a blank new set of options ready for configuration.

All options are initially set to false.

Source

pub fn read_write(&mut self, value: bool) -> &mut Self

Available on Linux only.

Sets the option for read-write access.

This option, when true, will indicate that a FIFO file will be opened in read-write access mode. This operation is not defined by the POSIX standard and is only guaranteed to work on Linux.

§Examples

Opening a Sender even if there are no open reading ends:

use tokio::net::unix::pipe;

let tx = pipe::OpenOptions::new()
    .read_write(true)
    .open_sender("path/to/a/fifo");

Opening a resilient Receiver i.e. a reading pipe end which will not fail with UnexpectedEof during reading if all writing ends of the pipe close the FIFO file.

use tokio::net::unix::pipe;

let tx = pipe::OpenOptions::new()
    .read_write(true)
    .open_receiver("path/to/a/fifo");
Source

pub fn unchecked(&mut self, value: bool) -> &mut Self

Sets the option to skip the check for FIFO file type.

By default, open_receiver and open_sender functions will check if the opened file is a FIFO file. Set this option to true if you are sure the file is a FIFO file.

§Examples
use tokio::net::unix::pipe;
use nix::{unistd::mkfifo, sys::stat::Mode};

// Our program has exclusive access to this path.
const FIFO_NAME: &str = "path/to/a/new/fifo";

mkfifo(FIFO_NAME, Mode::S_IRWXU)?;
let rx = pipe::OpenOptions::new()
    .unchecked(true)
    .open_receiver(FIFO_NAME)?;
Source

pub fn open_receiver<P: AsRef<Path>>(&self, path: P) -> Result<Receiver>

Creates a Receiver from a FIFO file with the options specified by self.

This function will open the FIFO file at the specified path, possibly check if it is a pipe, and associate the pipe with the default event loop for reading.

§Errors

If the file type check fails, this function will fail with io::ErrorKind::InvalidInput. This function may also fail with other standard OS errors.

§Panics

This function panics if it is not called from within a runtime with IO enabled.

The runtime is usually set implicitly when this function is called from a future driven by a tokio runtime, otherwise runtime can be set explicitly with Runtime::enter function.

Source

pub fn open_sender<P: AsRef<Path>>(&self, path: P) -> Result<Sender>

Creates a Sender from a FIFO file with the options specified by self.

This function will open the FIFO file at the specified path, possibly check if it is a pipe, and associate the pipe with the default event loop for writing.

§Errors

If the file type check fails, this function will fail with io::ErrorKind::InvalidInput. If the file is not opened in read-write access mode and the file is not currently open for reading, this function will fail with ENXIO. This function may also fail with other standard OS errors.

§Panics

This function panics if it is not called from within a runtime with IO enabled.

The runtime is usually set implicitly when this function is called from a future driven by a tokio runtime, otherwise runtime can be set explicitly with Runtime::enter function.

Trait Implementations§

Source§

impl Clone for OpenOptions

Source§

fn clone(&self) -> OpenOptions

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 OpenOptions

Source§

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

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

impl Default for OpenOptions

Source§

fn default() -> OpenOptions

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

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

Source§

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

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

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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.

Layout§

Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...) attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.

Size: 2 bytes