Replace unsafe code with raw trait

This commit is contained in:
2023-05-01 23:01:35 +02:00
parent 513275fba8
commit a50e0ec544
4 changed files with 24 additions and 14 deletions

View File

@@ -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

View File

@@ -1,7 +1,9 @@
use super::{Error, Write};
pub fn write_generic<T>(output: &mut dyn Write, value: T) -> Result<usize, Error> {
let raw = unsafe {std::slice::from_raw_parts(&value as *const _ as *const u8, std::mem::size_of_val(&value))};
pub trait RawConversion<const COUNT:usize> {
fn convert_to_raw(&self) -> [u8; COUNT];
}
Error::try_or_new(output.write(raw))
pub fn write_raw<T:RawConversion<COUNT>, const COUNT:usize>(output: &mut dyn Write, value: &T) -> Result<usize, Error> {
Error::try_or_new(output.write(&value.convert_to_raw()))
}