From a50e0ec5447effc9a28e3974f02dbcb06238b106 Mon Sep 17 00:00:00 2001 From: jaby Date: Mon, 1 May 2023 23:01:35 +0200 Subject: [PATCH] Replace unsafe code with raw trait --- .../src/images/reduced_tim/mod.rs | 10 +++++----- .../src/images/reduced_tim/types.rs | 18 +++++++++++++----- src/Tools/tool_helper/Cargo.toml | 2 +- src/Tools/tool_helper/src/raw.rs | 8 +++++--- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/Tools/jaby_engine_fconv/src/images/reduced_tim/mod.rs b/src/Tools/jaby_engine_fconv/src/images/reduced_tim/mod.rs index 415105c6..6deb8357 100644 --- a/src/Tools/jaby_engine_fconv/src/images/reduced_tim/mod.rs +++ b/src/Tools/jaby_engine_fconv/src/images/reduced_tim/mod.rs @@ -59,23 +59,23 @@ fn encode(image: T, color_depth: ColorType, clut_align: Cl }; let header = Header::encode(width, height, pal_width, pal_height).ok_or(Error::from_callback(|| {format!("Image size (width: {}, height: {}) needs to be even", width, height)}))?; - - tool_helper::raw::write_generic(output, header)?; + + tool_helper::raw::write_raw(output, &header)?; if let Some(palette) = palette { let mut color_count = pal_width*pal_height; for color in palette { - tool_helper::raw::write_generic(output, color.get_raw())?; + tool_helper::raw::write_raw(output, color)?; color_count -= 1; } while color_count > 0 { - tool_helper::raw::write_generic(output, PSXColor::black().get_raw())?; + tool_helper::raw::write_raw(output, &PSXColor::black())?; color_count -= 1; } } for color in image { - tool_helper::raw::write_generic(output, color.get_raw())?; + tool_helper::raw::write_raw(output, &color)?; } Ok(()) diff --git a/src/Tools/jaby_engine_fconv/src/images/reduced_tim/types.rs b/src/Tools/jaby_engine_fconv/src/images/reduced_tim/types.rs index e9600a38..44f356d2 100644 --- a/src/Tools/jaby_engine_fconv/src/images/reduced_tim/types.rs +++ b/src/Tools/jaby_engine_fconv/src/images/reduced_tim/types.rs @@ -1,4 +1,4 @@ -use tool_helper::{*, bits::BitRange}; +use tool_helper::{*, bits::BitRange, raw::RawConversion}; #[repr(packed(1))] #[allow(dead_code)] @@ -102,10 +102,6 @@ impl Color { Color{value} } - pub const fn get_raw(&self) -> u16 { - self.value.to_le() - } - const fn new(stp: u8, red: u8, green: u8, blue: u8) -> Color { let value = set_member_value!(set_member_value!(set_member_value!(set_member_value!(0, stp, 0, u16), @@ -122,6 +118,12 @@ impl Color { make_member_getter_setter!(blue, Self::COLOR_SHIFT, u16); } +impl RawConversion<2> for Color { + fn convert_to_raw(&self) -> [u8; 2] { + self.value.to_le_bytes() + } +} + #[allow(dead_code)] impl Header { const TEX_WIDTH_BIT_RANGE: BitRange = BitRange::from_to(0, 8); @@ -146,6 +148,12 @@ impl Header { } } +impl RawConversion<4> for Header { + fn convert_to_raw(&self) -> [u8; 4] { + self.value.to_le_bytes() + } +} + pub trait PSXImageConverter: std::iter::Iterator { fn width(&self) -> u16; fn height(&self) -> u16; diff --git a/src/Tools/tool_helper/Cargo.toml b/src/Tools/tool_helper/Cargo.toml index ae2fa4c8..1373c636 100644 --- a/src/Tools/tool_helper/Cargo.toml +++ b/src/Tools/tool_helper/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tool_helper" -version = "0.8.1" +version = "0.9.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/Tools/tool_helper/src/raw.rs b/src/Tools/tool_helper/src/raw.rs index 3350213a..29875eb0 100644 --- a/src/Tools/tool_helper/src/raw.rs +++ b/src/Tools/tool_helper/src/raw.rs @@ -1,7 +1,9 @@ use super::{Error, Write}; -pub fn write_generic(output: &mut dyn Write, value: T) -> Result { - let raw = unsafe {std::slice::from_raw_parts(&value as *const _ as *const u8, std::mem::size_of_val(&value))}; +pub trait RawConversion { + fn convert_to_raw(&self) -> [u8; COUNT]; +} - Error::try_or_new(output.write(raw)) +pub fn write_raw, const COUNT:usize>(output: &mut dyn Write, value: &T) -> Result { + Error::try_or_new(output.write(&value.convert_to_raw())) } \ No newline at end of file