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

Implement trait deriving for RustAutoOpaque (Clone, Debug, PartialEq, Eq) #2272

Closed
patmuk opened this issue Sep 4, 2024 · 6 comments · Fixed by #2293
Closed

Implement trait deriving for RustAutoOpaque (Clone, Debug, PartialEq, Eq) #2272

patmuk opened this issue Sep 4, 2024 · 6 comments · Fixed by #2293
Labels
awaiting Waiting for responses, PR, further discussions, upstream release, etc enhancement New feature or request

Comments

@patmuk
Copy link
Contributor

patmuk commented Sep 4, 2024

Describe the bug

#[derive(Debug, PartialEq, Eq, Default, Clone)]
pub struct MyStruct {
    pub content: RustAutoOpaque<String>,
}

leads to the compiler error

error[E0277]: `RustAutoOpaqueBase<String, MoiArc<RustAutoOpaqueInner<String>>>` doesn't implement `std::fmt::Debug`
 --> app_core/src/domain/example.rs:5:5
  |
3 | #[derive(Debug, PartialEq, Eq, Default, Clone)]
  |          ----- in this derive macro expansion
4 | pub struct MyStruct {
5 |     pub content: RustAutoOpaque<String>,
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `RustAutoOpaqueBase<String, MoiArc<RustAutoOpaqueInner<String>>>` cannot be formatted using `{:?}` because it doesn't implement `std::fmt::Debug`
  |
  = note: the full name for the type has been written to '/Users/patmuk/code/own/sherry/examples/flutter-rust-bridge_crux_style/target/debug/deps/app_core-7c266d89184dc8da.long-type-9818281645063396442.txt'
  = note: consider using `--verbose` to print the full type name to the console
  = help: the trait `std::fmt::Debug` is not implemented for `RustAutoOpaqueBase<String, MoiArc<RustAutoOpaqueInner<String>>>`, which is required by `&flutter_rust_bridge::for_generated::RustAutoOpaqueBase<std::string::String, application::bridge::frb_generated::MoiArc<flutter_rust_bridge::for_generated::RustAutoOpaqueInner<std::string::String>>>: std::fmt::Debug`
  = note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)

Steps to reproduce

  1. copy the code
  2. run frb generate
  3. see cargo's error message

Logs

see the error in the description

Expected behavior

RustAutoOpaque goes out of the way and uses the underlying type's derived traits.

Generated binding code

No response

OS

No response

Version of flutter_rust_bridge_codegen

No response

Flutter info

No response

Version of clang++

No response

Additional context

A workaround is to implement the traits on the wrapped type, for example

struct MyStruct {
   content: RustAutoOpaque<String>
}

needs to wrap the string, so we can implement a trait

struct content {
  inner: String,
}

struct MyStruct {
   content: RustAutoOpaque<Content>
}

and then implement the missing trait

    fn clone(&self) -> Self {
        panic!("I should not be called");
    }

While Clone is needed (even if not called, see #2244 (comment)), the other traits are more of a "nice to have".

It should be as easy as

impl Display for RustAutoOpaque<T> {
   fn display(&self) -> &str {
     self.inner.display
  }
}

though the devil might be in the details :)

Having

impl Clone for RustAutoOpaque{
    fn clone(&self) -> Self {
        panic!("I should not be called");
    }
}

might be sufficient for now :)

@patmuk patmuk added the bug Something isn't working label Sep 4, 2024
@fzyzcjy fzyzcjy added enhancement New feature or request awaiting Waiting for responses, PR, further discussions, upstream release, etc and removed bug Something isn't working labels Sep 5, 2024
@fzyzcjy
Copy link
Owner

fzyzcjy commented Sep 5, 2024

I guess there are several separated issues here

  1. Add #[derive(Debug, PartialEq, Eq)] struct RustAutoOpaque should solve the first 3
  2. Using my suggestion in How to create a effective Shared State that retriggers Widget building? #2244 (reply in thread) should solve the Clone thing

Since this looks not hard (several lines of code), again, feel free to PR! Alternatively I may work on it later if you explicitly not do it :)

@fzyzcjy
Copy link
Owner

fzyzcjy commented Sep 11, 2024

Just a double check: Do you want to have fun PR for this? If so I will leave it to you, if not I will make a small PR ;)

@patmuk
Copy link
Contributor Author

patmuk commented Sep 11, 2024

Just a double check: Do you want to have fun PR for this? If so I will leave it to you, if not I will make a small TR ;)

Thanks for asking :) I was about to approach it - but then wanted to get the other PRs done first ...
So, please go ahead and make that PR - I will look into it and lean :)

I am taking care of the logging now (investigating env_logger), PR this, write up how to use nix and PR that.

I guess you are done by then with this PR, so please go ahead! Thanks!

@fzyzcjy
Copy link
Owner

fzyzcjy commented Sep 12, 2024

Btw, PartialEq, Eq may not be implementable, since RustAutoOpaque has a RwLock inside, and https://doc.rust-lang.org/stable/std/sync/struct.RwLock.html does not have it (since that would require a lock)

@patmuk
Copy link
Contributor Author

patmuk commented Sep 12, 2024

Btw, PartialEq, Eq may not be implementable, since RustAutoOpaque has a RwLock inside, and https://doc.rust-lang.org/stable/std/sync/struct.RwLock.html does not have it (since that would require a lock)

Great that you already implemented it! Thanks!
Yes, that sounds reasonable ... I only needed it in tests for the assert- but actually a better test design is using getters of the inner fields, so PartialEq, Eq isn't necessary on RustAutoOpaque.

Copy link
Contributor

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
awaiting Waiting for responses, PR, further discussions, upstream release, etc enhancement New feature or request
Projects
None yet
2 participants