Implementing a trait for all Sync types

#1

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: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=ad4a2c34231660257c2e38419570df9f

Thanks!
Xavier

1 Like

#3

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

#4

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.

0 Likes

#5

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.

0 Likes

#6

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

#7

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

1 Like

#8

Right! sorry now I get your question.

1 Like