Advertisement
Guest User

Untitled

a guest
May 25th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.75 KB | None | 0 0
  1. #[derive(Clone)]
  2. pub struct CompatClosure<C3, A, F3>(C3, PhantomData<A>, PhantomData<F3>) where
  3.         F3: FutureExt,
  4.         C3: FnOnce<A, Output = F3>;
  5.  
  6. impl<C3, A, F3> Fn<A> for CompatClosure<C3, A, F3> where
  7.     F3: TryFuture + FutureExt + Send + 'static,
  8.    Pin<Box<dyn Future<Output = <F3 as Future>::Output> + Send>>: TryFutureExt,
  9.    C3: Fn<A, Output = F3> {
  10.  
  11.    extern "rust-call" fn call(&self, args: A) -> Self::Output {
  12.        self.0.call(args).boxed().compat()
  13.    }
  14. }
  15.  
  16. impl<C3, A, F3> FnMut<A> for CompatClosure<C3, A, F3> where
  17.    F3: TryFuture + FutureExt + Send + 'static,
  18.     Pin<Box<dyn Future<Output = <F3 as Future>::Output> + Send>>: TryFutureExt,
  19.     C3: FnMut<A, Output = F3> {
  20.  
  21.     extern "rust-call" fn call_mut(&mut self, args: A) -> Self::Output {
  22.         self.0.call_mut(args).boxed().compat()
  23.     }
  24. }
  25.  
  26. impl<C3, A, F3> FnOnce<A> for CompatClosure<C3, A, F3> where
  27.     F3: TryFuture + FutureExt + Send + 'static,
  28.    Pin<Box<dyn Future<Output = <F3 as Future>::Output> + Send>>: TryFutureExt,
  29.    C3: FnOnce<A, Output = F3> {
  30.  
  31.    type Output = compat::Compat<Pin<Box<dyn Future<Output = <F3 as Future>::Output> + Send>>>;
  32.  
  33.    extern "rust-call" fn call_once(self, args: A) -> Self::Output {
  34.        self.0.call_once(args).boxed().compat()
  35.    }
  36. }
  37.  
  38.  
  39. pub trait ClosureCompat<A, F3> where
  40.         Self: Sized + FnOnce<A, Output = F3>,
  41.         F3: TryFuture + FutureExt + Send + 'static {
  42.     fn asdf(self) -> CompatClosure<Self, A, F3>;
  43. }
  44.  
  45.  
  46. impl<C3, A, F3> ClosureCompat<A, F3> for C3 where
  47.         F3: TryFuture + FutureExt + Send + 'static,
  48.        C3: FnOnce<A, Output = F3> + Sized {
  49.    fn asdf(self) -> CompatClosure<Self, A, F3> {
  50.        CompatClosure(self, PhantomData, PhantomData)
  51.    }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement