X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Futil%2Fwakers.rs;h=166771948fad62bc2f10078b5360fb67207f50f5;hb=a031ff952bee42aad38edd29ede6aaa05a4cb6cb;hp=7ab9aca0efe42fbc97da40c70463426595e8bc87;hpb=f14ea3dd183d91ba2471ad0cb30d91352761d1cf;p=rust-lightning diff --git a/lightning/src/util/wakers.rs b/lightning/src/util/wakers.rs index 7ab9aca0..16677194 100644 --- a/lightning/src/util/wakers.rs +++ b/lightning/src/util/wakers.rs @@ -15,13 +15,12 @@ use alloc::sync::Arc; use core::mem; -use core::time::Duration; use sync::{Condvar, Mutex}; use prelude::*; #[cfg(any(test, feature = "std"))] -use std::time::Instant; +use std::time::{Duration, Instant}; use core::future::Future as StdFuture; use core::task::{Context, Poll}; @@ -164,6 +163,8 @@ pub struct Future { impl Future { /// Registers a callback to be called upon completion of this future. If the future has already /// completed, the callback will be called immediately. + /// + /// (C-not exported) use the bindings-only `register_callback_fn` instead pub fn register_callback(&self, callback: Box) { let mut state = self.state.lock().unwrap(); if state.complete { @@ -173,6 +174,16 @@ impl Future { state.callbacks.push(callback); } } + + // C bindings don't (currently) know how to map `Box`, and while it could add the + // following wrapper, doing it in the bindings is currently much more work than simply doing it + // here. + /// Registers a callback to be called upon completion of this future. If the future has already + /// completed, the callback will be called immediately. + #[cfg(c_bindings)] + pub fn register_callback_fn(&self, callback: F) { + self.register_callback(Box::new(callback)); + } } mod std_future { @@ -293,7 +304,7 @@ mod tests { // waker, which we do here with a trivial Arc data element to track woke-ness. const WAKER_V_TABLE: RawWakerVTable = RawWakerVTable::new(waker_clone, wake, wake_by_ref, drop); unsafe fn wake_by_ref(ptr: *const ()) { let p = ptr as *const Arc; assert!(!(*p).fetch_or(true, Ordering::SeqCst)); } - unsafe fn drop(ptr: *const ()) { let p = ptr as *mut Arc; Box::from_raw(p); } + unsafe fn drop(ptr: *const ()) { let p = ptr as *mut Arc; let _freed = Box::from_raw(p); } unsafe fn wake(ptr: *const ()) { wake_by_ref(ptr); drop(ptr); } unsafe fn waker_clone(ptr: *const ()) -> RawWaker { let p = ptr as *const Arc;