From 8751191f4e2c2ad9c62387e661bba69229586f95 Mon Sep 17 00:00:00 2001 From: vE5li Date: Sat, 9 Mar 2024 14:32:58 +0100 Subject: [PATCH] Make FixedByteSize a regular trait and move it to ragnarok_bytes Moving the const FixedByteSize trait to another crate caused the compiler to crash while building. Since I need this trait to be accessible outside of the main crate, I had to make it non-const for now. This is to be reverted once const traits are more stable. --- ragnarok_bytes/src/fixed/implement.rs | 38 ++++---- ragnarok_bytes/src/fixed/mod.rs | 8 +- ragnarok_bytes/src/lib.rs | 6 +- ragnarok_procedural/src/fixed_size.rs | 8 +- ragnarok_procedural/src/helper.rs | 2 +- src/loaders/archive/native/mod.rs | 11 +-- src/loaders/fixed.rs | 121 -------------------------- src/loaders/mod.rs | 4 - 8 files changed, 35 insertions(+), 163 deletions(-) delete mode 100644 src/loaders/fixed.rs diff --git a/ragnarok_bytes/src/fixed/implement.rs b/ragnarok_bytes/src/fixed/implement.rs index 72e8371f..c14760fc 100644 --- a/ragnarok_bytes/src/fixed/implement.rs +++ b/ragnarok_bytes/src/fixed/implement.rs @@ -3,117 +3,117 @@ use std::net::Ipv4Addr; #[cfg(feature = "cgmath")] use cgmath::{Matrix3, Quaternion, Vector2, Vector3, Vector4}; -use super::{FixedByteSize, FixedByteSizeWrapper}; +use super::{FixedByteSize, FixedByteSizeCollection}; -impl const FixedByteSize for u8 { +impl FixedByteSize for u8 { fn size_in_bytes() -> usize { core::mem::size_of::() } } -impl const FixedByteSize for u16 { +impl FixedByteSize for u16 { fn size_in_bytes() -> usize { core::mem::size_of::() } } -impl const FixedByteSize for u32 { +impl FixedByteSize for u32 { fn size_in_bytes() -> usize { core::mem::size_of::() } } -impl const FixedByteSize for u64 { +impl FixedByteSize for u64 { fn size_in_bytes() -> usize { core::mem::size_of::() } } -impl const FixedByteSize for i8 { +impl FixedByteSize for i8 { fn size_in_bytes() -> usize { core::mem::size_of::() } } -impl const FixedByteSize for i16 { +impl FixedByteSize for i16 { fn size_in_bytes() -> usize { core::mem::size_of::() } } -impl const FixedByteSize for i32 { +impl FixedByteSize for i32 { fn size_in_bytes() -> usize { core::mem::size_of::() } } -impl const FixedByteSize for i64 { +impl FixedByteSize for i64 { fn size_in_bytes() -> usize { core::mem::size_of::() } } -impl const FixedByteSize for f32 { +impl FixedByteSize for f32 { fn size_in_bytes() -> usize { core::mem::size_of::() } } -impl const FixedByteSize for f64 { +impl FixedByteSize for f64 { fn size_in_bytes() -> usize { core::mem::size_of::() } } -impl const FixedByteSize for [T; SIZE] { +impl FixedByteSize for [T; SIZE] { fn size_in_bytes() -> usize { T::size_in_bytes() * SIZE } } #[cfg(feature = "cgmath")] -impl const FixedByteSize for Vector2 { +impl FixedByteSize for Vector2 { fn size_in_bytes() -> usize { T::size_in_bytes() * 2 } } #[cfg(feature = "cgmath")] -impl const FixedByteSize for Vector3 { +impl FixedByteSize for Vector3 { fn size_in_bytes() -> usize { T::size_in_bytes() * 3 } } #[cfg(feature = "cgmath")] -impl const FixedByteSize for Vector4 { +impl FixedByteSize for Vector4 { fn size_in_bytes() -> usize { T::size_in_bytes() * 4 } } #[cfg(feature = "cgmath")] -impl const FixedByteSize for Quaternion { +impl FixedByteSize for Quaternion { fn size_in_bytes() -> usize { T::size_in_bytes() * 4 } } #[cfg(feature = "cgmath")] -impl const FixedByteSize for Matrix3 { +impl FixedByteSize for Matrix3 { fn size_in_bytes() -> usize { T::size_in_bytes() * 9 } } #[cfg(feature = "cgmath")] -impl const FixedByteSize for Ipv4Addr { +impl FixedByteSize for Ipv4Addr { fn size_in_bytes() -> usize { 4 } } -impl const FixedByteSizeWrapper for Vec { +impl FixedByteSizeCollection for Vec { fn size_in_bytes() -> usize { T::size_in_bytes() } diff --git a/ragnarok_bytes/src/fixed/mod.rs b/ragnarok_bytes/src/fixed/mod.rs index 5f13ef7a..2401a43b 100644 --- a/ragnarok_bytes/src/fixed/mod.rs +++ b/ragnarok_bytes/src/fixed/mod.rs @@ -1,11 +1,13 @@ mod implement; -#[const_trait] +/// Trait for getting the size of Ragnarok Online types. pub trait FixedByteSize { + /// Get the serialized size in bytes. fn size_in_bytes() -> usize; } -#[const_trait] -pub trait FixedByteSizeWrapper { +/// Trait for collections holding elements that implement [`FixedByteSize`]. +pub trait FixedByteSizeCollection { + /// Get the serialized size of the inner type in bytes. fn size_in_bytes() -> usize; } diff --git a/ragnarok_bytes/src/lib.rs b/ragnarok_bytes/src/lib.rs index 6d667984..f8065bb2 100644 --- a/ragnarok_bytes/src/lib.rs +++ b/ragnarok_bytes/src/lib.rs @@ -1,16 +1,14 @@ #![feature(const_trait_impl)] mod error; -// FIX: Not moved for now since it makes the compiler crash :) -// mod fixed; +mod fixed; mod from_bytes; mod helper; mod stream; mod to_bytes; pub use self::error::{ConversionError, ConversionErrorType}; -// FIX: Not moved for now since it makes the compiler crash :) -// pub use self::fixed::{FixedByteSize, FixedByteSizeWrapper}; +pub use self::fixed::{FixedByteSize, FixedByteSizeCollection}; pub use self::from_bytes::{FromBytes, FromBytesExt}; pub use self::helper::{ConversionResult, ConversionResultExt}; pub use self::stream::ByteStream; diff --git a/ragnarok_procedural/src/fixed_size.rs b/ragnarok_procedural/src/fixed_size.rs index 118dddc7..aa5a0455 100644 --- a/ragnarok_procedural/src/fixed_size.rs +++ b/ragnarok_procedural/src/fixed_size.rs @@ -22,11 +22,11 @@ pub fn derive_fixed_byte_size_struct(data_struct: DataStruct, generics: Generics syn::Meta::Path(_) | syn::Meta::NameValue(_) => panic!("expected token stream in attribute"), }) .map(|length_hint| quote!((#length_hint) as usize)) - .unwrap_or(quote!(<#field_type as crate::loaders::FixedByteSize>::size_in_bytes())) + .unwrap_or(quote!(<#field_type as ragnarok_bytes::FixedByteSize>::size_in_bytes())) }); quote! { - impl #impl_generics const crate::loaders::FixedByteSize for #name #type_generics #where_clause { + impl #impl_generics ragnarok_bytes::FixedByteSize for #name #type_generics #where_clause { fn size_in_bytes() -> usize { let mut total = 0; #(total += #sizes;)* @@ -45,9 +45,9 @@ pub fn derive_fixed_byte_size_enum(generics: Generics, mut attributes: Vec usize { - <#numeric_type as crate::loaders::FixedByteSize>::size_in_bytes() + <#numeric_type as ragnarok_bytes::FixedByteSize>::size_in_bytes() } } } diff --git a/ragnarok_procedural/src/helper.rs b/ragnarok_procedural/src/helper.rs index 80c2c9fd..64a68f77 100644 --- a/ragnarok_procedural/src/helper.rs +++ b/ragnarok_procedural/src/helper.rs @@ -125,7 +125,7 @@ pub fn byte_convertable_helper(data_struct: DataStruct) -> (Vec, Ve .expect("repeating_remaining is used but no packet_length attribute is set"); quote!({ - let repeat_count = (#packet_length - ((byte_stream.get_offset() - base_offset) as u16) - 2) / (<#field_type as crate::loaders::FixedByteSizeWrapper>::size_in_bytes() as u16); + let repeat_count = (#packet_length - ((byte_stream.get_offset() - base_offset) as u16) - 2) / (<#field_type as ragnarok_bytes::FixedByteSizeCollection>::size_in_bytes() as u16); // TODO: Add check to make sure this allocation is not too big. let mut vector = Vec::with_capacity(repeat_count as usize); diff --git a/src/loaders/archive/native/mod.rs b/src/loaders/archive/native/mod.rs index 1abc59aa..71b59d56 100644 --- a/src/loaders/archive/native/mod.rs +++ b/src/loaders/archive/native/mod.rs @@ -9,7 +9,7 @@ use std::fs::File; use std::io::{Read, Seek, SeekFrom}; use std::path::Path; -use ragnarok_bytes::{ByteStream, FromBytes}; +use ragnarok_bytes::{ByteStream, FixedByteSize, FromBytes}; use yazi::{decompress, Format}; use self::assettable::AssetTable; @@ -19,7 +19,6 @@ use self::header::Header; #[cfg(feature = "debug")] use crate::debug::*; use crate::loaders::archive::Archive; -use crate::loaders::FixedByteSize; /// Represents a GRF file. GRF Files are an archive to store game assets. /// Each GRF contains a [`Header`] with metadata (number of files, size, @@ -33,8 +32,6 @@ pub struct NativeArchive { const MAGIC_BYTES: &[u8] = b"Master of Magic\0"; const UNPACKED_SIZE_OF_MAGIC_STRING: usize = MAGIC_BYTES.len(); -const UNPACKED_SIZE_OF_ARCHIVEHEADER: usize = Header::size_in_bytes(); -const UNPACKED_SIZE_OF_FILETABLE: usize = AssetTable::size_in_bytes(); impl Archive for NativeArchive { fn from_path(path: &Path) -> Self { @@ -47,13 +44,13 @@ impl Archive for NativeArchive { // Keeping the convenience of using [`loaders::stream::ByteStream`] // while being able to read without buffering the entire file. - let mut file_header_buffer = [0u8; UNPACKED_SIZE_OF_ARCHIVEHEADER]; + let mut file_header_buffer = vec![0; Header::size_in_bytes()]; file.read_exact(&mut file_header_buffer).unwrap(); let file_header = Header::from_bytes(&mut ByteStream::<()>::without_metadata(&file_header_buffer)).unwrap(); file_header.validate_version(); let _ = file.seek(SeekFrom::Current(file_header.get_file_table_offset() as i64)).unwrap(); - let mut file_table_buffer = [0u8; UNPACKED_SIZE_OF_FILETABLE]; + let mut file_table_buffer = vec![0; AssetTable::size_in_bytes()]; file.read_exact(&mut file_table_buffer).unwrap(); let file_table = AssetTable::from_bytes(&mut ByteStream::<()>::without_metadata(&file_table_buffer)).unwrap(); @@ -95,7 +92,7 @@ impl Archive for NativeArchive { return None; } - let position = file_information.offset as u64 + UNPACKED_SIZE_OF_MAGIC_STRING as u64 + UNPACKED_SIZE_OF_ARCHIVEHEADER as u64; + let position = file_information.offset as u64 + UNPACKED_SIZE_OF_MAGIC_STRING as u64 + Header::size_in_bytes() as u64; self.os_file_handler.seek(SeekFrom::Start(position)).unwrap(); self.os_file_handler.read_exact(&mut compressed_file_buffer).unwrap(); diff --git a/src/loaders/fixed.rs b/src/loaders/fixed.rs deleted file mode 100644 index 982b1e7c..00000000 --- a/src/loaders/fixed.rs +++ /dev/null @@ -1,121 +0,0 @@ -use std::net::Ipv4Addr; - -use cgmath::{Matrix3, Quaternion, Vector2, Vector3, Vector4}; - -#[const_trait] -pub trait FixedByteSizeWrapper { - fn size_in_bytes() -> usize; -} - -impl const FixedByteSizeWrapper for Vec { - fn size_in_bytes() -> usize { - T::size_in_bytes() - } -} - -#[const_trait] -pub trait FixedByteSize { - fn size_in_bytes() -> usize; -} - -impl const FixedByteSize for u8 { - fn size_in_bytes() -> usize { - core::mem::size_of::() - } -} - -impl const FixedByteSize for u16 { - fn size_in_bytes() -> usize { - core::mem::size_of::() - } -} - -impl const FixedByteSize for u32 { - fn size_in_bytes() -> usize { - core::mem::size_of::() - } -} - -impl const FixedByteSize for u64 { - fn size_in_bytes() -> usize { - core::mem::size_of::() - } -} - -impl const FixedByteSize for i8 { - fn size_in_bytes() -> usize { - core::mem::size_of::() - } -} - -impl const FixedByteSize for i16 { - fn size_in_bytes() -> usize { - core::mem::size_of::() - } -} - -impl const FixedByteSize for i32 { - fn size_in_bytes() -> usize { - core::mem::size_of::() - } -} - -impl const FixedByteSize for i64 { - fn size_in_bytes() -> usize { - core::mem::size_of::() - } -} - -impl const FixedByteSize for f32 { - fn size_in_bytes() -> usize { - core::mem::size_of::() - } -} - -impl const FixedByteSize for f64 { - fn size_in_bytes() -> usize { - core::mem::size_of::() - } -} - -impl const FixedByteSize for [T; SIZE] { - fn size_in_bytes() -> usize { - T::size_in_bytes() * SIZE - } -} - -impl const FixedByteSize for Vector2 { - fn size_in_bytes() -> usize { - T::size_in_bytes() * 2 - } -} - -impl const FixedByteSize for Vector3 { - fn size_in_bytes() -> usize { - T::size_in_bytes() * 3 - } -} - -impl const FixedByteSize for Vector4 { - fn size_in_bytes() -> usize { - T::size_in_bytes() * 4 - } -} - -impl const FixedByteSize for Quaternion { - fn size_in_bytes() -> usize { - T::size_in_bytes() * 4 - } -} - -impl const FixedByteSize for Matrix3 { - fn size_in_bytes() -> usize { - T::size_in_bytes() * 9 - } -} - -impl const FixedByteSize for Ipv4Addr { - fn size_in_bytes() -> usize { - 4 - } -} diff --git a/src/loaders/mod.rs b/src/loaders/mod.rs index 8fc7bb98..c483b1d9 100644 --- a/src/loaders/mod.rs +++ b/src/loaders/mod.rs @@ -1,8 +1,6 @@ mod action; mod archive; mod effect; -// FIX: Move this to ragnarok_bytes once it doesn't crash the compiler anymore -mod fixed; mod font; mod gamefile; mod map; @@ -15,8 +13,6 @@ mod version; pub use self::action::*; pub use self::effect::{EffectHolder, EffectLoader, *}; -// FIX: Move this to ragnarok_bytes once it doesn't crash the compiler anymore -pub use self::fixed::{FixedByteSize, FixedByteSizeWrapper}; pub use self::font::FontLoader; pub use self::gamefile::*; #[cfg(feature = "debug")]