Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created August 30, 2018 00:21
Show Gist options
  • Save rust-play/f486f6a633636f37eb8a74ef0bf01de0 to your computer and use it in GitHub Desktop.
Save rust-play/f486f6a633636f37eb8a74ef0bf01de0 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
#![feature(unboxed_closures)]
#![feature(fn_traits)]
use std::marker::PhantomData;
use std::ops::{Add, FnOnce};
struct WhateverFn<F, T>(F, PhantomData<T>);
impl<F, T> FnOnce<(T,)> for WhateverFn<F, T> where F: FnOnce<(T,)> {
type Output = F::Output;
extern "rust-call" fn call_once(self, arg: (T,)) -> Self::Output {
self.0(arg.0)
}
}
impl<F, T, U> Add<U> for WhateverFn<F, T> where F: FnOnce<(T,)>, F::Output: Add<U> {
type Output = WhateverFn<impl FnOnce<T, Output=Self::Output>, T>;
fn add(self, rhs: U) -> Self::Output {
WhateverFn(|x| self(x) + rhs, PhantomData)
}
}
fn main() {
let what = WhateverFn(|n: usize| n, PhantomData);
let erm = what + 3;
println!("{}", erm(7));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment