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

EnumAccessor alias should also prevent duplicate accessor errors #7

Closed
tlaferriere opened this issue Jun 17, 2023 · 3 comments · May be fixed by #8
Closed

EnumAccessor alias should also prevent duplicate accessor errors #7

tlaferriere opened this issue Jun 17, 2023 · 3 comments · May be fixed by #8

Comments

@tlaferriere
Copy link
Contributor

When reworking the examples so they can be run as part of the doc tests, I came across the EnumAccessor alias feature, which was advertised to prevent name clashes.
Here's how I envisioned it:

use sort_by_derive::EnumAccessor;

#[derive(EnumAccessor)]
#[accessor(a: u16, except(Variant3))]
#[accessor(a as a_big: u32, except(Variant1,Variant2))]
enum E {
  Variant1 { a: u16 },
  Variant2 { a: u16, c: u32 },
  Variant3 { a: u32 },
}

assert_eq!(E::Variant1 { a: 1 }.a(), Some(&1));
assert_eq!(E::Variant2 { a: 1, c: 0 }.a(), Some(&1));
assert_eq!(E::Variant3 { a: 0 }.a(), None);
assert_eq!(E::Variant3 { a: 2 }.a_big(), Some(&2));
assert_eq!(E::Variant1 { a: 0 }.a_big(), None);

But it errors:

error: Duplicate accessor a
 --> src/lib.rs:192:12
  |
9 | #[accessor(a as a_big: u32, except(Variant1,Variant2))]
  |            ^^^^^^^^^^

I realise this feature is meant to prevent name clashes with existing methods rather than other enum variant members, but I think this usecase is intuitive and can be surprising when it breaks this way.

I want to get feedback on whether this a feature we want to support or not.

@valsteen
Copy link
Owner

Yes that's right there is a bug - I probably implemented the collision check before I introduced aliases with as.

I guess it's around if accessors.iter().any(|a| a.ident == accessor.ident) { that should be if accessors.iter().any(|a| a.alias == accessor.alias)

@valsteen
Copy link
Owner

.alias did the trick #11

@valsteen
Copy link
Owner

valsteen commented Aug 7, 2023

Hi, I think this issue is solved, but please re-open or create a follow-up issue if you think there is some remaining problem

@valsteen valsteen closed this as completed Aug 7, 2023
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 a pull request may close this issue.

2 participants