Skip to content

Commit 978a8b0

Browse files
committed
f readme - doc updates from val
1 parent 660ec56 commit 978a8b0

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

lightning-c-bindings/README.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ type getting an `LDK` prefix to their native Rust type names.
2424
You MUST NOT create such wrapper structs manually, relying instead on constructors which have
2525
been mapped from equivalent Rust constructors.
2626

27-
Note that, thanks to the is-reference flag, such structs effectively represent both `&RustThing`
28-
and `RustThing`. Further, the same applies to `Option<RustThing>`, in which cases `inner` may be
29-
set to NULL.
27+
Note that, thanks to the is-reference flag and the pointer being NULLable, such structs
28+
effectively represent both `&RustThing`, `RustThing`, and `Option<RustThing>`. Check the
29+
corresponding Rust documentation for the function or struct you are using to ensure you use
30+
the correct call semantics.
3031

3132
For example, this is the mapping of ChannelManager.
3233
```
@@ -50,7 +51,10 @@ type getting an `LDK` prefix to their native Rust type names.
5051
Each trait additionally contains a `free` and `clone` function pointer, which may be NULL. The
5152
`free` function is passed the void pointer when the object is `Drop`ed in Rust. The `clone`
5253
function is passed the void pointer when the object is `Clone`ed in Rust, returning a new void
53-
pointer for the new object.
54+
pointer for the new object. If the `free` pointer is NULL, you will not receive any notification
55+
when the trait is no longer needed. If the `clone` pointer is NULL, we assume that the trait
56+
object may be `memcpy()`'d to create copies. Note that if you release resources with `free`
57+
without implementing `clone`, you will likely end up with use-after-free bugs.
5458

5559
For example, `LDKSocketDescriptor` is mapped as follows:
5660
```
@@ -126,11 +130,11 @@ type getting an `LDK` prefix to their native Rust type names.
126130
struct as their first argument. Free-standing functions are mapped simply as `function_name` and
127131
take the relevant mapped type arguments.
128132

129-
Functions may return a reference to an underlying Rust object with a mapped struct or an owned
130-
Rust object with the same. The mapped struct contains a flag to indicate if the pointed-to Rust
131-
object is owned or only a reference, and the object's corresponding free function will Do The
132-
Right Thing based on the flag. In order to determine the expected return type, you should
133-
reference the Rust documentation for the function.
133+
Functions which return `&OpaqueRustType` and which return `OpaqueRustType` are both mapped to a
134+
function returning an owned wrapper struct. The `_underlying_ref` flag (see above) will be set
135+
to indicate that the pointed-to Rust object is owned or only a reference. Thus, when
136+
implementing a function which Rust will call or calling a Rust function, you should check the
137+
Rust documentation for the function to determine which behavior is expected.
134138

135139
Similarly, when a function takes an `Option<RustType>` as a parameter or a return value, the C
136140
type is the same as if it took only `RustType`, with the `inner` field set to NULL to indicate

0 commit comments

Comments
 (0)