diff --git a/lazuli_core/src/client/connector.rs b/lazuli_core/src/client/connector.rs index 1c4765b..dcef8b0 100644 --- a/lazuli_core/src/client/connector.rs +++ b/lazuli_core/src/client/connector.rs @@ -8,7 +8,7 @@ use std::{ use log::trace; -use crate::{stream::Stream, ArcMutex, PacketHeader, Result, Sendable, UnknownType}; +use crate::{sendable, stream::Stream, ArcMutex, PacketHeader, Result, Sendable, UnknownType}; /// A single byte type that is used to store the raw data. #[repr(transparent)] @@ -34,7 +34,7 @@ impl StreamConnector { vec_ptr: unsafe { mem::transmute(stream.get_ptr()) }, size: mem::size_of::(), grew: stream.get_grow_by(), - conversion_fn: T::as_conversion_fn(), + conversion_fn: sendable::as_conversion_fn::(), type_name: std::any::type_name::(), } } diff --git a/lazuli_core/src/sendable.rs b/lazuli_core/src/sendable.rs index 6480ea2..83db599 100644 --- a/lazuli_core/src/sendable.rs +++ b/lazuli_core/src/sendable.rs @@ -43,23 +43,24 @@ pub trait Sendable: Sized + std::fmt::Debug { /// Converts an incoming stream of bytes to the type. fn recv(data: &mut dyn Read) -> Result; +} - /// Converts the type to a function that can be used to convert incoming data to the type. - /// Returns a Vec that is the type's representation in memory. - /// This is used as a hacky way to convert the type if the type cant be known at runtime. - fn as_conversion_fn() -> fn(&mut dyn Read) -> Result> { - |data| { - let conversion = Box::new(Self::recv(data)?); - trace!("Converted to bytes: {:?}", conversion); - let as_slice_bytes = unsafe { - // We use a slice to get the bytes of the type. This is safe because we are using the size of the type to get the slice. - slice::from_raw_parts( - Box::leak(conversion) as *mut Self as *mut u8, - mem::size_of::(), - ) - }; - Ok(as_slice_bytes.into()) - } +/// Converts the type to a function that can be used to convert incoming data to the type. +/// This function hides the type of the data, allowing for the conversion function to be used in a generic context. +/// +/// This function is used internally by `StreamConnector`. +pub(crate) fn as_conversion_fn() -> fn(&mut dyn Read) -> Result> { + |data| { + let conversion = Box::new(T::recv(data)?); + trace!("Converted to bytes: {:?}", conversion); + let as_slice_bytes = unsafe { + // We use a slice to get the bytes of the type. This is safe because we are using the size of the type to get the slice. + slice::from_raw_parts( + Box::leak(conversion) as *mut T as *mut u8, + mem::size_of::(), + ) + }; + Ok(as_slice_bytes.into()) } }