Add automatically generated C/C++ wrapper bindings
[rust-lightning] / 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
24 use lightning::ln::features::InitFeatures as nativeInitFeaturesImport;
25 type nativeInitFeatures = nativeInitFeaturesImport;
26
27 /// Features used within an `init` message.
28 #[must_use]
29 #[repr(C)]
30 pub struct InitFeatures {
31         /// Nearly everyhwere, inner must be non-null, however in places where
32         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
33         pub inner: *mut nativeInitFeatures,
34         pub is_owned: bool,
35 }
36
37 impl Drop for InitFeatures {
38         fn drop(&mut self) {
39                 if self.is_owned && !self.inner.is_null() {
40                         let _ = unsafe { Box::from_raw(self.inner) };
41                 }
42         }
43 }
44 #[no_mangle]
45 pub extern "C" fn InitFeatures_free(this_ptr: InitFeatures) { }
46 #[allow(unused)]
47 /// Used only if an object of this type is returned as a trait impl by a method
48 extern "C" fn InitFeatures_free_void(this_ptr: *mut c_void) {
49         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeInitFeatures); }
50 }
51 #[allow(unused)]
52 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
53 impl InitFeatures {
54         pub(crate) fn take_ptr(mut self) -> *mut nativeInitFeatures {
55                 assert!(self.is_owned);
56                 let ret = self.inner;
57                 self.inner = std::ptr::null_mut();
58                 ret
59         }
60 }
61
62 use lightning::ln::features::NodeFeatures as nativeNodeFeaturesImport;
63 type nativeNodeFeatures = nativeNodeFeaturesImport;
64
65 /// Features used within a `node_announcement` message.
66 #[must_use]
67 #[repr(C)]
68 pub struct NodeFeatures {
69         /// Nearly everyhwere, inner must be non-null, however in places where
70         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
71         pub inner: *mut nativeNodeFeatures,
72         pub is_owned: bool,
73 }
74
75 impl Drop for NodeFeatures {
76         fn drop(&mut self) {
77                 if self.is_owned && !self.inner.is_null() {
78                         let _ = unsafe { Box::from_raw(self.inner) };
79                 }
80         }
81 }
82 #[no_mangle]
83 pub extern "C" fn NodeFeatures_free(this_ptr: NodeFeatures) { }
84 #[allow(unused)]
85 /// Used only if an object of this type is returned as a trait impl by a method
86 extern "C" fn NodeFeatures_free_void(this_ptr: *mut c_void) {
87         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeNodeFeatures); }
88 }
89 #[allow(unused)]
90 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
91 impl NodeFeatures {
92         pub(crate) fn take_ptr(mut self) -> *mut nativeNodeFeatures {
93                 assert!(self.is_owned);
94                 let ret = self.inner;
95                 self.inner = std::ptr::null_mut();
96                 ret
97         }
98 }
99
100 use lightning::ln::features::ChannelFeatures as nativeChannelFeaturesImport;
101 type nativeChannelFeatures = nativeChannelFeaturesImport;
102
103 /// Features used within a `channel_announcement` message.
104 #[must_use]
105 #[repr(C)]
106 pub struct ChannelFeatures {
107         /// Nearly everyhwere, inner must be non-null, however in places where
108         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
109         pub inner: *mut nativeChannelFeatures,
110         pub is_owned: bool,
111 }
112
113 impl Drop for ChannelFeatures {
114         fn drop(&mut self) {
115                 if self.is_owned && !self.inner.is_null() {
116                         let _ = unsafe { Box::from_raw(self.inner) };
117                 }
118         }
119 }
120 #[no_mangle]
121 pub extern "C" fn ChannelFeatures_free(this_ptr: ChannelFeatures) { }
122 #[allow(unused)]
123 /// Used only if an object of this type is returned as a trait impl by a method
124 extern "C" fn ChannelFeatures_free_void(this_ptr: *mut c_void) {
125         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeChannelFeatures); }
126 }
127 #[allow(unused)]
128 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
129 impl ChannelFeatures {
130         pub(crate) fn take_ptr(mut self) -> *mut nativeChannelFeatures {
131                 assert!(self.is_owned);
132                 let ret = self.inner;
133                 self.inner = std::ptr::null_mut();
134                 ret
135         }
136 }