Reduce transmute ugliness

This commit is contained in:
Brendan Zabarauskas 2013-07-09 21:17:04 +10:00
parent 452decd3e2
commit 29a99cea51
2 changed files with 46 additions and 27 deletions

View file

@ -17,7 +17,7 @@ use std::cast;
use color::{Channel, ToChannel}; use color::{Channel, ToChannel};
use color::{FloatChannel, ToFloatChannel}; use color::{FloatChannel, ToFloatChannel};
use color::{HSV, ToHSV, RGB, ToRGB, RGBA, ToRGBA}; use color::{HSV, ToHSV, ToRGB, RGBA, ToRGBA};
#[path = "../num_macros.rs"] #[path = "../num_macros.rs"]
mod num_macros; mod num_macros;
@ -30,6 +30,21 @@ impl<T> HSVA<T> {
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 }
} }
#[inline]
pub fn from_hsv_a(hsv: HSV<T>, a: T) -> HSVA<T> {
unsafe { cast::transmute((hsv, a)) }
}
#[inline]
pub fn hsv<'a>(&'a self) -> &'a HSV<T> {
unsafe { cast::transmute(self) }
}
#[inline]
pub fn hsv_mut<'a>(&'a mut self) -> &'a mut HSV<T> {
unsafe { cast::transmute(self) }
}
} }
pub trait ToHSVA { pub trait ToHSVA {
@ -40,9 +55,10 @@ impl<C: ToHSV, T:Clone + ToFloatChannel> ToHSVA for (C, T) {
#[inline] #[inline]
pub fn to_hsva<U:Clone + FloatChannel>(&self) -> HSVA<U> { pub fn to_hsva<U:Clone + FloatChannel>(&self) -> HSVA<U> {
match *self { match *self {
(ref c, ref a) => unsafe { (ref hsv, ref a) => {
cast::transmute::<(HSV<U>, U), HSVA<U>>( HSVA::from_hsv_a(
(c.to_hsv::<U>(), FloatChannel::from(a.clone())) hsv.to_hsv(),
FloatChannel::from(a.clone())
) )
} }
} }
@ -52,14 +68,9 @@ impl<C: ToHSV, T:Clone + ToFloatChannel> ToHSVA for (C, T) {
impl<T:Clone + Float + ToChannel> ToRGBA for HSVA<T> { impl<T:Clone + Float + ToChannel> ToRGBA for HSVA<T> {
#[inline] #[inline]
pub fn to_rgba<U:Clone + Channel>(&self) -> RGBA<U> { pub fn to_rgba<U:Clone + Channel>(&self) -> RGBA<U> {
match unsafe { RGBA::from_rgb_a(
cast::transmute::<&HSVA<T>, &(HSV<T>, T)>(self) self.hsv().to_rgb(),
} { Channel::from((*self).a.clone())
&(ref c, ref a) => unsafe {
cast::transmute::<(RGB<U>, U), RGBA<U>>(
(c.to_rgb::<U>(), Channel::from(a.clone()))
) )
},
}
} }
} }

View file

@ -16,7 +16,7 @@
use std::cast; use std::cast;
use color::{Channel, ToChannel, FloatChannel}; use color::{Channel, ToChannel, FloatChannel};
use color::{RGB, ToRGB, HSV, ToHSV, HSVA, ToHSVA}; use color::{RGB, ToRGB, ToHSV, HSVA, ToHSVA};
#[path = "../num_macros.rs"] #[path = "../num_macros.rs"]
mod num_macros; mod num_macros;
@ -29,6 +29,21 @@ impl<T> RGBA<T> {
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 }
} }
#[inline]
pub fn from_rgb_a(rgb: RGB<T>, a: T) -> RGBA<T> {
unsafe { cast::transmute((rgb, a)) }
}
#[inline]
pub fn rgb<'a>(&'a self) -> &'a RGB<T> {
unsafe { cast::transmute(self) }
}
#[inline]
pub fn rgb_mut<'a>(&'a mut self) -> &'a mut RGB<T> {
unsafe { cast::transmute(self) }
}
} }
pub trait ToRGBA { pub trait ToRGBA {
@ -39,10 +54,8 @@ impl<C: ToRGB, T:Clone + ToChannel> ToRGBA for (C, T) {
#[inline] #[inline]
pub fn to_rgba<U:Clone + Channel>(&self) -> RGBA<U> { pub fn to_rgba<U:Clone + Channel>(&self) -> RGBA<U> {
match *self { match *self {
(ref c, ref a) => unsafe { (ref rgb, ref a) => {
cast::transmute::<(RGB<U>, U), RGBA<U>>( RGBA::from_rgb_a(rgb.to_rgb(), Channel::from(a.clone()))
(c.to_rgb::<U>(), Channel::from(a.clone()))
)
} }
} }
} }
@ -51,14 +64,9 @@ impl<C: ToRGB, T:Clone + ToChannel> ToRGBA for (C, T) {
impl<T:Clone + ToChannel> ToHSVA for RGBA<T> { impl<T:Clone + ToChannel> ToHSVA for RGBA<T> {
#[inline] #[inline]
pub fn to_hsva<U:Clone + FloatChannel>(&self) -> HSVA<U> { pub fn to_hsva<U:Clone + FloatChannel>(&self) -> HSVA<U> {
match unsafe { HSVA::from_hsv_a(
cast::transmute::<&RGBA<T>, &(RGB<T>, T)>(self) self.rgb().to_hsv(),
} { Channel::from((*self).a.clone())
&(ref c, ref a) => unsafe {
cast::transmute::<(HSV<U>, U), HSVA<U>>(
(c.to_hsv::<U>(), FloatChannel::from(a.clone()))
) )
} }
} }
}
}