X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-c-bindings%2Fsrc%2Fchain%2Fchainmonitor.rs;h=c05f9017ca1eb6888aff883701b3642661b6339b;hb=c42ea50cc703f03465d52b5fccfc4a90466d9fea;hp=35eb1bed22d1e442c7bfbbc9991156e0cd72cc84;hpb=3c4a0c1fb30452d77177edc09b2f321e6502ac08;p=rust-lightning diff --git a/lightning-c-bindings/src/chain/chainmonitor.rs b/lightning-c-bindings/src/chain/chainmonitor.rs index 35eb1bed..c05f9017 100644 --- a/lightning-c-bindings/src/chain/chainmonitor.rs +++ b/lightning-c-bindings/src/chain/chainmonitor.rs @@ -26,7 +26,7 @@ use crate::c_types::*; use lightning::chain::chainmonitor::ChainMonitor as nativeChainMonitorImport; -type nativeChainMonitor = nativeChainMonitorImport; +type nativeChainMonitor = nativeChainMonitorImport; /// An implementation of [`chain::Watch`] for monitoring channels. /// @@ -49,7 +49,7 @@ pub struct ChainMonitor { impl Drop for ChainMonitor { fn drop(&mut self) { - if self.is_owned && !self.inner.is_null() { + if self.is_owned && !<*mut nativeChainMonitor>::is_null(self.inner) { let _ = unsafe { Box::from_raw(self.inner) }; } } @@ -64,7 +64,7 @@ extern "C" fn ChainMonitor_free_void(this_ptr: *mut c_void) { #[allow(unused)] /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy impl ChainMonitor { - pub(crate) fn take_ptr(mut self) -> *mut nativeChainMonitor { + pub(crate) fn take_inner(mut self) -> *mut nativeChainMonitor { assert!(self.is_owned); let ret = self.inner; self.inner = std::ptr::null_mut(); @@ -117,8 +117,18 @@ pub extern "C" fn ChainMonitor_new(chain_source: *mut crate::chain::Filter, mut ChainMonitor { inner: Box::into_raw(Box::new(ret)), is_owned: true } } +impl From for crate::chain::Watch { + fn from(obj: nativeChainMonitor) -> Self { + let mut rust_obj = ChainMonitor { inner: Box::into_raw(Box::new(obj)), is_owned: true }; + let mut ret = ChainMonitor_as_Watch(&rust_obj); + // We want to free rust_obj when ret gets drop()'d, not rust_obj, so wipe rust_obj's pointer and set ret's free() fn + rust_obj.inner = std::ptr::null_mut(); + ret.free = Some(ChainMonitor_free_void); + ret + } +} #[no_mangle] -pub extern "C" fn ChainMonitor_as_Watch(this_arg: *const ChainMonitor) -> crate::chain::Watch { +pub extern "C" fn ChainMonitor_as_Watch(this_arg: &ChainMonitor) -> crate::chain::Watch { crate::chain::Watch { this_arg: unsafe { (*this_arg).inner as *mut c_void }, free: None, @@ -127,39 +137,49 @@ pub extern "C" fn ChainMonitor_as_Watch(this_arg: *const ChainMonitor) -> crate: release_pending_monitor_events: ChainMonitor_Watch_release_pending_monitor_events, } } -use lightning::chain::Watch as WatchTraitImport; + #[must_use] extern "C" fn ChainMonitor_Watch_watch_channel(this_arg: *const c_void, mut funding_outpoint: crate::chain::transaction::OutPoint, mut monitor: crate::chain::channelmonitor::ChannelMonitor) -> crate::c_types::derived::CResult_NoneChannelMonitorUpdateErrZ { - let mut ret = unsafe { &mut *(this_arg as *mut nativeChainMonitor) }.watch_channel(*unsafe { Box::from_raw(funding_outpoint.take_ptr()) }, *unsafe { Box::from_raw(monitor.take_ptr()) }); - let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { 0u8 /*o*/ }), Err(mut e) => crate::c_types::CResultTempl::err( { crate::chain::channelmonitor::ChannelMonitorUpdateErr::native_into(e) }) }; + let mut ret = >::watch_channel(unsafe { &mut *(this_arg as *mut nativeChainMonitor) }, *unsafe { Box::from_raw(funding_outpoint.take_inner()) }, *unsafe { Box::from_raw(monitor.take_inner()) }); + let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { 0u8 /*o*/ }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::chain::channelmonitor::ChannelMonitorUpdateErr::native_into(e) }).into() }; local_ret } #[must_use] extern "C" fn ChainMonitor_Watch_update_channel(this_arg: *const c_void, mut funding_txo: crate::chain::transaction::OutPoint, mut update: crate::chain::channelmonitor::ChannelMonitorUpdate) -> crate::c_types::derived::CResult_NoneChannelMonitorUpdateErrZ { - let mut ret = unsafe { &mut *(this_arg as *mut nativeChainMonitor) }.update_channel(*unsafe { Box::from_raw(funding_txo.take_ptr()) }, *unsafe { Box::from_raw(update.take_ptr()) }); - let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { 0u8 /*o*/ }), Err(mut e) => crate::c_types::CResultTempl::err( { crate::chain::channelmonitor::ChannelMonitorUpdateErr::native_into(e) }) }; + let mut ret = >::update_channel(unsafe { &mut *(this_arg as *mut nativeChainMonitor) }, *unsafe { Box::from_raw(funding_txo.take_inner()) }, *unsafe { Box::from_raw(update.take_inner()) }); + let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { 0u8 /*o*/ }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::chain::channelmonitor::ChannelMonitorUpdateErr::native_into(e) }).into() }; local_ret } #[must_use] extern "C" fn ChainMonitor_Watch_release_pending_monitor_events(this_arg: *const c_void) -> crate::c_types::derived::CVec_MonitorEventZ { - let mut ret = unsafe { &mut *(this_arg as *mut nativeChainMonitor) }.release_pending_monitor_events(); - let mut local_ret = Vec::new(); for item in ret.drain(..) { local_ret.push( { crate::chain::channelmonitor::MonitorEvent { inner: Box::into_raw(Box::new(item)), is_owned: true } }); }; + let mut ret = >::release_pending_monitor_events(unsafe { &mut *(this_arg as *mut nativeChainMonitor) }, ); + let mut local_ret = Vec::new(); for mut item in ret.drain(..) { local_ret.push( { crate::chain::channelmonitor::MonitorEvent::native_into(item) }); }; local_ret.into() } +impl From for crate::util::events::EventsProvider { + fn from(obj: nativeChainMonitor) -> Self { + let mut rust_obj = ChainMonitor { inner: Box::into_raw(Box::new(obj)), is_owned: true }; + let mut ret = ChainMonitor_as_EventsProvider(&rust_obj); + // We want to free rust_obj when ret gets drop()'d, not rust_obj, so wipe rust_obj's pointer and set ret's free() fn + rust_obj.inner = std::ptr::null_mut(); + ret.free = Some(ChainMonitor_free_void); + ret + } +} #[no_mangle] -pub extern "C" fn ChainMonitor_as_EventsProvider(this_arg: *const ChainMonitor) -> crate::util::events::EventsProvider { +pub extern "C" fn ChainMonitor_as_EventsProvider(this_arg: &ChainMonitor) -> crate::util::events::EventsProvider { crate::util::events::EventsProvider { this_arg: unsafe { (*this_arg).inner as *mut c_void }, free: None, get_and_clear_pending_events: ChainMonitor_EventsProvider_get_and_clear_pending_events, } } -use lightning::util::events::EventsProvider as EventsProviderTraitImport; + #[must_use] extern "C" fn ChainMonitor_EventsProvider_get_and_clear_pending_events(this_arg: *const c_void) -> crate::c_types::derived::CVec_EventZ { - let mut ret = unsafe { &mut *(this_arg as *mut nativeChainMonitor) }.get_and_clear_pending_events(); - let mut local_ret = Vec::new(); for item in ret.drain(..) { local_ret.push( { crate::util::events::Event::native_into(item) }); }; + let mut ret = >::get_and_clear_pending_events(unsafe { &mut *(this_arg as *mut nativeChainMonitor) }, ); + let mut local_ret = Vec::new(); for mut item in ret.drain(..) { local_ret.push( { crate::util::events::Event::native_into(item) }); }; local_ret.into() }