News for hackers. Stuff that matters.
Re:Rewriting Libimagequant in Rust for Portability by pornel on Tuesday January 04, 2022 @01:07PM (#29797845)
I wasn't thinking about traits when I wrote that, but you make a great point — that's a whole another way to design things. Maybe it could have been something fancy like:

    impl<T> Sound for T where T: Percussion<Accessory=Stick>
Re:Rewriting Libimagequant in Rust for Portability by hedgehog on Tuesday January 04, 2022 @01:58PM (#29798550)
There's also the pattern used in from+into where one implementation is wrapped to provide its counterpart on the other type. Nice to read but feels a little extra magical for someone without a lot of Rust background.
Re:Rewriting Libimagequant in Rust for Portability by tialaramex on Tuesday January 04, 2022 @03:16PM (#29799752)
Blanket implementation is a very common idea in Rust. The standard library "owns" its own Traits and so it gets to provide blanket implementations for those, but you can do the same for your own.

Here's (really, this is literally the code) how std provides a blanket implementation of Into given From:

  impl<T, U> Into<U> for T
  where
      U: From<T>,
  {
      fn into(self) -> U {
          U::from(self)
      }
  }
There's actually a chain, From implies Into, Into implies TryFrom, then TryFrom implies TryInto.