I am using a trait to create an alias for a closure so that I do not have to repeat the declaration several times throughout the code, however I cannot work out how to cast the trait back to Fn so that I can call it in main, is this possible?
trait Closure: Send + Sync {}
impl <F: Send + Sync> Closure for F where F: Fn(&str) -> bool {}
pub struct Struct {
pub closure: Box<dyn Closure>
}
impl Struct {
pub fn new(closure: impl Closure + 'static) -> Self {
Self{
closure: Box::new(closure)
}
}
}
pub fn main() {
let s = Struct::new(|f: &str| f.is_empty());
let result: bool = (s.closure)("hello world"); //TODO
println!("{}", result );
}