Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove as sendable fn #9

Merged
merged 2 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lazuli_core/src/client/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -34,7 +34,7 @@ impl StreamConnector {
vec_ptr: unsafe { mem::transmute(stream.get_ptr()) },
size: mem::size_of::<T>(),
grew: stream.get_grow_by(),
conversion_fn: T::as_conversion_fn(),
conversion_fn: sendable::as_conversion_fn::<T>(),
type_name: std::any::type_name::<T>(),
}
}
Expand Down
33 changes: 17 additions & 16 deletions lazuli_core/src/sendable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self>;
}

/// Converts the type to a function that can be used to convert incoming data to the type.
/// Returns a Vec<u8> 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<Box<[u8]>> {
|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::<Self>(),
)
};
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<T: Sendable>() -> fn(&mut dyn Read) -> Result<Box<[u8]>> {
|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::<T>(),
)
};
Ok(as_slice_bytes.into())
}
}

Expand Down