Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add implemented traits to generated code, or add extension? #2169

Open
praveenperera opened this issue Jun 23, 2024 · 2 comments
Open

Add implemented traits to generated code, or add extension? #2169

praveenperera opened this issue Jun 23, 2024 · 2 comments

Comments

@praveenperera
Copy link
Contributor

praveenperera commented Jun 23, 2024

I have the following trait

use crate::impl_default_for;

#[uniffi::export(with_foreign)]
pub trait AutoComplete: Send + Sync + std::fmt::Debug + 'static {
    fn autocomplete(&self, word: String) -> Vec<String>;
}

#[derive(Debug, Copy, Clone, uniffi::Object)]
pub struct Bip39AutoComplete;


#[uniffi::export]
impl Bip39AutoComplete {
    #[uniffi::constructor]
    pub fn new() -> Self {
        Self
    }
}

#[uniffi::export]
impl AutoComplete for Bip39AutoComplete {
    fn autocomplete(&self, word: String) -> Vec<String> {
        // todo: implement
        vec![]
    }
}

And this SwiftUI component

struct AutocompleteField<AutoCompleter: AutoComplete>: View {
    let autocompleter: AutoCompleter
    @Binding var text: String

   ...

But I can't automatically pass in Bip39AutoComplete into the SwiftUI component. To get it to work I have to do the following on the swift side.

extension Bip39AutoComplete: AutoComplete {}

It's not a big deal, just one line, but I was wondering if it could be added to the already generated code as below:

- open class Bip39AutoComplete: Bip39AutoCompleteProtocol {
+ open class Bip39AutoComplete: Bip39AutoCompleteProtocol, AutoComplete {
@praveenperera praveenperera changed the title Auto generate extension STRUCT_NAME: Trait {} ? Auto generate extension STRUCT_NAME: Trait {} for swift ? Jun 23, 2024
@praveenperera
Copy link
Contributor Author

praveenperera commented Jun 24, 2024

In android I can do this

open class Bip39AutoComplete : org.bitcoinppl.cove.Bip39AutoComplete(), AutoComplete {}

Though it probably just makes sense to add them directly to already generated kt file

open class Bip39AutoComplete :
    Disposable,
    AutoCloseable,
-    Bip39AutoCompleteInterface {
+    Bip39AutoCompleteInterface, AutoComplete {
    constructor(pointer: Pointer) {}

@praveenperera praveenperera changed the title Auto generate extension STRUCT_NAME: Trait {} for swift ? Add implemented traits to generated code? Jun 24, 2024
@praveenperera praveenperera changed the title Add implemented traits to generated code? Add implemented traits to generated code, or add extension? Jun 24, 2024
@mhammond
Copy link
Member

mhammond commented Jul 4, 2024

This is likely to be tricky for a few reasons - one is capturing the metadata, but the other is mapping the semantics to all supported bindings once we end up with many such traits (ie, with multiple inheritance) - eg, Kotlin has a very strange inheritance story.

I wonder if we can instead come up with a way so that the swift binding generator alone can be told (eg, via uniffi.toml) about the desired hierarchy or similar?) But regardless, I don't see an easy answer here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants