Skip to content

Commit

Permalink
Make FixedByteSize a regular trait and move it to ragnarok_bytes
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vE5li committed Mar 9, 2024
1 parent 906ba84 commit 8751191
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 163 deletions.
38 changes: 19 additions & 19 deletions ragnarok_bytes/src/fixed/implement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Self>()
}
}

impl const FixedByteSize for u16 {
impl FixedByteSize for u16 {
fn size_in_bytes() -> usize {
core::mem::size_of::<Self>()
}
}

impl const FixedByteSize for u32 {
impl FixedByteSize for u32 {
fn size_in_bytes() -> usize {
core::mem::size_of::<Self>()
}
}

impl const FixedByteSize for u64 {
impl FixedByteSize for u64 {
fn size_in_bytes() -> usize {
core::mem::size_of::<Self>()
}
}

impl const FixedByteSize for i8 {
impl FixedByteSize for i8 {
fn size_in_bytes() -> usize {
core::mem::size_of::<Self>()
}
}

impl const FixedByteSize for i16 {
impl FixedByteSize for i16 {
fn size_in_bytes() -> usize {
core::mem::size_of::<Self>()
}
}

impl const FixedByteSize for i32 {
impl FixedByteSize for i32 {
fn size_in_bytes() -> usize {
core::mem::size_of::<Self>()
}
}

impl const FixedByteSize for i64 {
impl FixedByteSize for i64 {
fn size_in_bytes() -> usize {
core::mem::size_of::<Self>()
}
}

impl const FixedByteSize for f32 {
impl FixedByteSize for f32 {
fn size_in_bytes() -> usize {
core::mem::size_of::<Self>()
}
}

impl const FixedByteSize for f64 {
impl FixedByteSize for f64 {
fn size_in_bytes() -> usize {
core::mem::size_of::<Self>()
}
}

impl<T: ~const FixedByteSize, const SIZE: usize> const FixedByteSize for [T; SIZE] {
impl<T: FixedByteSize, const SIZE: usize> FixedByteSize for [T; SIZE] {
fn size_in_bytes() -> usize {
T::size_in_bytes() * SIZE
}
}

#[cfg(feature = "cgmath")]
impl<T: ~const FixedByteSize> const FixedByteSize for Vector2<T> {
impl<T: FixedByteSize> FixedByteSize for Vector2<T> {
fn size_in_bytes() -> usize {
T::size_in_bytes() * 2
}
}

#[cfg(feature = "cgmath")]
impl<T: ~const FixedByteSize> const FixedByteSize for Vector3<T> {
impl<T: FixedByteSize> FixedByteSize for Vector3<T> {
fn size_in_bytes() -> usize {
T::size_in_bytes() * 3
}
}

#[cfg(feature = "cgmath")]
impl<T: ~const FixedByteSize> const FixedByteSize for Vector4<T> {
impl<T: FixedByteSize> FixedByteSize for Vector4<T> {
fn size_in_bytes() -> usize {
T::size_in_bytes() * 4
}
}

#[cfg(feature = "cgmath")]
impl<T: ~const FixedByteSize> const FixedByteSize for Quaternion<T> {
impl<T: FixedByteSize> FixedByteSize for Quaternion<T> {
fn size_in_bytes() -> usize {
T::size_in_bytes() * 4
}
}

#[cfg(feature = "cgmath")]
impl<T: ~const FixedByteSize> const FixedByteSize for Matrix3<T> {
impl<T: FixedByteSize> FixedByteSize for Matrix3<T> {
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<T: ~const FixedByteSize> const FixedByteSizeWrapper for Vec<T> {
impl<T: FixedByteSize> FixedByteSizeCollection for Vec<T> {
fn size_in_bytes() -> usize {
T::size_in_bytes()
}
Expand Down
8 changes: 5 additions & 3 deletions ragnarok_bytes/src/fixed/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
}
6 changes: 2 additions & 4 deletions ragnarok_bytes/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
8 changes: 4 additions & 4 deletions ragnarok_procedural/src/fixed_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;)*
Expand All @@ -45,9 +45,9 @@ pub fn derive_fixed_byte_size_enum(generics: Generics, mut attributes: Vec<Attri
.unwrap_or_else(|| Ident::new("u8", Span::call_site()));

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 {
<#numeric_type as crate::loaders::FixedByteSize>::size_in_bytes()
<#numeric_type as ragnarok_bytes::FixedByteSize>::size_in_bytes()
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion ragnarok_procedural/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ pub fn byte_convertable_helper(data_struct: DataStruct) -> (Vec<TokenStream>, 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);

Expand Down
11 changes: 4 additions & 7 deletions src/loaders/archive/native/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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 {
Expand All @@ -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();
Expand Down Expand Up @@ -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();

Expand Down
121 changes: 0 additions & 121 deletions src/loaders/fixed.rs

This file was deleted.

4 changes: 0 additions & 4 deletions src/loaders/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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")]
Expand Down

0 comments on commit 8751191

Please sign in to comment.