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 FromValueDelegate #987

Closed
wants to merge 1 commit into from
Closed

Add FromValueDelegate #987

wants to merge 1 commit into from

Conversation

ranfdev
Copy link
Member

@ranfdev ranfdev commented Feb 11, 2023

Makes implementing FromValue safe and trivial

glib/src/value.rs Outdated Show resolved Hide resolved
@@ -369,20 +369,6 @@ pub trait ToValue {
fn value_type(&self) -> Type;
}

// rustdoc-stripper-ignore-next
/// Blanket implementation for all references.
impl<T: ToValue + StaticType> ToValue for &T {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I needed to remove this to auto-implement ToValue for T: ValueDelegate. Else they were in conflict.

The tests don't complain so... 🤷🏻

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this makes it impossible to pass &Object to e.g. Object::set_property() and only Object would work

@ranfdev
Copy link
Member Author

ranfdev commented Feb 12, 2023

Uh oh, we don't have GATs in rust 1.64, only from 1.65. 😢

That ValueDelegate trait implementation will require a T: Clone bound then

@sdroege
Copy link
Member

sdroege commented Feb 12, 2023

Uh oh, we don't have GATs in rust 1.64, only from 1.65. cry

For the next release we can update to 1.65 but that won't do for backporting :)

@RealKC
Copy link
Contributor

RealKC commented Feb 12, 2023

I don't think this can be backported anyway? It removes a trait impl and I'm not sure the new one covers the cases the old trait impl did

@ranfdev
Copy link
Member Author

ranfdev commented Feb 12, 2023

So, in brief, implementing this with traits is impossible. To keep the old ToValue implementation for &T, Rust would need support for specialization.

Fortunately, we have macros!
Instead of implementing ToValue/FromValue with a blanket implementation, we could have a #[derive(ValueDelegate)]
to achieve the same thing.

And that would work for every rust version.
The result is equivalent. The macro would just generate a ToValue/FromValue implementation calling From<DelegatedType> and Into<DelegatedType>

@sdroege
Copy link
Member

sdroege commented Feb 13, 2023

I think that makes sense. It's even less user code than implementing a trait manually ;)

@pbor
Copy link
Contributor

pbor commented Feb 18, 2023

#1000 has the macro approach

@ranfdev ranfdev closed this Mar 1, 2023
@ranfdev ranfdev deleted the from_val_del branch March 1, 2023 07:44
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

Successfully merging this pull request may close these issues.

4 participants