Skip to content

Instantly share code, notes, and snippets.

@bvssvni
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save bvssvni/7db83c8737d9996895c8 to your computer and use it in GitHub Desktop.

Select an option

Save bvssvni/7db83c8737d9996895c8 to your computer and use it in GitHub Desktop.
pub struct Per<A, B>(f64);
impl<T> Per<u8, T> {
pub fn bytes(num: f64) -> Per<u8, T> {
Per(num)
}
pub fn kibi_bytes(num: f64) -> Per<u8, T> {
Per(num * 1024.0)
}
}
impl<T, U> Per<T, U> {
pub fn new(num: f64) -> Per<T, U> {
Per(num)
}
}
impl<T, U, V> Div<Per<T, V>, Per<V, U>> for Per<T, U> {
fn div(&self, rhs: &Per<T, V>) -> Per<V, U> {
match (*self, *rhs) {
(Per(a), Per(b)) => Per(a / b),
}
}
}
impl<T, U, V> Mul<Per<U, V>, Per<T, V>> for Per<T, U> {
fn mul(&self, rhs: &Per<U, V>) -> Per<T, V> {
match (*self, *rhs) {
(Per(a), Per(b)) => Per(a * b),
}
}
}
impl<T, U> Add<Per<T, U>, Per<T, U>> for Per<T, U> {
fn add(&self, rhs: &Per<T, U>) -> Per<T, U> {
match (*self, *rhs) {
(Per(a), Per(b)) => Per(a + b),
}
}
}
impl<T, U> Sub<Per<T, U>, Per<T, U>> for Per<T, U> {
fn sub(&self, rhs: &Per<T, U>) -> Per<T, U> {
match (*self, *rhs) {
(Per(a), Per(b)) => Per(a - b),
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment