iota_http/
fuse.rs

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