3 ms·
I hate that (nightly) Rust lets you do this: #![feature(fn_traits)] #![feature(unboxed_closures)] struct If; impl<T, F, V> FnOnce<(bool, T, F
by Measter 4y ago
I hate that (nightly) Rust lets you do this:
#![feature(fn_traits)]
#![feature(unboxed_closures)]
struct If;
impl<T, F, V> FnOnce<(bool, T, F)> for If
where
T: FnMut() -> V,
F: FnMut() -> V
{
type Output = V;
extern "rust-call" fn call_once(self, args: (bool, T, F)) -> Self::Output {
let (condition, mut yay, mut nay) = args;
let arms: [&mut dyn FnMut() -> V; 2] = [&mut nay, &mut yay];
arms[condition as usize]()
}
}
https://play.rust-lang.org/?version=nightly&mode=release&edition=2021&gist=1b8101ffae222b9645d28a570d0cd72c https://play.rust-lang.org/?version=nightly&mode=release&edi...
- db48x 4y agoWhy do you hate that Rust allows you to write this? You could write something like this in most modern languages.