]>
git.proxmox.com Git - rustc.git/blob - vendor/tokio/src/io/util/async_seek_ext.rs
1 use crate::io
::seek
::{seek, Seek}
;
2 use crate::io
::AsyncSeek
;
6 /// An extension trait that adds utility methods to [`AsyncSeek`] types.
11 /// use std::io::{self, Cursor, SeekFrom};
12 /// use tokio::io::{AsyncSeekExt, AsyncReadExt};
15 /// async fn main() -> io::Result<()> {
16 /// let mut cursor = Cursor::new(b"abcdefg");
18 /// // the `seek` method is defined by this trait
19 /// cursor.seek(SeekFrom::Start(3)).await?;
21 /// let mut buf = [0; 1];
22 /// let n = cursor.read(&mut buf).await?;
24 /// assert_eq!(buf, [b'd']);
30 /// See [module][crate::io] documentation for more details.
32 /// [`AsyncSeek`]: AsyncSeek
33 pub trait AsyncSeekExt
: AsyncSeek
{
34 /// Creates a future which will seek an IO object, and then yield the
35 /// new position in the object and the object itself.
40 /// async fn seek(&mut self, pos: SeekFrom) -> io::Result<u64>;
43 /// In the case of an error the buffer and the object will be discarded, with
44 /// the error yielded.
49 /// use tokio::fs::File;
50 /// use tokio::io::{AsyncSeekExt, AsyncReadExt};
52 /// use std::io::SeekFrom;
54 /// # async fn dox() -> std::io::Result<()> {
55 /// let mut file = File::open("foo.txt").await?;
56 /// file.seek(SeekFrom::Start(6)).await?;
58 /// let mut contents = vec![0u8; 10];
59 /// file.read_exact(&mut contents).await?;
63 fn seek(&mut self, pos
: SeekFrom
) -> Seek
<'_
, Self>
70 /// Creates a future which will return the current seek position from the
71 /// start of the stream.
73 /// This is equivalent to `self.seek(SeekFrom::Current(0))`.
74 fn stream_position(&mut self) -> Seek
<'_
, Self>
78 self.seek(SeekFrom
::Current(0))
83 impl<S
: AsyncSeek
+ ?Sized
> AsyncSeekExt
for S {}