1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use futures::{future::try_join_all, SinkExt, StreamExt};

use crate::{
    channel::{Channel, ForkHandle},
    kind,
    kind::Future,
    ConstructResult, DeconstructResult, Kind,
};

use super::{using, AsKind, WrappedError};

use core::{iter::FromIterator, ops::Deref};

#[derive(Clone, Debug, Copy, Hash, Eq, Ord, PartialOrd, PartialEq, Default)]
pub struct Iterator<
    T: Unpin + Sync + Send + IntoIterator + FromIterator<<T as IntoIterator>::Item> + 'static,
>(pub T)
where
    <T as IntoIterator>::Item: Kind,
    T::IntoIter: Unpin + Sync + Send;

impl<T: Unpin + Sync + Send + IntoIterator + FromIterator<<T as IntoIterator>::Item> + 'static>
    Iterator<T>
where
    <T as IntoIterator>::Item: Kind,
    T::IntoIter: Unpin + Sync + Send,
{
    pub fn new(item: T) -> Self {
        Iterator(item)
    }
}

impl<T: Unpin + Sync + Send + IntoIterator + FromIterator<<T as IntoIterator>::Item> + 'static>
    Deref for Iterator<T>
where
    <T as IntoIterator>::Item: Kind,
    T::IntoIter: Unpin + Sync + Send,
{
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T: Unpin + Sync + Send + IntoIterator + FromIterator<<T as IntoIterator>::Item> + 'static>
    From<T> for Iterator<T>
where
    <T as IntoIterator>::Item: Kind,
    T::IntoIter: Unpin + Sync + Send,
{
    fn from(item: T) -> Self {
        Iterator(item)
    }
}

impl<T: Unpin + Sync + Send + IntoIterator + FromIterator<<T as IntoIterator>::Item> + 'static>
    FromIterator<<T as IntoIterator>::Item> for Iterator<T>
where
    <T as IntoIterator>::Item: Kind,
    T::IntoIter: Unpin + Sync + Send,
{
    fn from_iter<U>(iter: U) -> Self
    where
        U: IntoIterator<Item = <T as IntoIterator>::Item>,
    {
        Iterator(iter.into_iter().collect())
    }
}

impl<T: Unpin + Sync + Send + IntoIterator + FromIterator<<T as IntoIterator>::Item> + 'static>
    AsKind<using::Iterator> for T
where
    <T as IntoIterator>::Item: Kind,
    T::IntoIter: Unpin + Sync + Send,
{
    type Kind = Iterator<T>;

    fn into_kind(self) -> Iterator<T> {
        Iterator(self)
    }
    fn from_kind(kind: Self::Kind) -> Self {
        kind.0
    }
}

impl<T: Unpin + Sync + Send + IntoIterator + FromIterator<<T as IntoIterator>::Item> + 'static>
    IntoIterator for Iterator<T>
where
    <T as IntoIterator>::Item: Kind,
    T::IntoIter: Unpin + Sync + Send,
{
    type Item = <T as IntoIterator>::Item;
    type IntoIter = <T as IntoIterator>::IntoIter;

    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

#[kind]
impl<T: Unpin + Sync + Send + IntoIterator + FromIterator<<T as IntoIterator>::Item> + 'static> Kind
    for Iterator<T>
where
    <T as IntoIterator>::Item: Kind,
    T::IntoIter: Unpin + Sync + Send,
{
    type ConstructItem = Vec<ForkHandle>;
    type ConstructError = WrappedError<<<T as IntoIterator>::Item as Kind>::ConstructError>;
    type ConstructFuture = Future<ConstructResult<Self>>;
    type DeconstructItem = ();
    type DeconstructError = WrappedError<<<T as IntoIterator>::Item as Kind>::DeconstructError>;
    type DeconstructFuture = Future<DeconstructResult<Self>>;

    fn deconstruct<C: Channel<Self::DeconstructItem, Self::ConstructItem>>(
        self,
        mut channel: C,
    ) -> Self::DeconstructFuture {
        Box::pin(async move {
            Ok(channel
                .send(
                    try_join_all(
                        self.0
                            .into_iter()
                            .map(|entry| channel.fork::<<T as IntoIterator>::Item>(entry)),
                    )
                    .await?,
                )
                .await
                .map_err(WrappedError::Send)?)
        })
    }
    fn construct<C: Channel<Self::ConstructItem, Self::DeconstructItem>>(
        mut channel: C,
    ) -> Self::ConstructFuture {
        Box::pin(async move {
            let handles = channel.next().await.ok_or(WrappedError::Insufficient {
                got: 0,
                expected: 1,
            })?;
            Ok(Iterator(
                try_join_all(
                    handles
                        .into_iter()
                        .map(|entry| channel.get_fork::<<T as IntoIterator>::Item>(entry)),
                )
                .await?
                .into_iter()
                .collect(),
            ))
        })
    }
}