Rebuild against current RL main
[ldk-c-bindings] / lightning-c-bindings / src / ln / features.rs
1 //! Feature flag definitions for the Lightning protocol according to [BOLT #9].
2 //!
3 //! Lightning nodes advertise a supported set of operation through feature flags. Features are
4 //! applicable for a specific context as indicated in some [messages]. [`Features`] encapsulates
5 //! behavior for specifying and checking feature flags for a particular context. Each feature is
6 //! defined internally by a trait specifying the corresponding flags (i.e., even and odd bits). A
7 //! [`Context`] is used to parameterize [`Features`] and defines which features it can support.
8 //!
9 //! Whether a feature is considered \"known\" or \"unknown\" is relative to the implementation, whereas
10 //! the term \"supports\" is used in reference to a particular set of [`Features`]. That is, a node
11 //! supports a feature if it advertises the feature (as either required or optional) to its peers.
12 //! And the implementation can interpret a feature if the feature is known to it.
13 //!
14 //! [BOLT #9]: https://github.com/lightningnetwork/lightning-rfc/blob/master/09-features.md
15 //! [messages]: ../msgs/index.html
16 //! [`Features`]: struct.Features.html
17 //! [`Context`]: sealed/trait.Context.html
18
19 use std::ffi::c_void;
20 use bitcoin::hashes::Hash;
21 use crate::c_types::*;
22
23 impl Clone for InitFeatures {
24         fn clone(&self) -> Self {
25                 Self {
26                         inner: if <*mut nativeInitFeatures>::is_null(self.inner) { std::ptr::null_mut() } else {
27                                 Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
28                         is_owned: true,
29                 }
30         }
31 }
32 #[allow(unused)]
33 /// Used only if an object of this type is returned as a trait impl by a method
34 pub(crate) extern "C" fn InitFeatures_clone_void(this_ptr: *const c_void) -> *mut c_void {
35         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeInitFeatures)).clone() })) as *mut c_void
36 }
37 #[no_mangle]
38 /// Creates a copy of the InitFeatures
39 pub extern "C" fn InitFeatures_clone(orig: &InitFeatures) -> InitFeatures {
40         orig.clone()
41 }
42 impl Clone for NodeFeatures {
43         fn clone(&self) -> Self {
44                 Self {
45                         inner: if <*mut nativeNodeFeatures>::is_null(self.inner) { std::ptr::null_mut() } else {
46                                 Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
47                         is_owned: true,
48                 }
49         }
50 }
51 #[allow(unused)]
52 /// Used only if an object of this type is returned as a trait impl by a method
53 pub(crate) extern "C" fn NodeFeatures_clone_void(this_ptr: *const c_void) -> *mut c_void {
54         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeNodeFeatures)).clone() })) as *mut c_void
55 }
56 #[no_mangle]
57 /// Creates a copy of the NodeFeatures
58 pub extern "C" fn NodeFeatures_clone(orig: &NodeFeatures) -> NodeFeatures {
59         orig.clone()
60 }
61 impl Clone for ChannelFeatures {
62         fn clone(&self) -> Self {
63                 Self {
64                         inner: if <*mut nativeChannelFeatures>::is_null(self.inner) { std::ptr::null_mut() } else {
65                                 Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
66                         is_owned: true,
67                 }
68         }
69 }
70 #[allow(unused)]
71 /// Used only if an object of this type is returned as a trait impl by a method
72 pub(crate) extern "C" fn ChannelFeatures_clone_void(this_ptr: *const c_void) -> *mut c_void {
73         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeChannelFeatures)).clone() })) as *mut c_void
74 }
75 #[no_mangle]
76 /// Creates a copy of the ChannelFeatures
77 pub extern "C" fn ChannelFeatures_clone(orig: &ChannelFeatures) -> ChannelFeatures {
78         orig.clone()
79 }
80 impl Clone for InvoiceFeatures {
81         fn clone(&self) -> Self {
82                 Self {
83                         inner: if <*mut nativeInvoiceFeatures>::is_null(self.inner) { std::ptr::null_mut() } else {
84                                 Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
85                         is_owned: true,
86                 }
87         }
88 }
89 #[allow(unused)]
90 /// Used only if an object of this type is returned as a trait impl by a method
91 pub(crate) extern "C" fn InvoiceFeatures_clone_void(this_ptr: *const c_void) -> *mut c_void {
92         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeInvoiceFeatures)).clone() })) as *mut c_void
93 }
94 #[no_mangle]
95 /// Creates a copy of the InvoiceFeatures
96 pub extern "C" fn InvoiceFeatures_clone(orig: &InvoiceFeatures) -> InvoiceFeatures {
97         orig.clone()
98 }
99
100 use lightning::ln::features::InitFeatures as nativeInitFeaturesImport;
101 type nativeInitFeatures = nativeInitFeaturesImport;
102
103 /// Features used within an `init` message.
104 #[must_use]
105 #[repr(C)]
106 pub struct InitFeatures {
107         /// A pointer to the opaque Rust object.
108
109         /// Nearly everywhere, inner must be non-null, however in places where
110         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
111         pub inner: *mut nativeInitFeatures,
112         /// Indicates that this is the only struct which contains the same pointer.
113
114         /// Rust functions which take ownership of an object provided via an argument require
115         /// this to be true and invalidate the object pointed to by inner.
116         pub is_owned: bool,
117 }
118
119 impl Drop for InitFeatures {
120         fn drop(&mut self) {
121                 if self.is_owned && !<*mut nativeInitFeatures>::is_null(self.inner) {
122                         let _ = unsafe { Box::from_raw(self.inner) };
123                 }
124         }
125 }
126 /// Frees any resources used by the InitFeatures, if is_owned is set and inner is non-NULL.
127 #[no_mangle]
128 pub extern "C" fn InitFeatures_free(this_obj: InitFeatures) { }
129 #[allow(unused)]
130 /// Used only if an object of this type is returned as a trait impl by a method
131 extern "C" fn InitFeatures_free_void(this_ptr: *mut c_void) {
132         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeInitFeatures); }
133 }
134 #[allow(unused)]
135 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
136 impl InitFeatures {
137         pub(crate) fn take_inner(mut self) -> *mut nativeInitFeatures {
138                 assert!(self.is_owned);
139                 let ret = self.inner;
140                 self.inner = std::ptr::null_mut();
141                 ret
142         }
143 }
144
145 use lightning::ln::features::NodeFeatures as nativeNodeFeaturesImport;
146 type nativeNodeFeatures = nativeNodeFeaturesImport;
147
148 /// Features used within a `node_announcement` message.
149 #[must_use]
150 #[repr(C)]
151 pub struct NodeFeatures {
152         /// A pointer to the opaque Rust object.
153
154         /// Nearly everywhere, inner must be non-null, however in places where
155         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
156         pub inner: *mut nativeNodeFeatures,
157         /// Indicates that this is the only struct which contains the same pointer.
158
159         /// Rust functions which take ownership of an object provided via an argument require
160         /// this to be true and invalidate the object pointed to by inner.
161         pub is_owned: bool,
162 }
163
164 impl Drop for NodeFeatures {
165         fn drop(&mut self) {
166                 if self.is_owned && !<*mut nativeNodeFeatures>::is_null(self.inner) {
167                         let _ = unsafe { Box::from_raw(self.inner) };
168                 }
169         }
170 }
171 /// Frees any resources used by the NodeFeatures, if is_owned is set and inner is non-NULL.
172 #[no_mangle]
173 pub extern "C" fn NodeFeatures_free(this_obj: NodeFeatures) { }
174 #[allow(unused)]
175 /// Used only if an object of this type is returned as a trait impl by a method
176 extern "C" fn NodeFeatures_free_void(this_ptr: *mut c_void) {
177         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeNodeFeatures); }
178 }
179 #[allow(unused)]
180 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
181 impl NodeFeatures {
182         pub(crate) fn take_inner(mut self) -> *mut nativeNodeFeatures {
183                 assert!(self.is_owned);
184                 let ret = self.inner;
185                 self.inner = std::ptr::null_mut();
186                 ret
187         }
188 }
189
190 use lightning::ln::features::ChannelFeatures as nativeChannelFeaturesImport;
191 type nativeChannelFeatures = nativeChannelFeaturesImport;
192
193 /// Features used within a `channel_announcement` message.
194 #[must_use]
195 #[repr(C)]
196 pub struct ChannelFeatures {
197         /// A pointer to the opaque Rust object.
198
199         /// Nearly everywhere, inner must be non-null, however in places where
200         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
201         pub inner: *mut nativeChannelFeatures,
202         /// Indicates that this is the only struct which contains the same pointer.
203
204         /// Rust functions which take ownership of an object provided via an argument require
205         /// this to be true and invalidate the object pointed to by inner.
206         pub is_owned: bool,
207 }
208
209 impl Drop for ChannelFeatures {
210         fn drop(&mut self) {
211                 if self.is_owned && !<*mut nativeChannelFeatures>::is_null(self.inner) {
212                         let _ = unsafe { Box::from_raw(self.inner) };
213                 }
214         }
215 }
216 /// Frees any resources used by the ChannelFeatures, if is_owned is set and inner is non-NULL.
217 #[no_mangle]
218 pub extern "C" fn ChannelFeatures_free(this_obj: ChannelFeatures) { }
219 #[allow(unused)]
220 /// Used only if an object of this type is returned as a trait impl by a method
221 extern "C" fn ChannelFeatures_free_void(this_ptr: *mut c_void) {
222         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeChannelFeatures); }
223 }
224 #[allow(unused)]
225 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
226 impl ChannelFeatures {
227         pub(crate) fn take_inner(mut self) -> *mut nativeChannelFeatures {
228                 assert!(self.is_owned);
229                 let ret = self.inner;
230                 self.inner = std::ptr::null_mut();
231                 ret
232         }
233 }
234
235 use lightning::ln::features::InvoiceFeatures as nativeInvoiceFeaturesImport;
236 type nativeInvoiceFeatures = nativeInvoiceFeaturesImport;
237
238 /// Features used within an invoice.
239 #[must_use]
240 #[repr(C)]
241 pub struct InvoiceFeatures {
242         /// A pointer to the opaque Rust object.
243
244         /// Nearly everywhere, inner must be non-null, however in places where
245         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
246         pub inner: *mut nativeInvoiceFeatures,
247         /// Indicates that this is the only struct which contains the same pointer.
248
249         /// Rust functions which take ownership of an object provided via an argument require
250         /// this to be true and invalidate the object pointed to by inner.
251         pub is_owned: bool,
252 }
253
254 impl Drop for InvoiceFeatures {
255         fn drop(&mut self) {
256                 if self.is_owned && !<*mut nativeInvoiceFeatures>::is_null(self.inner) {
257                         let _ = unsafe { Box::from_raw(self.inner) };
258                 }
259         }
260 }
261 /// Frees any resources used by the InvoiceFeatures, if is_owned is set and inner is non-NULL.
262 #[no_mangle]
263 pub extern "C" fn InvoiceFeatures_free(this_obj: InvoiceFeatures) { }
264 #[allow(unused)]
265 /// Used only if an object of this type is returned as a trait impl by a method
266 extern "C" fn InvoiceFeatures_free_void(this_ptr: *mut c_void) {
267         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeInvoiceFeatures); }
268 }
269 #[allow(unused)]
270 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
271 impl InvoiceFeatures {
272         pub(crate) fn take_inner(mut self) -> *mut nativeInvoiceFeatures {
273                 assert!(self.is_owned);
274                 let ret = self.inner;
275                 self.inner = std::ptr::null_mut();
276                 ret
277         }
278 }
279 /// Create a blank Features with no features set
280 #[must_use]
281 #[no_mangle]
282 pub extern "C" fn InitFeatures_empty() -> InitFeatures {
283         let mut ret = lightning::ln::features::InitFeatures::empty();
284         InitFeatures { inner: Box::into_raw(Box::new(ret)), is_owned: true }
285 }
286
287 /// Creates features known by the implementation as defined by [`T::KNOWN_FEATURE_FLAGS`].
288 ///
289 /// [`T::KNOWN_FEATURE_FLAGS`]: sealed/trait.Context.html#associatedconstant.KNOWN_FEATURE_FLAGS
290 #[must_use]
291 #[no_mangle]
292 pub extern "C" fn InitFeatures_known() -> InitFeatures {
293         let mut ret = lightning::ln::features::InitFeatures::known();
294         InitFeatures { inner: Box::into_raw(Box::new(ret)), is_owned: true }
295 }
296
297 /// Create a blank Features with no features set
298 #[must_use]
299 #[no_mangle]
300 pub extern "C" fn NodeFeatures_empty() -> NodeFeatures {
301         let mut ret = lightning::ln::features::NodeFeatures::empty();
302         NodeFeatures { inner: Box::into_raw(Box::new(ret)), is_owned: true }
303 }
304
305 /// Creates features known by the implementation as defined by [`T::KNOWN_FEATURE_FLAGS`].
306 ///
307 /// [`T::KNOWN_FEATURE_FLAGS`]: sealed/trait.Context.html#associatedconstant.KNOWN_FEATURE_FLAGS
308 #[must_use]
309 #[no_mangle]
310 pub extern "C" fn NodeFeatures_known() -> NodeFeatures {
311         let mut ret = lightning::ln::features::NodeFeatures::known();
312         NodeFeatures { inner: Box::into_raw(Box::new(ret)), is_owned: true }
313 }
314
315 /// Create a blank Features with no features set
316 #[must_use]
317 #[no_mangle]
318 pub extern "C" fn ChannelFeatures_empty() -> ChannelFeatures {
319         let mut ret = lightning::ln::features::ChannelFeatures::empty();
320         ChannelFeatures { inner: Box::into_raw(Box::new(ret)), is_owned: true }
321 }
322
323 /// Creates features known by the implementation as defined by [`T::KNOWN_FEATURE_FLAGS`].
324 ///
325 /// [`T::KNOWN_FEATURE_FLAGS`]: sealed/trait.Context.html#associatedconstant.KNOWN_FEATURE_FLAGS
326 #[must_use]
327 #[no_mangle]
328 pub extern "C" fn ChannelFeatures_known() -> ChannelFeatures {
329         let mut ret = lightning::ln::features::ChannelFeatures::known();
330         ChannelFeatures { inner: Box::into_raw(Box::new(ret)), is_owned: true }
331 }
332
333 /// Create a blank Features with no features set
334 #[must_use]
335 #[no_mangle]
336 pub extern "C" fn InvoiceFeatures_empty() -> InvoiceFeatures {
337         let mut ret = lightning::ln::features::InvoiceFeatures::empty();
338         InvoiceFeatures { inner: Box::into_raw(Box::new(ret)), is_owned: true }
339 }
340
341 /// Creates features known by the implementation as defined by [`T::KNOWN_FEATURE_FLAGS`].
342 ///
343 /// [`T::KNOWN_FEATURE_FLAGS`]: sealed/trait.Context.html#associatedconstant.KNOWN_FEATURE_FLAGS
344 #[must_use]
345 #[no_mangle]
346 pub extern "C" fn InvoiceFeatures_known() -> InvoiceFeatures {
347         let mut ret = lightning::ln::features::InvoiceFeatures::known();
348         InvoiceFeatures { inner: Box::into_raw(Box::new(ret)), is_owned: true }
349 }
350
351 #[no_mangle]
352 /// Serialize the InitFeatures object into a byte array which can be read by InitFeatures_read
353 pub extern "C" fn InitFeatures_write(obj: &InitFeatures) -> crate::c_types::derived::CVec_u8Z {
354         crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner })
355 }
356 #[no_mangle]
357 pub(crate) extern "C" fn InitFeatures_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
358         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeInitFeatures) })
359 }
360 #[no_mangle]
361 /// Serialize the NodeFeatures object into a byte array which can be read by NodeFeatures_read
362 pub extern "C" fn NodeFeatures_write(obj: &NodeFeatures) -> crate::c_types::derived::CVec_u8Z {
363         crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner })
364 }
365 #[no_mangle]
366 pub(crate) extern "C" fn NodeFeatures_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
367         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeNodeFeatures) })
368 }
369 #[no_mangle]
370 /// Serialize the ChannelFeatures object into a byte array which can be read by ChannelFeatures_read
371 pub extern "C" fn ChannelFeatures_write(obj: &ChannelFeatures) -> crate::c_types::derived::CVec_u8Z {
372         crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner })
373 }
374 #[no_mangle]
375 pub(crate) extern "C" fn ChannelFeatures_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
376         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeChannelFeatures) })
377 }
378 #[no_mangle]
379 /// Serialize the InvoiceFeatures object into a byte array which can be read by InvoiceFeatures_read
380 pub extern "C" fn InvoiceFeatures_write(obj: &InvoiceFeatures) -> crate::c_types::derived::CVec_u8Z {
381         crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner })
382 }
383 #[no_mangle]
384 pub(crate) extern "C" fn InvoiceFeatures_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
385         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeInvoiceFeatures) })
386 }
387 #[no_mangle]
388 /// Read a InitFeatures from a byte array, created by InitFeatures_write
389 pub extern "C" fn InitFeatures_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_InitFeaturesDecodeErrorZ {
390         let res = crate::c_types::deserialize_obj(ser);
391         let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::ln::features::InitFeatures { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::DecodeError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
392         local_res
393 }
394 #[no_mangle]
395 /// Read a NodeFeatures from a byte array, created by NodeFeatures_write
396 pub extern "C" fn NodeFeatures_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_NodeFeaturesDecodeErrorZ {
397         let res = crate::c_types::deserialize_obj(ser);
398         let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::ln::features::NodeFeatures { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::DecodeError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
399         local_res
400 }
401 #[no_mangle]
402 /// Read a ChannelFeatures from a byte array, created by ChannelFeatures_write
403 pub extern "C" fn ChannelFeatures_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_ChannelFeaturesDecodeErrorZ {
404         let res = crate::c_types::deserialize_obj(ser);
405         let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::ln::features::ChannelFeatures { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::DecodeError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
406         local_res
407 }
408 #[no_mangle]
409 /// Read a InvoiceFeatures from a byte array, created by InvoiceFeatures_write
410 pub extern "C" fn InvoiceFeatures_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_InvoiceFeaturesDecodeErrorZ {
411         let res = crate::c_types::deserialize_obj(ser);
412         let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::ln::features::InvoiceFeatures { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::DecodeError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
413         local_res
414 }