From 11c7ee06fa3e3116a094a3418631a27bc5e55939 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Sun, 28 Oct 2012 16:57:41 +1000 Subject: [PATCH] Implement Abs for i8 to i64 --- src/math.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/math.rs b/src/math.rs index 902b272..9ace329 100644 --- a/src/math.rs +++ b/src/math.rs @@ -7,6 +7,43 @@ trait Abs { pure fn abs() -> self; } +#[inline] +pure fn abs(x: T) -> T { + x.abs() +} + +impl i8: Abs { + #[inline] + pure fn abs() -> i8 { + if self >= 0 { self } + else {-self } + } +} + +impl i16: Abs { + #[inline] + pure fn abs() -> i16 { + if self >= 0 { self } + else {-self } + } +} + +impl i32: Abs { + #[inline] + pure fn abs() -> i32 { + if self >= 0 { self } + else {-self } + } +} + +impl i64: Abs { + #[inline] + pure fn abs() -> i64 { + if self >= 0 { self } + else {-self } + } +} + impl int: Abs { #[inline] pure fn abs() -> int {