Skip to content

Commit 77c854c

Browse files
committed
Disambiguate associated items in trait impls
The addition of TryFrom<&str> for EnumString breaks for enums that have a variant called "Error". Rewrite as <Self as Trait> where associated types are used as the return type for trait functions. Example: ```rust #[derive(EnumString)] pub enum Test { Error, } ``` error: ambiguous associated item --> src/lib.rs:3:10 | 3 | #[derive(EnumString)] | ^^^^^^^^^^ | = note: `#[deny(ambiguous_associated_items)]` on by default = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #57644 <rust-lang/rust#57644>
1 parent 5272fce commit 77c854c

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

strum_macros/src/macros/enum_iter.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ pub fn enum_iter_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
9696
impl #impl_generics Iterator for #iter_name #ty_generics #where_clause {
9797
type Item = #name #ty_generics;
9898

99-
fn next(&mut self) -> Option<Self::Item> {
99+
fn next(&mut self) -> Option<<Self as Iterator>::Item> {
100100
self.nth(0)
101101
}
102102

@@ -105,7 +105,7 @@ pub fn enum_iter_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
105105
(t, Some(t))
106106
}
107107

108-
fn nth(&mut self, n: usize) -> Option<Self::Item> {
108+
fn nth(&mut self, n: usize) -> Option<<Self as Iterator>::Item> {
109109
let idx = self.idx + n + 1;
110110
if idx + self.back_idx > #variant_count {
111111
// We went past the end of the iterator. Freeze idx at #variant_count
@@ -127,7 +127,7 @@ pub fn enum_iter_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
127127
}
128128

129129
impl #impl_generics DoubleEndedIterator for #iter_name #ty_generics #where_clause {
130-
fn next_back(&mut self) -> Option<Self::Item> {
130+
fn next_back(&mut self) -> Option<<Self as Iterator>::Item> {
131131
let back_idx = self.back_idx + 1;
132132

133133
if self.idx + back_idx > #variant_count {

strum_macros/src/macros/strings/from_string.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ pub fn from_string_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
9191
#[allow(clippy::use_self)]
9292
impl #impl_generics ::core::str::FromStr for #name #ty_generics #where_clause {
9393
type Err = #strum_module_path::ParseError;
94-
fn from_str(s: &str) -> ::core::result::Result< #name #ty_generics , Self::Err> {
94+
fn from_str(s: &str) -> ::core::result::Result< #name #ty_generics , <Self as ::core::str::FromStr>::Err> {
9595
match s {
9696
#(#arms),*
9797
}
@@ -136,7 +136,7 @@ fn try_from_str(
136136
#[allow(clippy::use_self)]
137137
impl #impl_generics ::std::convert::TryFrom<&str> for #name #ty_generics #where_clause {
138138
type Error = #strum_module_path::ParseError;
139-
fn try_from(s: &str) -> ::std::result::Result< #name #ty_generics , Self::Error> {
139+
fn try_from(s: &str) -> ::std::result::Result< #name #ty_generics , <Self as ::std::convert::TryFrom<&str>>::Error> {
140140
::std::str::FromStr::from_str(s)
141141
}
142142
}

0 commit comments

Comments
 (0)