[][src]Struct tokio_file_unix::File

pub struct File<F> { /* fields omitted */ }

Wraps file-like objects for asynchronous I/O.

Normally, you should use File::new_nb rather than File::raw_new unless the underlying file descriptor has already been set to nonblocking mode. Using a file descriptor that is not in nonblocking mode for asynchronous I/O will lead to subtle and confusing bugs.

Wrapping regular files has no effect because they do not support nonblocking mode.

The most common instantiation of this type is File<std::fs::File>, which indirectly provides the following trait implementation:

This example is not tested
impl AsyncRead + AsyncWrite for PollEvented<File<std::fs::File>>;

Example: read standard input line by line

use tokio::stream::StreamExt;
use tokio_util::codec::FramedRead;
use tokio_util::codec::LinesCodec;

#[tokio::main]
async fn main() -> std::io::Result<()> {
    // convert stdin into a nonblocking file;
    // this is the only part that makes use of tokio_file_unix
    let file = tokio_file_unix::raw_stdin()?;
    let file = tokio_file_unix::File::new_nb(file)?;

    let mut framed = FramedRead::new(file, LinesCodec::new());

    while let Some(got) = framed.next().await {
        println!("Got this: {:?}", got);
    }

    println!("Received None, lol");
    Ok(())
}

Example: unsafe creation from raw file descriptor

To unsafely create File<F> from a raw file descriptor fd, you can do something like:

use std::os::unix::io::FromRawFd;

let file = tokio_file_unix::File::new_nb(F::from_raw_fd(fd))?;

which will enable nonblocking mode upon creation. The choice of F is critical: it determines the ownership semantics of the file descriptor. For example, if you choose F = std::fs::File, the file descriptor will be closed when the File is dropped.

Implementations

impl<F: AsRawFd> File<F>[src]

pub fn new_nb(file: F) -> Result<PollEvented<Self>>[src]

Wraps a file-like object into a pollable object that supports tokio::io::AsyncRead and tokio::io::AsyncWrite, and also enables nonblocking mode on the underlying file descriptor.

pub fn raw_new(file: F) -> Result<PollEvented<Self>>[src]

Raw constructor that does not enable nonblocking mode on the underlying file descriptor. This constructor should only be used if you are certain that the underlying file descriptor is already in nonblocking mode.

Trait Implementations

impl<F: AsRawFd> AsRawFd for File<F>[src]

impl<F: Debug> Debug for File<F>[src]

impl<F: AsRawFd> Evented for File<F>[src]

impl<F: Read> Read for File<F>[src]

impl<F: Seek> Seek for File<F>[src]

impl<F: Write> Write for File<F>[src]

Auto Trait Implementations

impl<F> !RefUnwindSafe for File<F>

impl<F> Send for File<F> where
    F: Send

impl<F> !Sync for File<F>

impl<F> Unpin for File<F> where
    F: Unpin

impl<F> !UnwindSafe for File<F>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.