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

Make AsMut unambiguous #52

Merged
merged 1 commit into from
Oct 3, 2023
Merged

Make AsMut unambiguous #52

merged 1 commit into from
Oct 3, 2023

Conversation

l0kod
Copy link
Member

@l0kod l0kod commented Sep 14, 2023

Remove AsMut<Option> from the public API to avoid ambiguous types that can lead to:

  error[E0283]: type annotations needed for `&mut T`
     --> src/ruleset.rs:306:9
      |
  306 |     let _ = ruleset.as_mut();
      |         ^           ------ type must be known at this point
      |
  note: multiple `impl`s satisfying `ruleset::Ruleset: AsMut<_>` found

Add tests to detect potential future change.

This is less elegant that the AsMut autoderivated implementations, but it is safer.

Fixes #48

Cc @cd-work

Remove AsMut<Option<CompatLevel>> from the public API to avoid ambiguous types
that can lead to:
  error[E0283]: type annotations needed for `&mut T`
     --> src/ruleset.rs:306:9
      |
  306 |     let _ = ruleset.as_mut();
      |         ^           ------ type must be known at this point
      |
  note: multiple `impl`s satisfying `ruleset::Ruleset: AsMut<_>` found

Add tests to detect potential future change.

This is less elegant that the AsMut autoderivated implementations, but
it is safer.

Fixes landlock-lsm#48

Signed-off-by: Mickaël Salaün <mic@digikod.net>
@l0kod l0kod changed the title src: Make AsMut<Ruleset> and AsMut<RulesetCreated> unambiguous Make AsMut unambiguous Sep 14, 2023
Comment on lines +280 to +286
pub(crate) mod private {
use crate::CompatLevel;

pub trait OptionCompatLevelMut {
fn as_option_compat_level_mut(&mut self) -> &mut Option<CompatLevel>;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This feels like it will be a compiler error at some point in the future.

Copy link
Member Author

Choose a reason for hiding this comment

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

This feels like it will be a compiler error at some point in the future.

What exactly and why?

Copy link
Contributor

Choose a reason for hiding this comment

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

The OptionCompatLevelMut is part of the public API as a constraint on Compatible, right? But it's not actually made public.

There is a private type in public interface warning in rustc, but I suppose it doesn't apply to this (yet?).

It's possible this will never get changed since it might be considered a "feature" to prevent others from implementing a trait defined in your library, but it still seems not ideal to me.

Copy link
Member Author

Choose a reason for hiding this comment

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

That may be a hack but there is no other way to make this work without exposing internal APIs. I did use this trick some years ago and it's still working the same. I don't think Rust will change that without an alternative.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's up to you, but if I had to choose between the two solutions I'd tell downstream to just deal with it rather than introducing a hacky workaround like this. Especially since you'll also have to do this for every new AsRef thing you want to add in the future.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep, that's not optimal, but we'll see if we need to add back the AsMut API.

@l0kod l0kod merged commit a0da8c0 into landlock-lsm:main Oct 3, 2023
5 checks passed
@l0kod l0kod deleted the fix-as-mut branch October 3, 2023 15:49
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.

Multiple as_mut implementations prevent automatic type resolution
2 participants