Skip to content

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

Closed
@pkgw

Description

@pkgw

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions