Testcase: unit clarification

A useful method of unit conversions can be examined by implementing Add
with a phantom type parameter. The Add trait is examined below:

  1. // This construction would impose: `Self + RHS = Output`
  2. // where RHS defaults to Self if not specified in the implementation.
  3. pub trait Add<RHS = Self> {
  4. type Output;
  5. fn add(self, rhs: RHS) -> Self::Output;
  6. }
  7. // `Output` must be `T<U>` so that `T<U> + T<U> = T<U>`.
  8. impl<U> Add for T<U> {
  9. type Output = T<U>;
  10. ...
  11. }

The whole implementation:

  1. use std::ops::Add;
  2. use std::marker::PhantomData;
  3. /// Create void enumerations to define unit types.
  4. #[derive(Debug, Clone, Copy)]
  5. enum Inch {}
  6. #[derive(Debug, Clone, Copy)]
  7. enum Mm {}
  8. /// `Length` is a type with phantom type parameter `Unit`,
  9. /// and is not generic over the length type (that is `f64`).
  10. ///
  11. /// `f64` already implements the `Clone` and `Copy` traits.
  12. #[derive(Debug, Clone, Copy)]
  13. struct Length<Unit>(f64, PhantomData<Unit>);
  14. /// The `Add` trait defines the behavior of the `+` operator.
  15. impl<Unit> Add for Length<Unit> {
  16. type Output = Length<Unit>;
  17. // add() returns a new `Length` struct containing the sum.
  18. fn add(self, rhs: Length<Unit>) -> Length<Unit> {
  19. // `+` calls the `Add` implementation for `f64`.
  20. Length(self.0 + rhs.0, PhantomData)
  21. }
  22. }
  23. fn main() {
  24. // Specifies `one_foot` to have phantom type parameter `Inch`.
  25. let one_foot: Length<Inch> = Length(12.0, PhantomData);
  26. // `one_meter` has phantom type parameter `Mm`.
  27. let one_meter: Length<Mm> = Length(1000.0, PhantomData);
  28. // `+` calls the `add()` method we implemented for `Length<Unit>`.
  29. //
  30. // Since `Length` implements `Copy`, `add()` does not consume
  31. // `one_foot` and `one_meter` but copies them into `self` and `rhs`.
  32. let two_feet = one_foot + one_foot;
  33. let two_meters = one_meter + one_meter;
  34. // Addition works.
  35. println!("one foot + one_foot = {:?} in", two_feet.0);
  36. println!("one meter + one_meter = {:?} mm", two_meters.0);
  37. // Nonsensical operations fail as they should:
  38. // Compile-time Error: type mismatch.
  39. //let one_feter = one_foot + one_meter;
  40. }

See also:

Borrowing (&), Bounds (X: Y), enum, impl & self,
Overloading, ref, Traits (X for Y), and TupleStructs.