Skip to content

Commit

Permalink
Add ConversionResult type alias
Browse files Browse the repository at this point in the history
  • Loading branch information
vE5li committed Mar 8, 2024
1 parent 58cdc02 commit 2276daa
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 91 deletions.
8 changes: 4 additions & 4 deletions procedural/src/byte/convertable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn derive_for_struct(
let from = implement_from.then(|| {
quote! {
impl #impl_generics crate::loaders::FromBytes for #name #type_generics #where_clause {
fn from_bytes<META>(byte_stream: &mut crate::loaders::ByteStream<META>) -> Result<Self, Box<crate::loaders::ConversionError>> {
fn from_bytes<META>(byte_stream: &mut crate::loaders::ByteStream<META>) -> crate::loaders::ConversionResult<Self> {
let base_offset = byte_stream.get_offset();
#(#from_bytes_implementations)*
Ok(#instanciate)
Expand All @@ -39,7 +39,7 @@ fn derive_for_struct(
impl #impl_generics crate::loaders::ToBytes for #name #type_generics #where_clause {
// Temporary until serialization is always possible
#[allow(unreachable_code)]
fn to_bytes(&self) -> Result<Vec<u8>, Box<crate::loaders::ConversionError>> {
fn to_bytes(&self) -> crate::loaders::ConversionResult<Vec<u8>> {
Ok([#(#to_bytes_implementations),*].concat())
}
}
Expand Down Expand Up @@ -88,7 +88,7 @@ fn derive_for_enum(
let from = add_from.then(|| {
quote! {
impl #impl_generics crate::loaders::FromBytes for #name #type_generics #where_clause {
fn from_bytes<META>(byte_stream: &mut ByteStream<META>) -> Result<Self, Box<crate::loaders::ConversionError>> {
fn from_bytes<META>(byte_stream: &mut ByteStream<META>) -> crate::loaders::ConversionResult<Self> {
match crate::loaders::conversion_result::<Self, _>(#numeric_type::from_bytes(byte_stream))? as usize {
#( #indices => Ok(Self::#values), )*
invalid => Err(crate::loaders::ConversionError::from_message(format!("invalid enum variant {}", invalid))),
Expand All @@ -103,7 +103,7 @@ fn derive_for_enum(
impl #impl_generics crate::loaders::ToBytes for #name #type_generics #where_clause {
// Temporary until serialization is always possible
#[allow(unreachable_code)]
fn to_bytes(&self) -> Result<Vec<u8>, Box<crate::loaders::ConversionError>> {
fn to_bytes(&self) -> crate::loaders::ConversionResult<Vec<u8>> {
match self {
#( #name::#values => crate::loaders::conversion_result::<Self, _>((#indices as #numeric_type).to_bytes()), )*
}
Expand Down
5 changes: 2 additions & 3 deletions procedural/src/byte/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ pub fn derive_incoming_packet_struct(
const IS_PING: bool = #is_ping;
const HEADER: u16 = #signature;

fn from_bytes<META>(byte_stream: &mut crate::loaders::ByteStream<META>) -> Result<Self, Box<crate::loaders::ConversionError>> {

fn from_bytes<META>(byte_stream: &mut crate::loaders::ByteStream<META>) -> crate::loaders::ConversionResult<Self> {
let base_offset = byte_stream.get_offset();
#(#from_bytes_implementations)*
let packet = #instanciate;
Expand Down Expand Up @@ -73,7 +72,7 @@ pub fn derive_outgoing_packet_struct(

// Temporary until serialization is always possible
#[allow(unreachable_code)]
fn to_bytes(&self) -> Result<Vec<u8>, Box<crate::loaders::ConversionError>> {
fn to_bytes(&self) -> crate::loaders::ConversionResult<Vec<u8>> {
Ok(#to_bytes)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/graphics/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::ops::Add;
use cgmath::{Deg, Rad, Vector3};
use procedural::*;

use crate::loaders::{conversion_result, ConversionError, FromBytes};
use crate::loaders::{conversion_result, ConversionResult, FromBytes};

#[derive(Copy, Clone, Debug, Named, PrototypeElement)]
pub struct Transform {
Expand All @@ -14,7 +14,7 @@ pub struct Transform {
}

impl FromBytes for Transform {
fn from_bytes<META>(byte_stream: &mut crate::loaders::ByteStream<META>) -> Result<Self, Box<ConversionError>> {
fn from_bytes<META>(byte_stream: &mut crate::loaders::ByteStream<META>) -> ConversionResult<Self> {
let mut position = conversion_result::<Self, _>(<Vector3<f32>>::from_bytes(byte_stream))?;
let rotation = conversion_result::<Self, _>(<Vector3<f32>>::from_bytes(byte_stream))?;
let scale = conversion_result::<Self, _>(<Vector3<f32>>::from_bytes(byte_stream))?;
Expand Down
4 changes: 2 additions & 2 deletions src/loaders/convertable/helper.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{ConversionError, ConversionErrorType, Named};
use super::{ConversionError, ConversionErrorType, ConversionResult, Named};

#[inline(always)]
pub fn check_upper_bound<S: Named>(offset: usize, length: usize) -> Result<(), Box<ConversionError>> {
Expand All @@ -11,7 +11,7 @@ pub fn check_upper_bound<S: Named>(offset: usize, length: usize) -> Result<(), B
}

#[inline(always)]
pub fn conversion_result<S: Named, T>(result: Result<T, Box<ConversionError>>) -> Result<T, Box<ConversionError>> {
pub fn conversion_result<S: Named, T>(result: ConversionResult<T>) -> ConversionResult<T> {
result.map_err(|mut error| {
error.add_to_stack(S::NAME);
error
Expand Down
Loading

0 comments on commit 2276daa

Please sign in to comment.