From 48b917998ab538fbcf2b25dd5a0e169724c2a01b Mon Sep 17 00:00:00 2001 From: Marrub Date: Mon, 11 Feb 2019 06:28:53 -0500 Subject: [PATCH] PrimInt may fuck off into oblivion --- src/durandal/cenum.rs | 4 ++-- src/durandal/err.rs | 9 ++++---- src/durandal/image.rs | 52 ++++++++++++++++++++++-------------------- src/durandal/mod.rs | 1 - src/durandal/traits.rs | 20 ---------------- 5 files changed, 33 insertions(+), 53 deletions(-) delete mode 100644 src/durandal/traits.rs diff --git a/src/durandal/cenum.rs b/src/durandal/cenum.rs index 440961f..6d77614 100644 --- a/src/durandal/cenum.rs +++ b/src/durandal/cenum.rs @@ -20,11 +20,11 @@ macro_rules! c_enum impl $E { - pub fn from_repr(n: $T) -> Result<$E, ReprError<$T>> + pub fn from_repr(n: $T) -> Result<$E, ReprError> { match n { $($value => Ok($E::$Enum),)+ - n => Err(ReprError(n)) + n => Err(ReprError(n.into())) } } } diff --git a/src/durandal/err.rs b/src/durandal/err.rs index f8efb3e..b5cbe85 100644 --- a/src/durandal/err.rs +++ b/src/durandal/err.rs @@ -2,7 +2,6 @@ pub use failure::{Error, Fail}; -use crate::durandal::traits::PrimInt; use std::fmt; macro_rules! ok { @@ -25,10 +24,10 @@ pub fn err_msg(msg: &'static str) -> Error Error::from(ErrMsg(msg)) } -impl Fail for ReprError where T: PrimInt {} +impl Fail for ReprError {} impl Fail for ErrMsg {} -impl fmt::Display for ReprError where T: PrimInt +impl fmt::Display for ReprError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { @@ -36,7 +35,7 @@ impl fmt::Display for ReprError where T: PrimInt } } -impl fmt::Debug for ReprError where T: PrimInt +impl fmt::Debug for ReprError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { @@ -61,7 +60,7 @@ impl fmt::Debug for ErrMsg } #[derive(PartialEq)] -pub struct ReprError(pub T) where T: PrimInt; +pub struct ReprError(pub i64); struct ErrMsg(&'static str); diff --git a/src/durandal/image.rs b/src/durandal/image.rs index 050b80d..e43b1ec 100644 --- a/src/durandal/image.rs +++ b/src/durandal/image.rs @@ -1,6 +1,6 @@ //! Image and color representations. -use crate::durandal::{err::*, traits::*}; +use crate::durandal::err::*; use std::io; /// Creates a RGB8 color from a R5G5B5 format color. @@ -32,13 +32,15 @@ pub fn rgb8_to_rgb16(r: u8, g: u8, b: u8) -> Color16 /// Writes a PPM file from an image. pub fn write_ppm(out: &mut impl io::Write, im: &T) -> ResultS<()> { - write!(out, "P3\n{} {}\n{}\n", im.w(), im.h(), T::Output::MAX)?; + write!(out, "P3\n{} {}\n{}\n", im.w(), im.h(), u16::max_value())?; for y in 0..im.h() { for x in 0..im.w() { - let cr = ok!(im.cr_at(x, y), "not enough data in image")?; + let cr = im.cr_at(x, y); write!(out, "{} {} {} ", cr.r(), cr.g(), cr.b())?; } + + out.write(b"\n")?; } Ok(()) @@ -50,18 +52,24 @@ pub trait Image fn w(&self) -> usize; fn h(&self) -> usize; - fn cr_at(&self, x: usize, y: usize) -> Option<&Self::Output>; + fn cr_at(&self, x: usize, y: usize) -> &Self::Output; + + fn cr_get(&self, x: usize, y: usize) -> Option<&Self::Output> + { + if x < self.w() && y < self.h() { + Some(self.cr_at(x, y)) + } else { + None + } + } } pub trait Color { - type Comp: PrimInt; - const MAX: u32; - - fn r(&self) -> Self::Comp; - fn g(&self) -> Self::Comp; - fn b(&self) -> Self::Comp; - fn a(&self) -> Self::Comp; + fn r(&self) -> u16; + fn g(&self) -> u16; + fn b(&self) -> u16; + fn a(&self) -> u16; } impl Image16 @@ -80,9 +88,9 @@ impl Image for Image16 fn w(&self) -> usize {self.w} fn h(&self) -> usize {self.h} - fn cr_at(&self, x: usize, y: usize) -> Option<&Color16> + fn cr_at(&self, x: usize, y: usize) -> &Self::Output { - self.cr.get(x + y * self.w) + &self.cr[x + y * self.w] } } @@ -102,9 +110,9 @@ impl Image for Image8 fn w(&self) -> usize {self.w} fn h(&self) -> usize {self.h} - fn cr_at(&self, x: usize, y: usize) -> Option<&Color8> + fn cr_at(&self, x: usize, y: usize) -> &Self::Output { - self.cr.get(x + y * self.w) + &self.cr[x + y * self.w] } } @@ -118,9 +126,6 @@ impl Color16 impl Color for Color16 { - type Comp = u16; - const MAX: u32 = u16::max_value() as u32; - fn r(&self) -> u16 {self.0} fn g(&self) -> u16 {self.1} fn b(&self) -> u16 {self.2} @@ -137,13 +142,10 @@ impl Color8 impl Color for Color8 { - type Comp = u8; - const MAX: u32 = u8::max_value() as u32; - - fn r(&self) -> u8 {self.0} - fn g(&self) -> u8 {self.1} - fn b(&self) -> u8 {self.2} - fn a(&self) -> u8 {u8::max_value()} + fn r(&self) -> u16 {u16::from(self.0) << 8} + fn g(&self) -> u16 {u16::from(self.1) << 8} + fn b(&self) -> u16 {u16::from(self.2) << 8} + fn a(&self) -> u16 {u16::max_value()} } /// A RGB16 color. diff --git a/src/durandal/mod.rs b/src/durandal/mod.rs index 1a735e2..a7dc90d 100644 --- a/src/durandal/mod.rs +++ b/src/durandal/mod.rs @@ -11,6 +11,5 @@ pub mod crc; pub mod fx32; pub mod image; pub mod text; -pub mod traits; // EOF diff --git a/src/durandal/traits.rs b/src/durandal/traits.rs deleted file mode 100644 index a1041c7..0000000 --- a/src/durandal/traits.rs +++ /dev/null @@ -1,20 +0,0 @@ -//! Traits for basic types, because Rust doesn't provide them. - -/// Any primitive integer type. -pub trait PrimInt: - 'static + Send + Sync + std::fmt::Display + PartialEq -{ -} - -impl PrimInt for u8 {} -impl PrimInt for i8 {} -impl PrimInt for u16 {} -impl PrimInt for i16 {} -impl PrimInt for u32 {} -impl PrimInt for i32 {} -impl PrimInt for u64 {} -impl PrimInt for i64 {} -impl PrimInt for u128 {} -impl PrimInt for i128 {} - -// EOF