use std::fmt;
use std::rc::Rc;
pub struct Callback<IN>(Rc<dyn Fn(IN)>);
impl<IN, F: Fn(IN) + 'static> From<F> for Callback<IN> {
fn from(func: F) -> Self {
Callback(Rc::new(func))
}
}
impl<IN> Clone for Callback<IN> {
fn clone(&self) -> Self {
Callback(self.0.clone())
}
}
impl<IN> PartialEq for Callback<IN> {
fn eq(&self, other: &Callback<IN>) -> bool {
Rc::ptr_eq(&self.0, &other.0)
}
}
impl<IN> fmt::Debug for Callback<IN> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Callback<_>")
}
}
impl<IN> Callback<IN> {
pub fn emit(&self, value: IN) {
(self.0)(value);
}
}
impl<IN: 'static> Callback<IN> {
pub fn reform<F, T>(self, func: F) -> Callback<T>
where
F: Fn(T) -> IN + 'static,
{
let func = move |input| {
let output = func(input);
self.clone().emit(output);
};
Callback::from(func)
}
}