Skip to content

Commit 660ec56

Browse files
committed
f readme - a few tweaks from arik
1 parent 72be89c commit 660ec56

File tree

1 file changed

+56
-10
lines changed

1 file changed

+56
-10
lines changed

lightning-c-bindings/README.md

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ LDK C Bindings
1212

1313
The C bindings available at include/lightning.h require inclusion of include/rust_types.h first.
1414

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.
1617

1718
* Structs are mapped into a simple wrapper containing a pointer to the native Rust-Lightning
1819
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
3637
} LDKChannelManager;
3738
```
3839

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
4445
descriptor into a void pointer and use it to track a socket.
4546

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+
4650
Each trait additionally contains a `free` and `clone` function pointer, which may be NULL. The
4751
`free` function is passed the void pointer when the object is `Drop`ed in Rust. The `clone`
4852
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
7680
which must never appear in an enum when accessed by Rust code). The `Sentinel` tag is used by
7781
the C++ wrapper classes to allow moving the ownership of an enum while invalidating the old copy.
7882

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+
79125
* Struct member functions are mapped as `Struct_function_name` and take a reference to the mapped
80126
struct as their first argument. Free-standing functions are mapped simply as `function_name` and
81127
take the relevant mapped type arguments.
82128

83129
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
85131
object is owned or only a reference, and the object's corresponding free function will Do The
86132
Right Thing based on the flag. In order to determine the expected return type, you should
87133
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
107153
function parameters are largely only ever passed by reference or by move, with pass-by-copy
108154
semantics only applying to primitive types. However, because the underlying types are largely
109155
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.
113159

114160
LDK C++ Bindings
115161
================

0 commit comments

Comments
 (0)