mirror of
https://github.com/async-rs/async-std.git
synced 2025-01-19 20:13:51 +00:00
fix: Remove Pin API related unsafe code
This commit is contained in:
parent
b17af61367
commit
feeb3c10df
1 changed files with 15 additions and 11 deletions
|
@ -4,22 +4,24 @@ use std::pin::Pin;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use futures_timer::Delay;
|
use futures_timer::Delay;
|
||||||
|
use pin_project_lite::pin_project;
|
||||||
|
|
||||||
use crate::future::Future;
|
use crate::future::Future;
|
||||||
use crate::stream::Stream;
|
use crate::stream::Stream;
|
||||||
use crate::task::{Context, Poll};
|
use crate::task::{Context, Poll};
|
||||||
|
|
||||||
/// A stream with timeout time set
|
pin_project! {
|
||||||
#[derive(Debug)]
|
/// A stream with timeout time set
|
||||||
pub struct Timeout<S: Stream> {
|
#[derive(Debug)]
|
||||||
stream: S,
|
pub struct Timeout<S: Stream> {
|
||||||
delay: Delay,
|
#[pin]
|
||||||
|
stream: S,
|
||||||
|
#[pin]
|
||||||
|
delay: Delay,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S: Stream> Timeout<S> {
|
impl<S: Stream> Timeout<S> {
|
||||||
pin_utils::unsafe_pinned!(stream: S);
|
|
||||||
pin_utils::unsafe_pinned!(delay: Delay);
|
|
||||||
|
|
||||||
pub fn new(stream: S, dur: Duration) -> Timeout<S> {
|
pub fn new(stream: S, dur: Duration) -> Timeout<S> {
|
||||||
let delay = Delay::new(dur);
|
let delay = Delay::new(dur);
|
||||||
|
|
||||||
|
@ -30,11 +32,13 @@ impl<S: Stream> Timeout<S> {
|
||||||
impl<S: Stream> Stream for Timeout<S> {
|
impl<S: Stream> Stream for Timeout<S> {
|
||||||
type Item = Result<S::Item, TimeoutError>;
|
type Item = Result<S::Item, TimeoutError>;
|
||||||
|
|
||||||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||||
match self.as_mut().stream().poll_next(cx) {
|
let this = self.project();
|
||||||
|
|
||||||
|
match this.stream.poll_next(cx) {
|
||||||
Poll::Ready(Some(v)) => Poll::Ready(Some(Ok(v))),
|
Poll::Ready(Some(v)) => Poll::Ready(Some(Ok(v))),
|
||||||
Poll::Ready(None) => Poll::Ready(None),
|
Poll::Ready(None) => Poll::Ready(None),
|
||||||
Poll::Pending => match self.delay().poll(cx) {
|
Poll::Pending => match this.delay.poll(cx) {
|
||||||
Poll::Ready(_) => Poll::Ready(Some(Err(TimeoutError { _private: () }))),
|
Poll::Ready(_) => Poll::Ready(Some(Err(TimeoutError { _private: () }))),
|
||||||
Poll::Pending => Poll::Pending,
|
Poll::Pending => Poll::Pending,
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in a new issue