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

Unable to implement a trait in terms of more than one other trait #4326

Closed
pkgw opened this issue Jan 2, 2013 · 1 comment
Closed

Unable to implement a trait in terms of more than one other trait #4326

pkgw opened this issue Jan 2, 2013 · 1 comment

Comments

@pkgw
Copy link
Contributor

pkgw commented Jan 2, 2013

I'd like to implement a trait in terms of a few other ones, but the compiler tells me that I have conflicting trait implementations. The most succinct example is

trait Generic { }
trait SpecificOne { }
trait SpecificTwo { }
impl<T: SpecificOne> T: Generic { }
impl<T: SpecificTwo> T: Generic { }

Now, if there existed a type implementing both SpecificOne and SpecificTwo, that would be a problem, but there isn't.

I'm pretty sure the compiler should be able to determine whether the implementation is legal or not. (It's not clear to me whether you have to worry about something introducing ambiguities by linking to your crate later.) And unless I'm overlooking something (not to imply that this is unlikely), it seems to me that this is an important thing to be able to do.

My specific use case is for SHA1 hashing of things. I want to do something along the lines of:

trait Hashable { fn hash (&self, Sha1 sha); }

impl<T: to_bytes::IterBytes> T: Hashable { /* send the bytes to the sha1 */ }

impl<T: io::Reader> T: Hashable { /* send the stream to the sha1 */ }

Readers can't be IterBytes because the bytes iterator function is defined as being pure.

@pkgw
Copy link
Contributor Author

pkgw commented Jan 2, 2013

Never mind, I'm an idiot.

impl SpecificOne: Generic { }
impl SpecificTwo: Generic { }

I had been reading the tutorial and didn't catch anything saying that you can provide an impl for a bare trait in this way, and the people that I briefly consulted on IRC didn't immediately come up with this, so maybe that's something that should be added to, or made more prominent in, the tutorial.

@pkgw pkgw closed this as completed Jan 2, 2013
flip1995 pushed a commit to flip1995/rust that referenced this issue Jul 14, 2020
…1995

Improvements for `type_repetition_in_bounds` lint

Some improvements for `type_repetition_in_bounds`:
- add a configurable threshold to trigger the lint (rust-lang#4380). The lint won't trigger anymore if there are more bounds (strictly) than `conf.max_trait_bounds` on this type.
- take generic args into account over bounded type (rust-lang#4323)
- don't lint for predicates generated in macros (rust-lang#4326)

Fixes rust-lang#4380,
Fixes rust-lang#4323,
Fixes rust-lang#4326,
Closes rust-lang#3764

changelog: Fix multiple FPs in `type_repetition_in_bounds` and add a configuration option

Note: the rust-lang#3764 has already been fixed but not closed
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

No branches or pull requests

1 participant