Skip to content

Commit c9db5bf

Browse files
Merge pull request #9 from quackitsquinn/remove-as_sendable_fn
Remove as sendable fn
2 parents 9738564 + a1701c8 commit c9db5bf

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

Diff for: lazuli_core/src/client/connector.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::{
88

99
use log::trace;
1010

11-
use crate::{stream::Stream, ArcMutex, PacketHeader, Result, Sendable, UnknownType};
11+
use crate::{sendable, stream::Stream, ArcMutex, PacketHeader, Result, Sendable, UnknownType};
1212

1313
/// A single byte type that is used to store the raw data.
1414
#[repr(transparent)]
@@ -34,7 +34,7 @@ impl StreamConnector {
3434
vec_ptr: unsafe { mem::transmute(stream.get_ptr()) },
3535
size: mem::size_of::<T>(),
3636
grew: stream.get_grow_by(),
37-
conversion_fn: T::as_conversion_fn(),
37+
conversion_fn: sendable::as_conversion_fn::<T>(),
3838
type_name: std::any::type_name::<T>(),
3939
}
4040
}

Diff for: lazuli_core/src/sendable.rs

+17-16
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,24 @@ pub trait Sendable: Sized + std::fmt::Debug {
4343

4444
/// Converts an incoming stream of bytes to the type.
4545
fn recv(data: &mut dyn Read) -> Result<Self>;
46+
}
4647

47-
/// Converts the type to a function that can be used to convert incoming data to the type.
48-
/// Returns a Vec<u8> that is the type's representation in memory.
49-
/// This is used as a hacky way to convert the type if the type cant be known at runtime.
50-
fn as_conversion_fn() -> fn(&mut dyn Read) -> Result<Box<[u8]>> {
51-
|data| {
52-
let conversion = Box::new(Self::recv(data)?);
53-
trace!("Converted to bytes: {:?}", conversion);
54-
let as_slice_bytes = unsafe {
55-
// 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.
56-
slice::from_raw_parts(
57-
Box::leak(conversion) as *mut Self as *mut u8,
58-
mem::size_of::<Self>(),
59-
)
60-
};
61-
Ok(as_slice_bytes.into())
62-
}
48+
/// Converts the type to a function that can be used to convert incoming data to the type.
49+
/// This function hides the type of the data, allowing for the conversion function to be used in a generic context.
50+
///
51+
/// This function is used internally by `StreamConnector`.
52+
pub(crate) fn as_conversion_fn<T: Sendable>() -> fn(&mut dyn Read) -> Result<Box<[u8]>> {
53+
|data| {
54+
let conversion = Box::new(T::recv(data)?);
55+
trace!("Converted to bytes: {:?}", conversion);
56+
let as_slice_bytes = unsafe {
57+
// 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.
58+
slice::from_raw_parts(
59+
Box::leak(conversion) as *mut T as *mut u8,
60+
mem::size_of::<T>(),
61+
)
62+
};
63+
Ok(as_slice_bytes.into())
6364
}
6465
}
6566

0 commit comments

Comments
 (0)