pub struct File<F> { /* private fields */ }
Expand description
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:
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§
Source§impl<F: AsRawFd> File<F>
impl<F: AsRawFd> File<F>
Sourcepub fn new_nb(file: F) -> Result<PollEvented<Self>>
pub fn new_nb(file: F) -> Result<PollEvented<Self>>
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.
Sourcepub fn raw_new(file: F) -> Result<PollEvented<Self>>
pub fn raw_new(file: F) -> Result<PollEvented<Self>>
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§
Source§impl<F: AsRawFd> Evented for File<F>
impl<F: AsRawFd> Evented for File<F>
Source§impl<F: Read> Read for File<F>
impl<F: Read> Read for File<F>
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
1.36.0 · Source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read
, except that it reads into a slice of buffers. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf
. Read more1.0.0 · Source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf
. Read more1.6.0 · Source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf
. Read moreSource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)Source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)cursor
. Read more1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read
. Read moreSource§impl<F: Seek> Seek for File<F>
impl<F: Seek> Seek for File<F>
Source§fn seek(&mut self, pos: SeekFrom) -> Result<u64>
fn seek(&mut self, pos: SeekFrom) -> Result<u64>
1.55.0 · Source§fn rewind(&mut self) -> Result<(), Error>
fn rewind(&mut self) -> Result<(), Error>
Source§fn stream_len(&mut self) -> Result<u64, Error>
fn stream_len(&mut self) -> Result<u64, Error>
seek_stream_len
)Source§impl<F: Write> Write for File<F>
impl<F: Write> Write for File<F>
Source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored
)