+
+use lightning::onion_message::packet::Packet as nativePacketImport;
+pub(crate) type nativePacket = nativePacketImport;
+
+/// Packet of hop data for next peer
+#[must_use]
+#[repr(C)]
+pub struct Packet {
+ /// A pointer to the opaque Rust object.
+
+ /// Nearly everywhere, inner must be non-null, however in places where
+ /// the Rust equivalent takes an Option, it may be set to null to indicate None.
+ pub inner: *mut nativePacket,
+ /// Indicates that this is the only struct which contains the same pointer.
+
+ /// Rust functions which take ownership of an object provided via an argument require
+ /// this to be true and invalidate the object pointed to by inner.
+ pub is_owned: bool,
+}
+
+impl Drop for Packet {
+ fn drop(&mut self) {
+ if self.is_owned && !<*mut nativePacket>::is_null(self.inner) {
+ let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
+ }
+ }
+}
+/// Frees any resources used by the Packet, if is_owned is set and inner is non-NULL.
+#[no_mangle]
+pub extern "C" fn Packet_free(this_obj: Packet) { }
+#[allow(unused)]
+/// Used only if an object of this type is returned as a trait impl by a method
+pub(crate) extern "C" fn Packet_free_void(this_ptr: *mut c_void) {
+ let _ = unsafe { Box::from_raw(this_ptr as *mut nativePacket) };
+}
+#[allow(unused)]
+impl Packet {
+ pub(crate) fn get_native_ref(&self) -> &'static nativePacket {
+ unsafe { &*ObjOps::untweak_ptr(self.inner) }
+ }
+ pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativePacket {
+ unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
+ }
+ /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
+ pub(crate) fn take_inner(mut self) -> *mut nativePacket {
+ assert!(self.is_owned);
+ let ret = ObjOps::untweak_ptr(self.inner);
+ self.inner = core::ptr::null_mut();
+ ret
+ }
+}
+/// Bolt 04 version number
+#[no_mangle]
+pub extern "C" fn Packet_get_version(this_ptr: &Packet) -> u8 {
+ let mut inner_val = &mut this_ptr.get_native_mut_ref().version;
+ *inner_val
+}
+/// Bolt 04 version number
+#[no_mangle]
+pub extern "C" fn Packet_set_version(this_ptr: &mut Packet, mut val: u8) {
+ unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.version = val;
+}
+/// A random sepc256k1 point, used to build the ECDH shared secret to decrypt hop_data
+#[no_mangle]
+pub extern "C" fn Packet_get_public_key(this_ptr: &Packet) -> crate::c_types::PublicKey {
+ let mut inner_val = &mut this_ptr.get_native_mut_ref().public_key;
+ crate::c_types::PublicKey::from_rust(&inner_val)
+}
+/// A random sepc256k1 point, used to build the ECDH shared secret to decrypt hop_data
+#[no_mangle]
+pub extern "C" fn Packet_set_public_key(this_ptr: &mut Packet, mut val: crate::c_types::PublicKey) {
+ unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.public_key = val.into_rust();
+}
+/// Encrypted payload for the next hop
+///
+/// Returns a copy of the field.
+#[no_mangle]
+pub extern "C" fn Packet_get_hop_data(this_ptr: &Packet) -> crate::c_types::derived::CVec_u8Z {
+ let mut inner_val = this_ptr.get_native_mut_ref().hop_data.clone();
+ let mut local_inner_val = Vec::new(); for mut item in inner_val.drain(..) { local_inner_val.push( { item }); };
+ local_inner_val.into()
+}
+/// Encrypted payload for the next hop
+#[no_mangle]
+pub extern "C" fn Packet_set_hop_data(this_ptr: &mut Packet, mut val: crate::c_types::derived::CVec_u8Z) {
+ let mut local_val = Vec::new(); for mut item in val.into_rust().drain(..) { local_val.push( { item }); };
+ unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.hop_data = local_val;
+}
+/// HMAC to verify the integrity of hop_data
+#[no_mangle]
+pub extern "C" fn Packet_get_hmac(this_ptr: &Packet) -> *const [u8; 32] {
+ let mut inner_val = &mut this_ptr.get_native_mut_ref().hmac;
+ inner_val
+}
+/// HMAC to verify the integrity of hop_data
+#[no_mangle]
+pub extern "C" fn Packet_set_hmac(this_ptr: &mut Packet, mut val: crate::c_types::ThirtyTwoBytes) {
+ unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.hmac = val.data;
+}
+/// Constructs a new Packet given each field
+#[must_use]
+#[no_mangle]
+pub extern "C" fn Packet_new(mut version_arg: u8, mut public_key_arg: crate::c_types::PublicKey, mut hop_data_arg: crate::c_types::derived::CVec_u8Z, mut hmac_arg: crate::c_types::ThirtyTwoBytes) -> Packet {
+ let mut local_hop_data_arg = Vec::new(); for mut item in hop_data_arg.into_rust().drain(..) { local_hop_data_arg.push( { item }); };
+ Packet { inner: ObjOps::heap_alloc(nativePacket {
+ version: version_arg,
+ public_key: public_key_arg.into_rust(),
+ hop_data: local_hop_data_arg,
+ hmac: hmac_arg.data,
+ }), is_owned: true }
+}
+impl Clone for Packet {
+ fn clone(&self) -> Self {
+ Self {
+ inner: if <*mut nativePacket>::is_null(self.inner) { core::ptr::null_mut() } else {
+ ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
+ is_owned: true,
+ }
+ }
+}
+#[allow(unused)]
+/// Used only if an object of this type is returned as a trait impl by a method
+pub(crate) extern "C" fn Packet_clone_void(this_ptr: *const c_void) -> *mut c_void {
+ Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativePacket)).clone() })) as *mut c_void
+}
+#[no_mangle]
+/// Creates a copy of the Packet
+pub extern "C" fn Packet_clone(orig: &Packet) -> Packet {
+ orig.clone()
+}
+/// Checks if two Packets contain equal inner contents.
+/// This ignores pointers and is_owned flags and looks at the values in fields.
+/// Two objects with NULL inner values will be considered "equal" here.
+#[no_mangle]
+pub extern "C" fn Packet_eq(a: &Packet, b: &Packet) -> bool {
+ if a.inner == b.inner { return true; }
+ if a.inner.is_null() || b.inner.is_null() { return false; }
+ if a.get_native_ref() == b.get_native_ref() { true } else { false }
+}
+#[no_mangle]
+/// Serialize the Packet object into a byte array which can be read by Packet_read
+pub extern "C" fn Packet_write(obj: &crate::lightning::onion_message::packet::Packet) -> crate::c_types::derived::CVec_u8Z {
+ crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
+}
+#[no_mangle]
+pub(crate) extern "C" fn Packet_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
+ crate::c_types::serialize_obj(unsafe { &*(obj as *const nativePacket) })
+}