sui_http/
fuse.rs

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