async_let/
list.rs

1use crate::{wait::DriveWaitFor, ReadyOrNot};
2use core::{future::Future, marker::PhantomData};
3
4/// Represents a typed list of no background futures.
5#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
6pub struct Empty {
7    pub(crate) _priv: (),
8}
9
10/// Represents a typed list of one or more background futures.
11#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
12pub struct At<F: Future, Tail>
13{
14    pub(crate) node: ReadyOrNot<F>,
15    pub(crate) tail: Tail,
16    // needed to tell derive macros that this type indirectly contains F::Output
17    pub(crate) _holds_output: PhantomData<F::Output>,
18}
19
20/// A trait representing a list of background futures.
21pub trait FutList: DriveWaitFor {}
22
23impl FutList for Empty {}
24
25impl<F: Future + Unpin, T: FutList> FutList for At<F, T>
26{
27}
28
29/// A marker type used for indexing futures in a group. This type represents the first future in a group.
30pub struct Z(());
31
32/// A marker type used for indexing futures in a group. This type represents the next future in a group.
33pub struct S<I>(I);
34
35/// A trait that defines the operation of detaching a future of type `F` at index `I`.
36pub trait Detach<F: Future, I>
37{
38    /// The group that remains after detaching the future.
39    type Output;
40
41    /// Detaches the future at index `I`.
42    fn detach(self) -> (ReadyOrNot<F>, Self::Output);
43}
44
45impl<F: Future, T> Detach<F, Z> for At<F, T>
46{
47    type Output = T;
48
49    fn detach(self) -> (ReadyOrNot<F>, Self::Output) {
50        (self.node, self.tail)
51    }
52}
53
54impl<F: Future, I, H: Future, T> Detach<F, S<I>> for At<H, T>
55where
56    T: Detach<F, I>,
57{
58    type Output = At<H, T::Output>;
59
60    fn detach(self) -> (ReadyOrNot<F>, Self::Output) {
61        let (val, tail) = self.tail.detach();
62        (
63            val,
64            At {
65                node: self.node,
66                tail,
67                _holds_output: PhantomData,
68            },
69        )
70    }
71}