/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
impl<T> StateCallback<T> { // This is used for the Condvar, which requires this kind of construction #[allow(clippy::mutex_atomic)] pub fn new(cb: Box<dyn FnOnce(T) + Send>) -> Self { Self {
callback: Arc::new(Mutex::new(Some(cb))),
observer: Arc::new(Mutex::new(None)),
condition: Arc::new((Mutex::new(true), Condvar::new())),
}
}
assert_eq!(counter.load(Ordering::SeqCst), 0);
for _ in 0..10 {
sc.call(());
assert_eq!(counter.load(Ordering::SeqCst), 1);
}
for _ in 0..10 {
sc.clone().call(());
assert_eq!(counter.load(Ordering::SeqCst), 1);
}
}
#[test]
fn test_statecallback_observer_only_runs_for_completing_callback() { let cb_counter = Arc::new(AtomicUsize::new(0)); let cb_counter_clone = cb_counter.clone(); let sc = StateCallback::new(Box::new(move |_| {
cb_counter_clone.fetch_add(1, Ordering::SeqCst);
}));
let obs_counter = Arc::new(AtomicUsize::new(0));
for _ in 0..10 { let obs_counter_clone = obs_counter.clone(); letmut c = sc.clone();
c.add_uncloneable_observer(Box::new(move || {
obs_counter_clone.fetch_add(1, Ordering::SeqCst);
}));
assert!(sc.observer.lock().unwrap().is_some()); // This is deliberate, to force an extra clone
assert!(sc.clone().observer.lock().unwrap().is_none());
}
#[test]
fn test_statecallback_wait() { let sc = StateCallback::<()>::new(Box::new(move |_| {})); let barrier = Arc::new(Barrier::new(2));
{ let c = sc.clone(); let b = barrier.clone();
thread::spawn(move || {
b.wait();
c.call(());
});
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.