I am trying to get the below code to specialize based on the type F
which has to be a function or a closure (implements FnOnce
). FnOnce
is a super-trait of Fn
, so I expect to be able to provide a general implementation of the trait Runnable
for FnOnce
and then specialize the implementation for Fn
(to have a different behavior).
#![feature(unboxed_closures)] // to switch from parenthetical notation to generics for `Fn*` traits
#![feature(fn_traits)] // to use `call_once` and `call` methods on Fn* traits
#![feature(min_specialization)] // for specialization of trait implementations
struct Callable<
A, // arguments as a tuple
R, // return type
F, // Fn trait (like Fn, FnOnce, and FnMut)
>
where
F: FnOnce<A, Output = R>,
{
}
pub trait Runnable {
fn run(&mut self);
}
default impl<A, R, F> Runnable
for Callable<
A, // arguments as a tuple
R, // return type
F, // Fn trait (like Fn, FnOnce, and FnMut)
>
where
F: FnOnce<A, Output = R>,
{
fn run(self) {
println!("Just testing the run method for FnOnce...");
}
}
impl<A, R, F> Runnable
for Callable<
A, // arguments as a tuple
R, // return type
F, // Fn trait (like Fn, FnOnce, and FnMut)
>
where
F: Fn<A, Output = R>,
{
fn run(self) {
println!("Just testing the run method for Fn...");
}
}
pub fn main(){
}
Here is a playground link for reference: Rust Playground
I get errors about overflow
. Could someone please point out what I am doing wrong here, and how I can accomplish this?