Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #[async_trait]
- pub trait AsyncTryIntoGql<GT> {
- async fn try_into_gql(self, context: &Context) -> Result<GT, Error>;
- }
- pin_project! {
- pub struct TryIntoGqlStream<'a, St, Gt> {
- #[pin]
- stream: St,
- #[pin]
- future: Option<dyn TryFuture<Output = Result<Gt, crate::Error>, Ok=Gt, Error=crate::Error>>,
- context: &'a crate::Context,
- }
- }
- impl<St, Gt> TryIntoGqlStream<St, Gt>
- where
- St: TryStream,
- {
- pub fn new(stream: St) -> Self {
- Self {
- stream,
- future: None,
- }
- }
- }
- impl<'a, St, Gt> Stream for TryIntoGqlStream<'a, St, Gt>
- where
- St: TryStream,
- St::Ok: AsyncTryIntoGql<Gt>,
- {
- type Item = Result<Gt, St::Error>;
- fn poll_next(self: Pin<&mut Self>, cx: &mut FContext<'_>) -> Poll<Option<Self::Item>> {
- let mut this = self.project();
- Poll::Ready(loop {
- if let Some(fut) = this.future.as_mut().as_pin_mut() {
- let item = ready!(*fut.try_poll(cx));
- this.future.set(None);
- break Some(item);
- }
- else if let Some(item) = ready!(this.stream.as_mut().try_poll_next(cx)?) {
- let x = async { item.try_into_gql(self.context).await };
- this.future.set(Some(x));
- }
- else {
- break None;
- }
- })
- }
- fn size_hint(&self) -> (usize, Option<usize>) {
- // ...
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement