1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
/* // TODO: replace the 3 macros with 3 functions once they work correctly with // reference arguments. // (The returned closure must implement the for<'a> Fn(T, U<'a>)... // TODO: replace the 3 macros/functions with a generic function when it can // accept any number of arguments. /// Wraps a `FnMut` into a `Fn` /// /// This can be used to use a `FnMut` when a callack expects a `Fn`. /// /// # Note /// /// If the resulting `Fn` is called recursively, subsequent calls will be /// no-ops. pub fn immutify<F: FnMut(&mut Cursive)>( f: F, ) -> impl for<'s> Fn(&'s mut Cursive) { let callback = RefCell::new(f); move |s| { // Here's the weird trick: if we're already borrowed, // just ignored the callback. if let Ok(mut f) = callback.try_borrow_mut() { // Beeeaaah that's ugly. // Why do we need to manually dereference here? (&mut *f)(s); } } } */ /// Macro to wrap a `FnMut` with 1 argument into a `Fn`. /// /// This can wrap any `FnMut` with a single arguments (for example `&mut Cursive`). /// /// See [`immut2!`] and [`immut3!`] to support a different number of arguments. /// /// # Note /// /// If this function tries to call itself recursively (for example by /// triggering an event in `Cursive`), the second call will be a no-op. /// Enabling recursive calls would break the `FnMut` contract. /// /// 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. /// /// # Examples /// /// ```rust /// # use cursive_core::{Cursive, immut1}; /// # fn main() { /// # let mut siv = Cursive::new(); /// let mut i = 0; /// // `Cursive::add_global_callback` takes a `Fn(&mut Cursive)` /// siv.add_global_callback( /// 'q', /// immut1!(move |s: &mut Cursive| { /// // But here we mutate the environment! Crazy! /// i += 1; /// if i == 5 { /// s.quit(); /// } /// }), /// ); /// # } /// ``` #[macro_export] macro_rules! immut1 { ($f:expr ; else $else:expr) => {{ let callback = ::std::cell::RefCell::new($f); move |s| { if let ::std::result::Result::Ok(mut f) = callback.try_borrow_mut() { (&mut *f)(s) } else { $else } } }}; ($f:expr) => {{ let callback = ::std::cell::RefCell::new($f); move |s| { if let ::std::result::Result::Ok(mut f) = callback.try_borrow_mut() { (&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); } } }}; } /// Macro to wrap a `FnMut` with 2 arguments into a `Fn`. /// /// This can wrap any `FnMut` with two arguments. /// /// See [`immut1!`] and [`immut3!`] to support a different number of arguments. /// /// # Note /// /// If this function tries to call itself recursively (for example by /// triggering an event in `Cursive`), the second call will be a no-op. /// Enabling recursive calls would break the `FnMut` contract. /// /// 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! immut2 { ($f:expr ; else $else:expr) => {{ let callback = ::std::cell::RefCell::new($f); move |s, t| { if let ::std::result::Result::Ok(mut f) = callback.try_borrow_mut() { (&mut *f)(s, t) } else { $else } } }}; ($f:expr) => {{ let callback = ::std::cell::RefCell::new($f); move |s, t| { if let Ok(mut f) = callback.try_borrow_mut() { (&mut *f)(s, t); } } }}; } /// Macro to wrap a `FnMut` with 3 arguments into a `Fn`. /// /// This can wrap any `FnMut` with three arguments. /// /// See [`immut1!`] and [`immut2!`] to support a different number of arguments. /// /// # Note /// /// If this function tries to call itself recursively (for example by /// triggering an event in `Cursive`), the second call will be a no-op. /// Enabling recursive calls would break the `FnMut` contract. /// /// 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! immut3 { ($f:expr ; else $else:expr) => {{ let callback = ::std::cell::RefCell::new($f); move |s, t, t| { if let ::std::result::Result::Ok(mut f) = callback.try_borrow_mut() { (&mut *f)(s, t, u) } else { $else } } }}; ($f:expr) => {{ let callback = ::std::cell::RefCell::new($f); move |s, t, u| { if let Ok(mut f) = callback.try_borrow_mut() { (&mut *f)(s, t, u); } } }}; }