[−][src]Struct tokio_file_unix::File
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
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]
pub fn register(
&self,
poll: &Poll,
token: Token,
interest: Ready,
opts: PollOpt
) -> Result<()>
[src]
&self,
poll: &Poll,
token: Token,
interest: Ready,
opts: PollOpt
) -> Result<()>
pub fn reregister(
&self,
poll: &Poll,
token: Token,
interest: Ready,
opts: PollOpt
) -> Result<()>
[src]
&self,
poll: &Poll,
token: Token,
interest: Ready,
opts: PollOpt
) -> Result<()>
pub fn deregister(&self, poll: &Poll) -> Result<()>
[src]
impl<F: Read> Read for File<F>
[src]
pub fn read(&mut self, buf: &mut [u8]) -> Result<usize>
[src]
pub fn read_vectored(
&mut self,
bufs: &mut [IoSliceMut<'_>]
) -> Result<usize, Error>
1.36.0[src]
&mut self,
bufs: &mut [IoSliceMut<'_>]
) -> Result<usize, Error>
pub fn is_read_vectored(&self) -> bool
[src]
pub unsafe fn initializer(&self) -> Initializer
[src]
pub fn read_to_end(&mut self, buf: &mut Vec<u8, Global>) -> Result<usize, Error>
1.0.0[src]
pub fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
1.0.0[src]
pub fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
1.6.0[src]
pub fn by_ref(&mut self) -> &mut Self
1.0.0[src]
pub fn bytes(self) -> Bytes<Self>
1.0.0[src]
pub fn chain<R>(self, next: R) -> Chain<Self, R> where
R: Read,
1.0.0[src]
R: Read,
pub fn take(self, limit: u64) -> Take<Self>
1.0.0[src]
impl<F: Seek> Seek for File<F>
[src]
pub fn seek(&mut self, pos: SeekFrom) -> Result<u64>
[src]
pub fn stream_len(&mut self) -> Result<u64, Error>
[src]
pub fn stream_position(&mut self) -> Result<u64, Error>
[src]
impl<F: Write> Write for File<F>
[src]
pub fn write(&mut self, buf: &[u8]) -> Result<usize>
[src]
pub fn flush(&mut self) -> Result<()>
[src]
pub fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>
1.36.0[src]
pub fn is_write_vectored(&self) -> bool
[src]
pub fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
1.0.0[src]
pub fn write_all_vectored(
&mut self,
bufs: &mut [IoSlice<'_>]
) -> Result<(), Error>
[src]
&mut self,
bufs: &mut [IoSlice<'_>]
) -> Result<(), Error>
pub fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result<(), Error>
1.0.0[src]
pub fn by_ref(&mut self) -> &mut Self
1.0.0[src]
Auto Trait Implementations
impl<F> !RefUnwindSafe for File<F>
impl<F> Send for File<F> where
F: Send,
F: Send,
impl<F> !Sync for File<F>
impl<F> Unpin for File<F> where
F: Unpin,
F: Unpin,
impl<F> !UnwindSafe for File<F>
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,