Skip to content

Instantly share code, notes, and snippets.

@mbrubeck
Created May 9, 2014 23:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbrubeck/74dd82ddb093dcda7f2f to your computer and use it in GitHub Desktop.
Save mbrubeck/74dd82ddb093dcda7f2f to your computer and use it in GitHub Desktop.
/// A scaling factor between two different length units.
///
/// This is effectively a type-safe float, intended to be used in combination with the known-type
/// instances of Point2D, Rect, etc.
#[deriving(Clone, Decodable, Encodable, Eq)]
pub struct ScaleFactor<Src, Dst>(pub f32);
impl<Src, Dst> Deref<f32> for ScaleFactor<Src, Dst> {
fn deref<'a>(&'a self) -> &'a f32 {
let ScaleFactor(ref x) = *self;
x
}
}
impl<A,B,C> Mul<ScaleFactor<B, C>, ScaleFactor<A, C>> for ScaleFactor<A, B> {
#[inline]
fn mul(&self, other: &ScaleFactor<B, C>) -> ScaleFactor<A, C> {
ScaleFactor(**self * **other)
}
}
impl<Src, Dst> Mul<Src, Dst> for ScaleFactor<Src, Dst> {
#[inline]
fn mul(&self, other: &Src) -> Dst {
**self * *other
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment