Skip to content

Commit

Permalink
Restore Pixel::COLOR_TYPE associated type
Browse files Browse the repository at this point in the history
  • Loading branch information
aschampion committed Dec 10, 2019
1 parent 5f33a49 commit b30f4af
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
15 changes: 10 additions & 5 deletions src/buffer.rs
Expand Up @@ -42,8 +42,13 @@ pub trait Pixel: Copy + Clone {
Self::COLOR_MODEL
}

/// ColorType for this pixel format
const COLOR_TYPE: ColorType;
/// Returns the ColorType for this pixel format
fn color_type() -> ColorType;
#[deprecated(note="please use COLOR_TYPE associated constant")]
fn color_type() -> ColorType {
Self::COLOR_TYPE
}

/// Returns the channels of this pixel as a 4 tuple. If the pixel
/// has less than 4 channels the remainder is filled with the maximum value
Expand Down Expand Up @@ -650,7 +655,7 @@ where
FlatSamples {
samples: self.data,
layout,
color_hint: Some(P::color_type()),
color_hint: Some(P::COLOR_TYPE),
}
}

Expand All @@ -664,7 +669,7 @@ where
FlatSamples {
samples: self.data.as_ref(),
layout,
color_hint: Some(P::color_type()),
color_hint: Some(P::COLOR_TYPE),
}
}
}
Expand Down Expand Up @@ -758,7 +763,7 @@ where
self.as_bytes(),
self.width(),
self.height(),
<P as Pixel>::color_type(),
<P as Pixel>::COLOR_TYPE,
)
}
}
Expand All @@ -784,7 +789,7 @@ where
self.as_bytes(),
self.width(),
self.height(),
<P as Pixel>::color_type(),
<P as Pixel>::COLOR_TYPE,
format,
)
}
Expand Down
8 changes: 2 additions & 6 deletions src/color.rs
Expand Up @@ -181,12 +181,8 @@ impl<T: Primitive + 'static> Pixel for $ident<T> {

const COLOR_MODEL: &'static str = $interpretation;

fn color_type() -> ColorType {
match std::mem::size_of::<T>() {
1 => $color_type_u8,
_ => $color_type_u16,
}
}
const COLOR_TYPE: ColorType =
[$color_type_u8, $color_type_u16][(std::mem::size_of::<T>() > 1) as usize];

#[inline(always)]
fn channels(&self) -> &[T] {
Expand Down
8 changes: 4 additions & 4 deletions src/flat.rs
Expand Up @@ -560,7 +560,7 @@ impl<Buffer> FlatSamples<Buffer> {
where P: Pixel, Buffer: AsRef<[P::Subpixel]>,
{
if self.layout.channels != P::CHANNEL_COUNT {
return Err(Error::WrongColor(P::color_type()))
return Err(Error::WrongColor(P::COLOR_TYPE))
}

let as_ref = self.samples.as_ref();
Expand Down Expand Up @@ -597,7 +597,7 @@ impl<Buffer> FlatSamples<Buffer> {
where P: Pixel, Buffer: AsMut<[P::Subpixel]>,
{
if self.layout.channels != P::CHANNEL_COUNT {
return Err(Error::WrongColor(P::color_type()))
return Err(Error::WrongColor(P::COLOR_TYPE))
}

let as_mut = self.samples.as_mut();
Expand Down Expand Up @@ -634,7 +634,7 @@ impl<Buffer> FlatSamples<Buffer> {
}

if self.layout.channels != P::CHANNEL_COUNT {
return Err(Error::WrongColor(P::color_type()))
return Err(Error::WrongColor(P::COLOR_TYPE))
}

let as_mut = self.samples.as_mut();
Expand Down Expand Up @@ -718,7 +718,7 @@ impl<Buffer> FlatSamples<Buffer> {
}

if self.layout.channels != P::CHANNEL_COUNT {
return Err((Error::WrongColor(P::color_type()), self))
return Err((Error::WrongColor(P::COLOR_TYPE), self))
}

if !self.fits(self.samples.deref().len()) {
Expand Down

0 comments on commit b30f4af

Please sign in to comment.