Add once1! macro

This commit is contained in:
Alexandre Bury 2020-06-14 22:51:57 -07:00
parent 9060b38e75
commit e3a266aa0e
2 changed files with 37 additions and 2 deletions

View File

@ -200,6 +200,16 @@ impl Callback {
Self::from_fn(crate::immut1!(f)) Self::from_fn(crate::immut1!(f))
} }
/// Wrap a `FnOnce` into a `Callback` object.
///
/// After being called once, the callback will become a no-op.
pub fn from_fn_once<F>(f: F) -> Self
where
F: 'static + FnOnce(&mut Cursive),
{
Self::from_fn_mut(crate::once1!(f))
}
/// Returns a dummy callback that doesn't run anything. /// Returns a dummy callback that doesn't run anything.
pub fn dummy() -> Self { pub fn dummy() -> Self {
Callback::from_fn(|_| ()) Callback::from_fn(|_| ())

View File

@ -71,8 +71,33 @@ macro_rules! immut1 {
($f:expr) => {{ ($f:expr) => {{
let callback = ::std::cell::RefCell::new($f); let callback = ::std::cell::RefCell::new($f);
move |s| { move |s| {
if let Ok(mut f) = callback.try_borrow_mut() { if let ::std::result::Result::Ok(mut f) = callback.try_borrow_mut()
(&mut *f)(s) {
(&mut *f)(s);
}
}
}};
}
/// Macro to wrap a `FnOnce` with 1 argument into a `FnMut`.
///
/// This can wrap any `FnOnce` with a single argument (for example `&mut Cursive`).
///
/// # Note
///
/// If the resulting function is called multiple times, only the first call will trigger the
/// wrapped `FnOnce`. All subsequent calls will be no-ops.
///
/// In addition, due to weird interaction between Higher-rank trait bounds and
/// closures, you should use the result from the macro directly, and not
/// assign it to a variable.
#[macro_export]
macro_rules! once1 {
($f:expr) => {{
let mut callback = ::std::option::Option::Some($f);
move |s| {
if let ::std::option::Option::Some(f) = callback.take() {
f(s);
} }
} }
}}; }};