Advertisement
Makku_CZ

Untitled

Jun 30th, 2021
1,920
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.48 KB | None | 0 0
  1. #[async_trait]
  2. pub trait AsyncTryIntoGql<GT> {
  3.     async fn try_into_gql(self, context: &Context) -> Result<GT, Error>;
  4. }
  5.  
  6. pin_project! {
  7.     pub struct TryIntoGqlStream<'a, St, Gt> {
  8.        #[pin]
  9.        stream: St,
  10.        #[pin]
  11.        future: Option<dyn TryFuture<Output = Result<Gt, crate::Error>, Ok=Gt, Error=crate::Error>>,
  12.        context: &'a crate::Context,
  13.     }
  14. }
  15.  
  16. impl<St, Gt> TryIntoGqlStream<St, Gt>
  17. where
  18.     St: TryStream,
  19. {
  20.     pub fn new(stream: St) -> Self {
  21.         Self {
  22.             stream,
  23.             future: None,
  24.         }
  25.     }
  26. }
  27.  
  28. impl<'a, St, Gt> Stream for TryIntoGqlStream<'a, St, Gt>
  29. where
  30.     St: TryStream,
  31.     St::Ok: AsyncTryIntoGql<Gt>,
  32. {
  33.     type Item = Result<Gt, St::Error>;
  34.  
  35.     fn poll_next(self: Pin<&mut Self>, cx: &mut FContext<'_>) -> Poll<Option<Self::Item>> {
  36.        let mut this = self.project();
  37.  
  38.        Poll::Ready(loop {
  39.            if let Some(fut) = this.future.as_mut().as_pin_mut() {
  40.                let item = ready!(*fut.try_poll(cx));
  41.                this.future.set(None);
  42.                break Some(item);
  43.            }
  44.            else if let Some(item) = ready!(this.stream.as_mut().try_poll_next(cx)?) {
  45.                let x = async { item.try_into_gql(self.context).await };
  46.                this.future.set(Some(x));
  47.            }
  48.            else {
  49.                break None;
  50.            }
  51.        })
  52.    }
  53.  
  54.    fn size_hint(&self) -> (usize, Option<usize>) {
  55.        // ...
  56.    }
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement