sui_http/
fuse.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use std::{
5    future::Future,
6    pin::Pin,
7    task::{Context, Poll},
8};
9
10// From `futures-util` crate
11// LICENSE: MIT or Apache-2.0
12// A future which only yields `Poll::Ready` once, and thereafter yields `Poll::Pending`.
13pin_project_lite::pin_project! {
14    pub struct Fuse<F> {
15        #[pin]
16        inner: Option<F>,
17    }
18}
19
20impl<F> Fuse<F> {
21    pub fn new(future: F) -> Self {
22        Self {
23            inner: Some(future),
24        }
25    }
26}
27
28impl<F> Future for Fuse<F>
29where
30    F: Future,
31{
32    type Output = F::Output;
33
34    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
35        match self.as_mut().project().inner.as_pin_mut() {
36            Some(fut) => fut.poll(cx).map(|output| {
37                self.project().inner.set(None);
38                output
39            }),
40            None => Poll::Pending,
41        }
42    }
43}