5

I want a min() method for f32, u32 and i32, so I created a trait Min:

trait Min {
    fn min(v1: Self, v2: Self) -> Self;
}

impl<T> Min for T where T: Ord {
    fn min(v1: Self, v2: Self) -> Self {
        ::std::cmp::min(v1, v2)
    }
}

impl Min for f32 {
    fn min(v1: Self, v2: Self) -> Self {
        v1.min(v2)
    }
}

I get an error:

error[E0119]: conflicting implementations of trait `Min` for type `f32`:
  --> src/main.rs:11:1
   |
5  | / impl<T> Min for T where T: Ord {
6  | |     fn min(v1: Self, v2: Self) -> Self {
7  | |         ::std::cmp::min(v1, v2)
8  | |     }
9  | | }
   | |_- first implementation here
10 | 
11 | / impl Min for f32 {
12 | |     fn min(v1: Self, v2: Self) -> Self {
13 | |         v1.min(v2)
14 | |     }
15 | | }
   | |_^ conflicting implementation for `f32`

According to the Rust standard library documentation, f32 does not implement Ord. Why there are conflicting implementations?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Laurence
  • 721
  • 7
  • 24
  • 2
    @erip It seems [`f32`](https://doc.rust-lang.org/stable/std/primitive.f32.html) only implements `PartialOrd` – Laurence Jul 11 '17 at 11:54
  • Wow, this seems like a bug. With only the generic implementation, the compiler clearly says that f32 does not implement Ord, but in the OP's example, it considers that f32 implements the generic one. – Boiethios Jul 11 '17 at 11:56
  • @Laurence Woops. :) Thanks for keeping me honest. – erip Jul 11 '17 at 11:57

1 Answers1

6

I believe this is because the compiler can't rule out the possibility that someday, someone will implement Ord for f32. To put it another way: if the compiler didn't act conservatively, it would be a breaking change to ever implement any new trait on existing types. That would severely limit every library's ability to grow without breaking all downstream users.

There is no direct way around this, as it is an intentional design choice for the language. The closest would be to implement a wrapper type around f32 (i.e. struct OrdF32(f32);) and implement Ord or Min on that, or to use a crate that defines such a wrapper (such as ordered-float).

DK.
  • 55,277
  • 5
  • 189
  • 162
  • How to get around it? – Boiethios Jul 11 '17 at 12:05
  • 1
    @Boiethios: You don't; I added some more to my answer. – DK. Jul 11 '17 at 12:11
  • 1
    @Laurence: The closest I'm aware of is [RFC 1023](https://github.com/rust-lang/rfcs/blob/master/text/1023-rebalancing-coherence.md), but if that proves too inscrutable, then I'm not aware of anything that explains it more... *coherently*. – DK. Jul 11 '17 at 12:42
  • 1
    @Laurence these are called the *orphan rules*, IIRC. Refer to [RFC 1023](https://github.com/rust-lang/rfcs/blob/master/text/1023-rebalancing-coherence.md) and [error 0210](https://doc.rust-lang.org/error-index.html#E0210). – Shepmaster Jul 11 '17 at 12:43
  • Can this be side-stepped by implementing `Ord` for `f32`? – 9000 Sep 11 '18 at 17:47
  • 1
    @9000: You *can't* implement `Ord` for `f32`, because it doesn't satisfy the requirements: it's not totally ordered. Even if it did, you can't, because you can't implement a trait you didn't write on a type you didn't define. – DK. Sep 12 '18 at 02:41
  • With [specialization](https://github.com/rust-lang/rfcs/blob/master/text/1210-impl-specialization.md) in mind, do you think this can be worked around somehow? – seamlik Sep 28 '19 at 19:58