Improve trait bounds

This commit is contained in:
Brendan Zabarauskas 2013-07-09 23:10:59 +10:00
parent d65aa944b9
commit 23e21ac1b3
2 changed files with 20 additions and 20 deletions

View file

@ -25,27 +25,27 @@ mod num_macros;
#[deriving(Clone, Eq)] #[deriving(Clone, Eq)]
pub struct HSV<T> { h: T, s: T, v: T } pub struct HSV<T> { h: T, s: T, v: T }
impl<T> HSV<T> { impl<T:Channel + Float> HSV<T> {
pub fn new(h: T, s: T, v: T) -> HSV<T> { pub fn new(h: T, s: T, v: T) -> HSV<T> {
HSV { h: h, s: s, v: v } HSV { h: h, s: s, v: v }
} }
} }
pub trait ToHSV { pub trait ToHSV {
pub fn to_hsv<U:Clone + Float + Channel>(&self) -> HSV<U>; pub fn to_hsv<U:Channel + Float>(&self) -> HSV<U>;
} }
impl<T:Clone + Float + Channel> ToHSV for HSV<T> { impl<T:Clone + Channel + Float> ToHSV for HSV<T> {
#[inline] #[inline]
pub fn to_hsv<U:Clone + Float + Channel>(&self) -> HSV<U> { pub fn to_hsv<U:Channel + Float>(&self) -> HSV<U> {
HSV::new(Channel::from((*self).h.clone()), HSV::new(Channel::from((*self).h.clone()),
Channel::from((*self).s.clone()), Channel::from((*self).s.clone()),
Channel::from((*self).v.clone())) Channel::from((*self).v.clone()))
} }
} }
impl<T:Clone + Float + Channel> ToRGB for HSV<T> { impl<T:Clone + Channel + Float> ToRGB for HSV<T> {
pub fn to_rgb<U:Clone + Channel>(&self) -> RGB<U> { pub fn to_rgb<U:Channel>(&self) -> RGB<U> {
// Algorithm taken from the Wikipedia article on HSL and HSV: // Algorithm taken from the Wikipedia article on HSL and HSV:
// http://en.wikipedia.org/wiki/HSL_and_HSV#From_HSV // http://en.wikipedia.org/wiki/HSL_and_HSV#From_HSV
@ -79,7 +79,7 @@ impl<T:Clone + Float + Channel> ToRGB for HSV<T> {
#[deriving(Clone, Eq)] #[deriving(Clone, Eq)]
pub struct HSVA<T> { h: T, s: T, v: T, a: T } pub struct HSVA<T> { h: T, s: T, v: T, a: T }
impl<T> HSVA<T> { impl<T:Channel + Float> HSVA<T> {
#[inline] #[inline]
pub fn new(h: T, s: T, v: T, a: T) -> HSVA<T> { pub fn new(h: T, s: T, v: T, a: T) -> HSVA<T> {
HSVA { h: h, s: s, v: v, a: a } HSVA { h: h, s: s, v: v, a: a }
@ -102,12 +102,12 @@ impl<T> HSVA<T> {
} }
pub trait ToHSVA { pub trait ToHSVA {
pub fn to_hsva<U:Clone + Float + Channel>(&self) -> HSVA<U>; pub fn to_hsva<U:Channel + Float>(&self) -> HSVA<U>;
} }
impl<C: ToHSV, T:Clone + Float + Channel> ToHSVA for (C, T) { impl<C: ToHSV, T:Clone + Channel + Float> ToHSVA for (C, T) {
#[inline] #[inline]
pub fn to_hsva<U:Clone + Float + Channel>(&self) -> HSVA<U> { pub fn to_hsva<U:Channel + Float>(&self) -> HSVA<U> {
match *self { match *self {
(ref hsv, ref a) => { (ref hsv, ref a) => {
HSVA::from_hsv_a( HSVA::from_hsv_a(
@ -119,9 +119,9 @@ impl<C: ToHSV, T:Clone + Float + Channel> ToHSVA for (C, T) {
} }
} }
impl<T:Clone + Float + Channel> ToRGBA for HSVA<T> { impl<T:Clone + Channel + Float> ToRGBA for HSVA<T> {
#[inline] #[inline]
pub fn to_rgba<U:Clone + Channel>(&self) -> RGBA<U> { pub fn to_rgba<U:Channel>(&self) -> RGBA<U> {
RGBA::from_rgb_a( RGBA::from_rgb_a(
self.hsv().to_rgb(), self.hsv().to_rgb(),
Channel::from((*self).a.clone()) Channel::from((*self).a.clone())

View file

@ -25,7 +25,7 @@ mod num_macros;
#[deriving(Clone, Eq)] #[deriving(Clone, Eq)]
pub struct RGB<T> { r: T, g: T, b: T } pub struct RGB<T> { r: T, g: T, b: T }
impl<T> RGB<T> { impl<T:Channel> RGB<T> {
#[inline] #[inline]
pub fn new(r: T, g: T, b: T) -> RGB<T> { pub fn new(r: T, g: T, b: T) -> RGB<T> {
RGB { r: r, g: g, b: b } RGB { r: r, g: g, b: b }
@ -33,12 +33,12 @@ impl<T> RGB<T> {
} }
pub trait ToRGB { pub trait ToRGB {
pub fn to_rgb<U:Clone + Channel>(&self) -> RGB<U>; pub fn to_rgb<U:Channel>(&self) -> RGB<U>;
} }
impl<T:Clone + Channel> ToRGB for RGB<T> { impl<T:Clone + Channel> ToRGB for RGB<T> {
#[inline] #[inline]
pub fn to_rgb<U:Clone + Channel>(&self) -> RGB<U> { pub fn to_rgb<U:Channel>(&self) -> RGB<U> {
RGB::new(Channel::from((*self).r.clone()), RGB::new(Channel::from((*self).r.clone()),
Channel::from((*self).g.clone()), Channel::from((*self).g.clone()),
Channel::from((*self).b.clone())) Channel::from((*self).b.clone()))
@ -47,7 +47,7 @@ impl<T:Clone + Channel> ToRGB for RGB<T> {
impl<T:Clone + Channel> ToHSV for RGB<T> { impl<T:Clone + Channel> ToHSV for RGB<T> {
#[inline] #[inline]
pub fn to_hsv<U:Clone + Float + Channel>(&self) -> HSV<U> { pub fn to_hsv<U:Channel + Float>(&self) -> HSV<U> {
// Algorithm taken from the Wikipedia article on HSL and HSV: // Algorithm taken from the Wikipedia article on HSL and HSV:
// http://en.wikipedia.org/wiki/HSL_and_HSV#From_HSV // http://en.wikipedia.org/wiki/HSL_and_HSV#From_HSV
@ -77,7 +77,7 @@ impl<T:Clone + Channel> ToHSV for RGB<T> {
#[deriving(Clone, Eq)] #[deriving(Clone, Eq)]
pub struct RGBA<T> { r: T, g: T, b: T, a: T } pub struct RGBA<T> { r: T, g: T, b: T, a: T }
impl<T> RGBA<T> { impl<T:Channel> RGBA<T> {
#[inline] #[inline]
pub fn new(r: T, g: T, b: T, a: T) -> RGBA<T> { pub fn new(r: T, g: T, b: T, a: T) -> RGBA<T> {
RGBA { r: r, g: g, b: b, a: a } RGBA { r: r, g: g, b: b, a: a }
@ -100,12 +100,12 @@ impl<T> RGBA<T> {
} }
pub trait ToRGBA { pub trait ToRGBA {
pub fn to_rgba<U:Clone + Channel>(&self) -> RGBA<U>; pub fn to_rgba<U:Channel>(&self) -> RGBA<U>;
} }
impl<C: ToRGB, T:Clone + Channel> ToRGBA for (C, T) { impl<C: ToRGB, T:Clone + Channel> ToRGBA for (C, T) {
#[inline] #[inline]
pub fn to_rgba<U:Clone + Channel>(&self) -> RGBA<U> { pub fn to_rgba<U:Channel>(&self) -> RGBA<U> {
match *self { match *self {
(ref rgb, ref a) => { (ref rgb, ref a) => {
RGBA::from_rgb_a(rgb.to_rgb(), Channel::from(a.clone())) RGBA::from_rgb_a(rgb.to_rgb(), Channel::from(a.clone()))
@ -116,7 +116,7 @@ impl<C: ToRGB, T:Clone + Channel> ToRGBA for (C, T) {
impl<T:Clone + Channel> ToHSVA for RGBA<T> { impl<T:Clone + Channel> ToHSVA for RGBA<T> {
#[inline] #[inline]
pub fn to_hsva<U:Clone + Float + Channel>(&self) -> HSVA<U> { pub fn to_hsva<U:Channel + Float>(&self) -> HSVA<U> {
HSVA::from_hsv_a( HSVA::from_hsv_a(
self.rgb().to_hsv(), self.rgb().to_hsv(),
Channel::from((*self).a.clone()) Channel::from((*self).a.clone())