purrrr
This commit is contained in:
parent
eb948e4889
commit
deb96a3692
@ -1,9 +1,9 @@
|
|||||||
use std::{
|
use std::{
|
||||||
future::Future,
|
|
||||||
pin::Pin,
|
pin::Pin,
|
||||||
task::{Context, Poll},
|
task::{Context, Poll},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use futures::{future::ready, Future, FutureExt};
|
||||||
use pin_project::pin_project;
|
use pin_project::pin_project;
|
||||||
|
|
||||||
trait More {
|
trait More {
|
||||||
@ -379,26 +379,31 @@ trait TraitInto<A> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<A, I: Impl<Is<A>>> TraitInto<A> for I {
|
impl<A, I: Impl<Is<A>>> TraitInto<A> for I {
|
||||||
|
#[inline]
|
||||||
fn trait_into(self) -> A {
|
fn trait_into(self) -> A {
|
||||||
I::method_s(self)
|
I::method_s(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[repr(transparent)]
|
||||||
#[pin_project]
|
#[pin_project]
|
||||||
struct TraitFuture<F: ?Sized + Impl<Futures>>(#[pin] F);
|
struct TraitFuture<F: ?Sized + Impl<Futures>>(#[pin] F);
|
||||||
|
|
||||||
impl<F: ?Sized + Impl<Futures>> Future for TraitFuture<F> {
|
impl<F: ?Sized + Impl<Futures>> Future for TraitFuture<F> {
|
||||||
type Output = F::Associated;
|
type Output = F::Associated;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
F::method((self.project().0, cx))
|
F::method((self.project().0, cx))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
fn aaaaaaa<A>(a: impl Impl<Is<A>>) -> A {
|
fn aaaaaaa<A>(a: impl Impl<Is<A>>) -> A {
|
||||||
a.trait_into()
|
a.trait_into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
fn aaaaaa<
|
fn aaaaaa<
|
||||||
A,
|
A,
|
||||||
Ot: ?Sized + OverlayTrait<M::Trait, Associated = Is<A>>,
|
Ot: ?Sized + OverlayTrait<M::Trait, Associated = Is<A>>,
|
||||||
@ -411,7 +416,8 @@ fn aaaaaa<
|
|||||||
aaaaaaa::<A>(a)
|
aaaaaaa::<A>(a)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn aaaaa<
|
#[inline]
|
||||||
|
fn aaaaa<
|
||||||
A,
|
A,
|
||||||
Ot: ?Sized + OverlayTrait<Futures, Associated = Is<A>>,
|
Ot: ?Sized + OverlayTrait<Futures, Associated = Is<A>>,
|
||||||
Om: ?Sized + OverlayMore<M, Trait = Ot>,
|
Om: ?Sized + OverlayMore<M, Trait = Ot>,
|
||||||
@ -419,12 +425,11 @@ async fn aaaaa<
|
|||||||
I: ImplOverlay<M> + Impl<Futures> + ImplMember<Futures>,
|
I: ImplOverlay<M> + Impl<Futures> + ImplMember<Futures>,
|
||||||
>(
|
>(
|
||||||
a: I,
|
a: I,
|
||||||
) -> A {
|
) -> impl Future<Output = A> {
|
||||||
let a = TraitFuture(a).await;
|
TraitFuture(a).map(aaaaaa::<A, Ot, Om, M, I>)
|
||||||
let a: <I as ImplOverlayBase<M>>::_Associated = a;
|
|
||||||
aaaaaa::<A, Ot, Om, M, I>(a)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
fn aaaa<
|
fn aaaa<
|
||||||
A,
|
A,
|
||||||
Ot: ?Sized + OverlayTrait<Futures, Associated = Is<A>>,
|
Ot: ?Sized + OverlayTrait<Futures, Associated = Is<A>>,
|
||||||
@ -437,6 +442,7 @@ fn aaaa<
|
|||||||
aaaaa(upcast_ioi_ioia(upcast_im_ioi::<M, _>(a)))
|
aaaaa(upcast_ioi_ioia(upcast_im_ioi::<M, _>(a)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
fn aaa<A>(a: impl Impl<FutureIs<A>>) -> impl Future<Output = A> {
|
fn aaa<A>(a: impl Impl<FutureIs<A>>) -> impl Future<Output = A> {
|
||||||
aaaa(upcast_i_im(a))
|
aaaa(upcast_i_im(a))
|
||||||
}
|
}
|
||||||
@ -446,6 +452,7 @@ trait TraitIntoFuture<A> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<A, F: Impl<FutureIs<A>>> TraitIntoFuture<A> for F {
|
impl<A, F: Impl<FutureIs<A>>> TraitIntoFuture<A> for F {
|
||||||
|
#[inline]
|
||||||
fn trait_into_future(self) -> impl Future<Output = A> {
|
fn trait_into_future(self) -> impl Future<Output = A> {
|
||||||
aaa(self)
|
aaa(self)
|
||||||
}
|
}
|
||||||
@ -462,23 +469,26 @@ impl Functor for Futures {
|
|||||||
type Trait<A> = FutureIs<A>;
|
type Trait<A> = FutureIs<A>;
|
||||||
|
|
||||||
fn pure<A>(a: A) -> impl Impl<Self::Trait<A>> {
|
fn pure<A>(a: A) -> impl Impl<Self::Trait<A>> {
|
||||||
async { a }
|
ready(a)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
fn map<A, B>(
|
fn map<A, B>(
|
||||||
a: impl Impl<Self::Trait<A>>,
|
a: impl Impl<Self::Trait<A>>,
|
||||||
f: impl FnOnce(A) -> B,
|
f: impl FnOnce(A) -> B,
|
||||||
) -> impl Impl<Self::Trait<B>> {
|
) -> impl Impl<Self::Trait<B>> {
|
||||||
async { f(a.trait_into_future().await) }
|
a.trait_into_future().map(f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
fn upcast_ioi_ioia<T: ?Sized + More, I: ImplOverlay<T> + Impl<T::Trait>>(
|
fn upcast_ioi_ioia<T: ?Sized + More, I: ImplOverlay<T> + Impl<T::Trait>>(
|
||||||
value: I,
|
value: I,
|
||||||
) -> impl ImplOverlay<T> + ImplMember<<T::Trait as Trait>::Member> {
|
) -> impl ImplOverlay<T> + ImplMember<<T::Trait as Trait>::Member> {
|
||||||
value
|
value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
fn upcast_imb_ioi<T: ?Sized + More, I: ImplMoreBase<T>>(
|
fn upcast_imb_ioi<T: ?Sized + More, I: ImplMoreBase<T>>(
|
||||||
value: I::MoreTrait,
|
value: I::MoreTrait,
|
||||||
) -> impl ImplOverlay<T> + Impl<T::Trait>
|
) -> impl ImplOverlay<T> + Impl<T::Trait>
|
||||||
@ -488,10 +498,12 @@ where
|
|||||||
value
|
value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
fn upcast_im_ioi<T: ?Sized + More, I: ImplMore<T>>(value: I) -> impl ImplOverlay<T> {
|
fn upcast_im_ioi<T: ?Sized + More, I: ImplMore<T>>(value: I) -> impl ImplOverlay<T> {
|
||||||
upcast_imb_ioi::<T, I>(value)
|
upcast_imb_ioi::<T, I>(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
fn upcast_ib_im<T: ?Sized + Trait, I: ImplBase<T>>(value: I::More) -> impl ImplMore<T::More>
|
fn upcast_ib_im<T: ?Sized + Trait, I: ImplBase<T>>(value: I::More) -> impl ImplMore<T::More>
|
||||||
where
|
where
|
||||||
<I as ImplBase<T>>::More: Sized,
|
<I as ImplBase<T>>::More: Sized,
|
||||||
@ -499,18 +511,41 @@ where
|
|||||||
value
|
value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
fn upcast_i_im<T: ?Sized + Trait, I: Impl<T>>(value: I) -> impl ImplMore<T::More> {
|
fn upcast_i_im<T: ?Sized + Trait, I: Impl<T>>(value: I) -> impl ImplMore<T::More> {
|
||||||
upcast_ib_im::<T, I>(value)
|
upcast_ib_im::<T, I>(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
trait Pure: Sized {
|
||||||
|
fn pure_<F: Functor>(self) -> impl Impl<F::Trait<Self>> {
|
||||||
|
F::pure(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Pure for T {}
|
||||||
|
|
||||||
|
trait Map<A, B>: Sized {
|
||||||
|
fn map_<F: Functor>(self, f: impl FnOnce(A) -> B) -> impl Impl<F::Trait<B>>
|
||||||
|
where
|
||||||
|
Self: Impl<F::Trait<A>>,
|
||||||
|
{
|
||||||
|
F::map(self, f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<A, B, T> Map<A, B> for T {}
|
||||||
|
|
||||||
|
fn add5_generic<F: Functor>(x: i32) -> impl Impl<F::Trait<i32>> {
|
||||||
|
x.pure_::<F>()
|
||||||
|
.map_::<F>(|x| x + 1)
|
||||||
|
.map_::<F>(|x| x + 1)
|
||||||
|
.map_::<F>(|x| x + 1)
|
||||||
|
.map_::<F>(|x| x + 1)
|
||||||
|
.map_::<F>(|x| x + 1)
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn add5(x: i32) -> i32 {
|
pub async fn add5(x: i32) -> i32 {
|
||||||
let x = Futures::pure(x);
|
add5_generic::<Futures>(x).trait_into_future().await
|
||||||
let x = Futures::map(x, |x| x + 1);
|
|
||||||
let x = Futures::map(x, |x| x + 1);
|
|
||||||
let x = Futures::map(x, |x| x + 1);
|
|
||||||
let x = Futures::map(x, |x| x + 1);
|
|
||||||
let x = Futures::map(x, |x| x + 1);
|
|
||||||
x.trait_into_future().await
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
Reference in New Issue
Block a user