mozilla / uniffi-rs

a multi-language bindings generator for rust
https://mozilla.github.io/uniffi-rs/
Mozilla Public License 2.0
2.48k stars 211 forks source link

Using proc-macros for constructor requires Arc<Self> #2158

Closed parthdt closed 1 week ago

parthdt commented 1 week ago

Hello,

I'm trying to export some rust code to swift using uniffi. For the following snippet:

#[derive(uniffi::Record)]
pub struct FzilInput
{
    pub bytes: Vec<u8>
}

#[uniffi::export]
impl FzilInput
{
    #[uniffi::constructor]
        pub const fn new(bytes: Vec<u8>) -> Self {
        Self { bytes }
    }
}

uniffi-bindgen aborts with error: called Result::unwrap() on an Err value: Constructor return type must be Arc<Self>.

As per Proc-macro docs, Self should be a valid return type.

How can this be solved?

jplatte commented 1 week ago

You can't export an impl block for a record type at all. impl blocks are only supported for object types (at the moment).

parthdt commented 1 week ago

Thanks for the response. Exporting the struct as an object does work.

I'm a beginner in both rust, and swift; would you tell me how to use this field: bytes, inside swift? To be precise, Vec<u8> should be [UInt8] in swift, but trying to use FzilInput(bytes), where bytes is of type [UInt8] throws a pointer error.

mhammond commented 1 week ago

It's difficult to know without seeing your code, but our test at https://github.com/mozilla/uniffi-rs/tree/main/fixtures/coverall has extensive coverage of "bytes", so that can probably point you in the right direction.

mhammond commented 1 week ago

It looks like #2159 is handling this bytes request, so I'll close this for now.