/rust/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.46.1/src/io/seek.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use crate::io::AsyncSeek; |
2 | | |
3 | | use pin_project_lite::pin_project; |
4 | | use std::future::Future; |
5 | | use std::io::{self, SeekFrom}; |
6 | | use std::marker::PhantomPinned; |
7 | | use std::pin::Pin; |
8 | | use std::task::{ready, Context, Poll}; |
9 | | |
10 | | pin_project! { |
11 | | /// Future for the [`seek`](crate::io::AsyncSeekExt::seek) method. |
12 | | #[derive(Debug)] |
13 | | #[must_use = "futures do nothing unless you `.await` or poll them"] |
14 | | pub struct Seek<'a, S: ?Sized> { |
15 | | seek: &'a mut S, |
16 | | pos: Option<SeekFrom>, |
17 | | // Make this future `!Unpin` for compatibility with async trait methods. |
18 | | #[pin] |
19 | | _pin: PhantomPinned, |
20 | | } |
21 | | } |
22 | | |
23 | 0 | pub(crate) fn seek<S>(seek: &mut S, pos: SeekFrom) -> Seek<'_, S> |
24 | 0 | where |
25 | 0 | S: AsyncSeek + ?Sized + Unpin, |
26 | 0 | { |
27 | 0 | Seek { |
28 | 0 | seek, |
29 | 0 | pos: Some(pos), |
30 | 0 | _pin: PhantomPinned, |
31 | 0 | } |
32 | 0 | } |
33 | | |
34 | | impl<S> Future for Seek<'_, S> |
35 | | where |
36 | | S: AsyncSeek + ?Sized + Unpin, |
37 | | { |
38 | | type Output = io::Result<u64>; |
39 | | |
40 | 0 | fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
41 | 0 | let me = self.project(); |
42 | 0 | match me.pos { |
43 | 0 | Some(pos) => { |
44 | | // ensure no seek in progress |
45 | 0 | ready!(Pin::new(&mut *me.seek).poll_complete(cx))?; |
46 | 0 | match Pin::new(&mut *me.seek).start_seek(*pos) { |
47 | | Ok(()) => { |
48 | 0 | *me.pos = None; |
49 | 0 | Pin::new(&mut *me.seek).poll_complete(cx) |
50 | | } |
51 | 0 | Err(e) => Poll::Ready(Err(e)), |
52 | | } |
53 | | } |
54 | 0 | None => Pin::new(&mut *me.seek).poll_complete(cx), |
55 | | } |
56 | 0 | } |
57 | | } |