Implementing a trait for all Sync types

I'm trying to implement a trait for all types that are Sync in a generic manner. The same trait would also be implemented for some specific non-Sync types.

The compiler is however fighting me on this, claiming that the non-Sync implementation is conflicting with the first one. See for instance: Rust Playground

Thanks!
Xavier

1 Like

There is nothing preventing the standard library from adding unsafe impl<T> Sync for Rc<T> in the future (other than that being unsound, but the compiler doesn’t know this), if it did then there would be multiple applicable implementations of A for B.

3 Likes

Sure, but that isn't the case today, so I'm not sure why the compiler effectively pretend that B is Sync. My reasoning on this is that B is not Sync, and therefore the trait A shouldn't be implemented for it, due to the generic implementation requiring a Sync type. Thus, the specialized implementation of A for B shouldn't be conflicting with anything.

Yes, I could do that, but the point here is to have a "default" implementation of a trait for all Sync types, and I would then have a specific implementation of that trait for types that aren't Sync.

The compiler has to treat these potential future conflicts as errors. Otherwise, the standard library could never add a new impl to an existing type, because any new impl could break existing crates.

6 Likes

When specialization is stabilized, you'll be able to write both of these impls without an error.

1 Like

Right! sorry now I get your question.

1 Like