Sha256: 56c8405acdd82b843dae3a9b4d4e98adf47881f6deee6ed2e71cbdb91ba8379d

Contents?: true

Size: 1.55 KB

Versions: 6

Compression:

Stored size: 1.55 KB

Contents

use crate::io::AsyncSeek;

use pin_project_lite::pin_project;
use std::future::Future;
use std::io::{self, SeekFrom};
use std::marker::PhantomPinned;
use std::pin::Pin;
use std::task::{ready, Context, Poll};

pin_project! {
    /// Future for the [`seek`](crate::io::AsyncSeekExt::seek) method.
    #[derive(Debug)]
    #[must_use = "futures do nothing unless you `.await` or poll them"]
    pub struct Seek<'a, S: ?Sized> {
        seek: &'a mut S,
        pos: Option<SeekFrom>,
        // Make this future `!Unpin` for compatibility with async trait methods.
        #[pin]
        _pin: PhantomPinned,
    }
}

pub(crate) fn seek<S>(seek: &mut S, pos: SeekFrom) -> Seek<'_, S>
where
    S: AsyncSeek + ?Sized + Unpin,
{
    Seek {
        seek,
        pos: Some(pos),
        _pin: PhantomPinned,
    }
}

impl<S> Future for Seek<'_, S>
where
    S: AsyncSeek + ?Sized + Unpin,
{
    type Output = io::Result<u64>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let me = self.project();
        match me.pos {
            Some(pos) => {
                // ensure no seek in progress
                ready!(Pin::new(&mut *me.seek).poll_complete(cx))?;
                match Pin::new(&mut *me.seek).start_seek(*pos) {
                    Ok(()) => {
                        *me.pos = None;
                        Pin::new(&mut *me.seek).poll_complete(cx)
                    }
                    Err(e) => Poll::Ready(Err(e)),
                }
            }
            None => Pin::new(&mut *me.seek).poll_complete(cx),
        }
    }
}

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
wasmtime-30.0.2 ./ext/cargo-vendor/tokio-1.43.0/src/io/seek.rs
wasmtime-29.0.0 ./ext/cargo-vendor/tokio-1.43.0/src/io/seek.rs
wasmtime-28.0.0 ./ext/cargo-vendor/tokio-1.43.0/src/io/seek.rs
wasmtime-27.0.0 ./ext/cargo-vendor/tokio-1.41.1/src/io/seek.rs
wasmtime-26.0.0 ./ext/cargo-vendor/tokio-1.41.0/src/io/seek.rs
wasmtime-25.0.2 ./ext/cargo-vendor/tokio-1.40.0/src/io/seek.rs