Matt Corallo [Mon, 26 Aug 2024 19:15:57 +0000 (19:15 +0000)]
Handle writing Rust types for single-impl traits
Single-impl traits which are marked no-export (like
`AChannelManager` in LDK) are mapped as the singular implementing
type, rather than a full-blown trait.
Here we handle this case when writing Rust objects (generally used
in writing generic bounds).
Matt Corallo [Mon, 26 Aug 2024 19:05:06 +0000 (19:05 +0000)]
impl `Send`/`Sync`/`Deref`/deref method for opaque wrapper structs
This adds a "same item but as a reference/non-`is_owned`" helper
method for our opaque wrapper structs, as well as implements
`Deref` on them to the native type. Finally, it implements `Send`
and `Sync` on them by blindly assuming it is implemented on the
underlying struct.
Matt Corallo [Mon, 26 Aug 2024 18:52:07 +0000 (18:52 +0000)]
Add a `*Ref` struct for traits and `Deref` to it rather than `Self`
Because LDK often takes `Deref<Target=Trait>` as type parameters,
we'd implemented `Deref { type Target=Self; .. }` for the traits
defined in the bindings crate. This worked well, but because Rust
auto-`Deref`s, it can lead to spurious compilation failures due to
infinite recursion trying to deref.
In the past we've worked around this by coming up with alternative
compilation strategies when faced with `Deref` recursion, but we
don't strictly need to.
Instead, here, we introduce duplicates of all our `Trait` structs
which become the `Deref` `Target`. This way, we can `Deref` into
the `Trait` and maintain LDK compatibility, without having any
infinite `Deref` recursion issues.
One complication is traits which contain associated types to define
`Deref`s to another associated type, e.g.
trait A {
type B;
type C: Deref<Target = Self::B>;
}
In this case, `B` needs to be the `TraitRef` and `C` needs to be
the `Trait`. We add code specifically to detect this case.
Matt Corallo [Tue, 4 Jun 2024 17:47:02 +0000 (17:47 +0000)]
Handle `Vec`s resolved as `alloc::vec::Vec`
Up until now we'd always relied on `Vec`s being imported through a
prelude, but LDK sometimes forgets to keep doing that. Thus, here
we move to also accepting `alloc::vec::Vec` by mapping it to `Vec`
during name resolution.
Matt Corallo [Tue, 23 Jan 2024 22:31:55 +0000 (22:31 +0000)]
Expose accessors for `TxIn` and `TxOut`
The fields themselves are already public, so there's not a lot of
value for C users for these, but it avoids having to have this
logic written out by hand in the Java bindings, which is nice.
Matt Corallo [Fri, 29 Dec 2023 20:50:22 +0000 (20:50 +0000)]
Support passing lifetime'd enums to C
While this is generally unsafe, we have to do something for
`CandidateRouteHop`, which is passed to the scorer. Because its
incredibly performance-sensitive, forcing the bindings users to
take a performance hit just to make the scoring interface marginally
safer seems like a bad trade-off.
Matt Corallo [Sun, 29 Oct 2023 18:40:47 +0000 (18:40 +0000)]
Fix race in peer connection in C++ demo app
When we disconnect then immediately reconnect from one peer we may
still get the second connection through before the disconnection is
handled on the other end, causing connection failures. We fix this
here by disconnecting on both ends before reconnecting.
Matt Corallo [Mon, 16 Oct 2023 01:35:28 +0000 (01:35 +0000)]
Drop `#[no_mangle]` from `*_write_void` to make cbindgen not export
cbindgen recently switched to exporting all `#[no_mangle]` and
`extern "C"` functions. Our `*_write_void` methods are
internal-only but we'd marked them `#[no_mangle]`, which resulted
in them spuriously appearing in `lightning.h`.
Matt Corallo [Sun, 8 Oct 2023 05:53:25 +0000 (05:53 +0000)]
Update args on supertraits of supertraits when cloning trait impls
When cloning implementations of traits from in-crate structs, we
forgot to update the arguments for supertraits of supertraits,
causing methods on that trait to be called against the previous
struct, not the cloned one.
This was ultimately identified downstream in the Java bindings,
fixes https://github.com/lightningdevkit/ldk-garbagecollected/issues/138
Matt Corallo [Thu, 28 Sep 2023 01:14:41 +0000 (01:14 +0000)]
Use the resolved type name in generic strings, not the original
This makes some generics less readable, but some more, and reduces
generic type pollution. Some cases are a loss, eg
`Option_PaymentHashZ` gets converted to `Option_ThirtyTwoBytesZ`
(which less less readable but fewer total types as a result), but
we ultimately need this because we now have `schnorr::Signature`
and `ecdsa::Signature` so we can't rely on just using `Signature`
and having it be unique :(.
Matt Corallo [Sun, 30 Jul 2023 03:36:56 +0000 (03:36 +0000)]
Include a `struct` before trait return fields
EIther `struct Type` or `Type` work fine because of our type
declarations, but cbindgen uses `struct Type` everywhere so its
easiest if we match it, especially for downstream code.
Matt Corallo [Sat, 22 Jul 2023 20:03:11 +0000 (20:03 +0000)]
Allow generation of manual types in reference options
The limiting of supported types in `write_template_generics` isn't
super useful as its only called once per type - later uses of the
same type with the reference flag set or wiped doesn't change
anything. Still, it is relied on somewhat, so we can't drop it
entirely.
Now that we've moved more of the `[u8; 32]` newtypes to option
wrappers, failing for non-primitive references spuriously breaks.
Instead, we just allow it for manually-mapped types and let the
compiler complain if we do something invalid.
Matt Corallo [Thu, 20 Jul 2023 22:51:01 +0000 (22:51 +0000)]
Handle all traits in type resolution, including no-export ones
In the previous commit, we set up handling of no-export traits with
only a single implementor. In order to utilize that, we need such
traits to participate in type resolution, which we add support for
here.
Matt Corallo [Mon, 17 Jul 2023 02:16:01 +0000 (02:16 +0000)]
Handle no-export traits which are only implemented once as the implementor
If we have a trait marked no-export, and there's a single
implementor, we can just pretend its always the implementor. This
makes sense in LDK, for example, for the `APeerManager` trait,
which is implemented for all `PeerManager`s, but doesn't make sense
in bindings.