@@ -12,7 +12,8 @@ LDK C Bindings
12
12
13
13
The C bindings available at include/lightning.h require inclusion of include/rust_types.h first.
14
14
15
- All of the Rust-Lightning types are mapped into C equivalents which take a few forms, namely:
15
+ All of the Rust-Lightning types are mapped into C equivalents which take a few forms, with the C
16
+ type getting an ` LDK ` prefix to their native Rust type names.
16
17
17
18
* Structs are mapped into a simple wrapper containing a pointer to the native Rust-Lightning
18
19
object and a flag to indicate whether the object is owned or only a reference. Such mappings
@@ -36,13 +37,16 @@ All of the Rust-Lightning types are mapped into C equivalents which take a few f
36
37
} LDKChannelManager;
37
38
```
38
39
39
- * Traits are mapped into a concrete struct containing a void pointer and a jump table listing the
40
- functions which the trait must implement. The void pointer may be set to any value and is never
41
- interpreted (or dereferenced) by the bindings logic in any way. It is passed as the first
42
- argument to all function calls in the trait. You may wish to use it as a pointer to your own
43
- internal data structure, though it may also occasionally make sense to e.g. cast a file
40
+ * Traits are mapped into a concrete struct containing a void pointer (named ` this_arg ` and a jump
41
+ table listing the functions which the trait must implement. The void pointer may be set to any
42
+ value and is never interpreted (or dereferenced) by the bindings logic in any way. It is passed
43
+ as the first argument to all function calls in the trait. You may wish to use it as a pointer to
44
+ your own internal data structure, though it may also occasionally make sense to e.g. cast a file
44
45
descriptor into a void pointer and use it to track a socket.
45
46
47
+ This should remind you of a C++ vtable, only written out by hand and with the class only
48
+ containing a pointer, instead of the regular class data.
49
+
46
50
Each trait additionally contains a ` free ` and ` clone ` function pointer, which may be NULL. The
47
51
` free ` function is passed the void pointer when the object is ` Drop ` ed in Rust. The ` clone `
48
52
function is passed the void pointer when the object is ` Clone ` ed in Rust, returning a new void
@@ -76,12 +80,54 @@ All of the Rust-Lightning types are mapped into C equivalents which take a few f
76
80
which must never appear in an enum when accessed by Rust code). The ` Sentinel ` tag is used by
77
81
the C++ wrapper classes to allow moving the ownership of an enum while invalidating the old copy.
78
82
83
+ For example, the unitary enum ` LDKChannelMonitorUpdateErr ` is mapped as follows:
84
+ ```
85
+ typedef enum LDKChannelMonitorUpdateErr {
86
+ /** .. */
87
+ LDKChannelMonitorUpdateErr_TemporaryFailure,
88
+ /** .. */
89
+ LDKChannelMonitorUpdateErr_PermanentFailure,
90
+ /** .. */
91
+ LDKChannelMonitorUpdateErr_Sentinel,
92
+ } LDKChannelMonitorUpdateErr;
93
+ ```
94
+
95
+ and the non-unitary enum LDKErrorAction is mapped as follows:
96
+ ```
97
+ typedef enum LDKErrorAction_Tag {
98
+ /** .. */
99
+ LDKErrorAction_DisconnectPeer,
100
+ /** .. */
101
+ LDKErrorAction_IgnoreError,
102
+ /** .. */
103
+ LDKErrorAction_SendErrorMessage,
104
+ /** .. */
105
+ LDKErrorAction_Sentinel,
106
+ } LDKErrorAction_Tag;
107
+
108
+ typedef struct LDKErrorAction_LDKDisconnectPeer_Body {
109
+ LDKErrorMessage msg;
110
+ } LDKErrorAction_LDKDisconnectPeer_Body;
111
+
112
+ typedef struct LDKErrorAction_LDKSendErrorMessage_Body {
113
+ LDKErrorMessage msg;
114
+ } LDKErrorAction_LDKSendErrorMessage_Body;
115
+
116
+ typedef struct LDKErrorAction {
117
+ LDKErrorAction_Tag tag;
118
+ union {
119
+ LDKErrorAction_LDKDisconnectPeer_Body disconnect_peer;
120
+ LDKErrorAction_LDKSendErrorMessage_Body send_error_message;
121
+ };
122
+ } LDKErrorAction;
123
+ ```
124
+
79
125
* Struct member functions are mapped as ` Struct_function_name ` and take a reference to the mapped
80
126
struct as their first argument. Free-standing functions are mapped simply as ` function_name ` and
81
127
take the relevant mapped type arguments.
82
128
83
129
Functions may return a reference to an underlying Rust object with a mapped struct or an owned
84
- Rust object with the same. The mapped struct contains a flag to indicate if the poitned -to Rust
130
+ Rust object with the same. The mapped struct contains a flag to indicate if the pointed -to Rust
85
131
object is owned or only a reference, and the object's corresponding free function will Do The
86
132
Right Thing based on the flag. In order to determine the expected return type, you should
87
133
reference the Rust documentation for the function.
@@ -107,9 +153,9 @@ The memory model is largely the Rust memory model and not a native C-like memory
107
153
function parameters are largely only ever passed by reference or by move, with pass-by-copy
108
154
semantics only applying to primitive types. However, because the underlying types are largely
109
155
pointers, the same function signature may imply two different memory ownership semantics. Thus, you
110
- MUST read the Rust documentation while using the C bindings. For functions which ownership is moved
111
- to Rust , the corresponding ` X_fre ` e function MUST NOT be called on the object, whereas for all other
112
- objects, ` X_free ` MUST be used to free resources.
156
+ MUST read the Rust documentation while using the C bindings. For functions which take arguments
157
+ where ownership is moved to the function scope , the corresponding ` X_free ` function MUST NOT be
158
+ called on the object, whereas for all other objects, ` X_free ` MUST be used to free resources.
113
159
114
160
LDK C++ Bindings
115
161
================
0 commit comments