X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-c-bindings%2Fsrc%2Fc_types%2Fmod.rs;h=76807621c2681943c5bcc4712531528b45d9cff3;hb=7dd9d6f40cc5c7d251cc45820e350f669e3e1dfb;hp=20c49a6d92b30f0c96ad2ac6e394fbfbbcfa2c31;hpb=c7ddcd3867757c74d8a3a2997604edf52aa0a536;p=rust-lightning diff --git a/lightning-c-bindings/src/c_types/mod.rs b/lightning-c-bindings/src/c_types/mod.rs index 20c49a6d..76807621 100644 --- a/lightning-c-bindings/src/c_types/mod.rs +++ b/lightning-c-bindings/src/c_types/mod.rs @@ -45,6 +45,7 @@ impl SecretKey { } #[repr(C)] +#[derive(Clone)] pub struct Signature { pub compact_form: [u8; 64], } @@ -127,7 +128,7 @@ impl Transaction { impl Drop for Transaction { fn drop(&mut self) { if self.data_is_owned && self.datalen != 0 { - let _ = CVecTempl { data: self.data as *mut u8, datalen: self.datalen }; + let _ = derived::CVec_u8Z { data: self.data as *mut u8, datalen: self.datalen }; } } } @@ -156,13 +157,15 @@ impl TxOut { } pub(crate) fn from_rust(txout: ::bitcoin::blockdata::transaction::TxOut) -> Self { Self { - script_pubkey: CVecTempl::from(txout.script_pubkey.into_bytes()), + script_pubkey: derived::CVec_u8Z::from(txout.script_pubkey.into_bytes()), value: txout.value } } } #[no_mangle] pub extern "C" fn TxOut_free(_res: TxOut) { } +#[no_mangle] +pub extern "C" fn TxOut_clone(orig: &TxOut) -> TxOut { orig.clone() } #[repr(C)] pub struct u8slice { @@ -220,11 +223,14 @@ impl lightning::util::ser::Writer for VecWriter { pub(crate) fn serialize_obj(i: &I) -> derived::CVec_u8Z { let mut out = VecWriter(Vec::new()); i.write(&mut out).unwrap(); - CVecTempl::from(out.0) + derived::CVec_u8Z::from(out.0) } pub(crate) fn deserialize_obj(s: u8slice) -> Result { I::read(&mut s.to_slice()) } +pub(crate) fn deserialize_obj_arg>(s: u8slice, args: A) -> Result { + I::read(&mut s.to_slice(), args) +} #[repr(C)] #[derive(Copy, Clone)] @@ -253,14 +259,14 @@ impl Into<&'static str> for Str { // everywhere in the containers. #[repr(C)] -pub union CResultPtr { - pub result: *mut O, - pub err: *mut E, +pub(crate) union CResultPtr { + pub(crate) result: *mut O, + pub(crate) err: *mut E, } #[repr(C)] -pub struct CResultTempl { - pub contents: CResultPtr, - pub result_ok: bool, +pub(crate) struct CResultTempl { + pub(crate) contents: CResultPtr, + pub(crate) result_ok: bool, } impl CResultTempl { pub(crate) extern "C" fn ok(o: O) -> Self { @@ -280,7 +286,6 @@ impl CResultTempl { } } } -pub extern "C" fn CResultTempl_free(_res: CResultTempl) { } impl Drop for CResultTempl { fn drop(&mut self) { if self.result_ok { @@ -293,46 +298,6 @@ impl Drop for CResultTempl { } } -#[repr(C)] -pub struct CVecTempl { - pub data: *mut T, - pub datalen: usize -} -impl CVecTempl { - pub(crate) fn into_rust(&mut self) -> Vec { - if self.datalen == 0 { return Vec::new(); } - let ret = unsafe { Box::from_raw(std::slice::from_raw_parts_mut(self.data, self.datalen)) }.into(); - self.data = std::ptr::null_mut(); - self.datalen = 0; - ret - } - pub(crate) fn as_slice(&self) -> &[T] { - unsafe { std::slice::from_raw_parts_mut(self.data, self.datalen) } - } -} -impl From> for CVecTempl { - fn from(v: Vec) -> Self { - let datalen = v.len(); - let data = Box::into_raw(v.into_boxed_slice()); - CVecTempl { datalen, data: unsafe { (*data).as_mut_ptr() } } - } -} -pub extern "C" fn CVecTempl_free(_res: CVecTempl) { } -impl Drop for CVecTempl { - fn drop(&mut self) { - if self.datalen == 0 { return; } - unsafe { Box::from_raw(std::slice::from_raw_parts_mut(self.data, self.datalen)) }; - } -} -impl Clone for CVecTempl { - fn clone(&self) -> Self { - let mut res = Vec::new(); - if self.datalen == 0 { return Self::from(res); } - res.extend_from_slice(unsafe { std::slice::from_raw_parts_mut(self.data, self.datalen) }); - Self::from(res) - } -} - #[repr(C)] pub struct C2TupleTempl { pub a: A,