This commit is contained in:
AF 2024-05-10 18:02:41 +03:00
parent eb948e4889
commit deb96a3692
Signed by: alisa
SSH Key Fingerprint: SHA256:vNY4pdIZvO1FYJKHROkdHLtvyopizvZVAEwg9AF6h04

View File

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