cocoro/
just_yield.rs

1use crate::coro::Coro;
2use crate::suspended::Suspended;
3use crate::suspended::SuspendedVisitor;
4
5#[derive(Debug, PartialEq, Eq, Clone, Copy)]
6pub struct JustYield<T>(T);
7
8/// An implementation of `Suspended` that wraps a yield value.
9///
10/// A coroutine that uses this struct as its `Suspend` associated type will be
11/// known at compile time to always return, and never yield.
12pub struct Yielded<T, N>(pub T, pub N);
13
14impl<T, N, R, I> Suspended<T, R, I> for Yielded<T, N>
15where
16    N: Coro<T, R, I>,
17{
18    type Next = N;
19    fn visit<X>(
20        self,
21        visitor: impl SuspendedVisitor<T, R, I, N, Out = X>,
22    ) -> X {
23        let Self(y, n) = self;
24        visitor.on_yield(y, n)
25    }
26}
27
28impl<T, R, I> Coro<T, R, I> for JustYield<T>
29where
30    T: Copy,
31{
32    type Next = Self;
33    type Suspend = Yielded<T, Self>;
34    fn resume(self, _: I) -> Self::Suspend {
35        Yielded(self.0, self)
36    }
37}
38
39pub fn just_yield<T>(t: T) -> JustYield<T> {
40    JustYield(t)
41}