-
Notifications
You must be signed in to change notification settings - Fork 212
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
Option<T> does not implement OwnedToVariant when T implements only OwnedToVariant. #869
Comments
I'm not sure how to address this now, due to the conflicts you mentioned. Rust's lack of specialization is quite limiting sometimes. Any ideas? |
I'm not familiar with Rust either😅 In conclusion, I do not have an easy idea. So what I write below is for reference only. First, since there is no specialization in Rust, I would remove Implement separatelyRequire users to implement ToVariant and OwnedToVariant separately. #[derive(ToVariant, OwnedToVariant)]
struct Foo {} Pros
Cons
Standard library approachThe same approach is used to implement the standard library in From (as Into). The define of ToVariant is changed as follows, and OwnedToVariant is removed. trait ToVariant {
// Note that there is no & in self.
fn to_variant(self) -> Variant;
} In the implementation to the trait, separate implementations are made for T corresponding to OwnedToVariant and &T corresponding to the old ToVariant. (String in the standard library is helpful) impl ToVariant for Foo {
fn to_variant(self) -> Variant {
/* Implementation in move (or copy) semantics. */
}
}
impl ToVariant for &'_ Foo {
fn to_variant(self) -> Variant {
/* Implementation in clone semantics. */
}
} Pros
Cons
What I feelI think it is silly to make breaking changes to solve small issues that have workarounds. So better to resolve this issue in conjunction with #808 and related issues. |
I'm not even sure how many users manually implement I agree that we shouldn't break the code for 0.10, so I'll leave this open for later. One option might also be that we allow #[derive(ToVariant)]`
struct MyStruct { ... } which would use |
Assigned to 0.11, but honestly not sure if this can be solvable at all. Maybe there can be some takeaways for GDExtension w.r.t. how to design those traits. |
I think #869 (comment) is the approach one would have taken if the traits were to be designed from scratch. This is the same way serde and co does it. The I don't think there are a lot of people who write their own |
Made an attempt at this, but unfortunately ran into a rustc bug with impls on references: rust-lang/rust#39959 Seems like this will have to be delayed until the upstream bug is fixed, as there isn't an obvious workaround that can be reliably generated. |
For example, the following code will not work.
This issue can be avoided by using into_shared() to perform type conversion.
I tried to solve this issue, but implementing
impl<T: OwnedToVariant > OwnedToVariant for Option<T>
causes a conflict withimpl<T: ToVariant> OwnedToVariant for T
.The text was updated successfully, but these errors were encountered: