@@ -43,23 +43,24 @@ pub trait Sendable: Sized + std::fmt::Debug {
43
43
44
44
/// Converts an incoming stream of bytes to the type.
45
45
fn recv ( data : & mut dyn Read ) -> Result < Self > ;
46
+ }
46
47
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 ( ) )
63
64
}
64
65
}
65
66
0 commit comments