@@ -12,11 +12,11 @@ impl<T: 'static + Copy + Sized + Send + Sync + std::fmt::Debug> Pod for T {}
12
12
13
13
pub trait Messageable : Pod { }
14
14
15
- /// T represents an Enum which tells both sides what kind of message is being
15
+ /// T represents should probably be a Enum which tells both sides what kind of message is being
16
16
/// passed in the body of the message
17
17
#[ derive( Debug , Clone , Copy ) ]
18
18
pub struct MessageHeader < T : Messageable > {
19
- /// The kind of invariant in the message body, used as an identifier
19
+ /// Type of message`
20
20
pub id : T ,
21
21
/// the length of the message in bytes
22
22
pub size : u32 ,
@@ -54,19 +54,14 @@ impl<T: Messageable> Message<T> {
54
54
unsafe {
55
55
let data_ptr: * const V = & data;
56
56
let byte_ptr: * const u8 = data_ptr as * const _ ;
57
- // SAFETY:
58
- // We are just reinterpetting the type as a slice of bytes and since the type is
59
- // constrained by the Pod trait we know everything on the type is actually on the type
60
- let byte_slice: & [ u8 ] = std:: slice:: from_raw_parts ( byte_ptr, std:: mem:: size_of :: < V > ( ) ) ;
61
-
62
57
// SAFETY:
63
58
// Since we resized the Vec to fit the existing data along with the number of bytes in
64
59
// the incoming type, we can be sure that there is enough space to copy the actual
65
60
// data
66
61
// The source is known to be aligned since it is just a reinterpetation of an existing
67
62
// valid Pod type
68
63
std:: ptr:: copy (
69
- & byte_slice [ 0 ] ,
64
+ byte_ptr ,
70
65
self . body . as_mut_ptr ( ) . add ( i) ,
71
66
std:: mem:: size_of :: < V > ( ) ,
72
67
) ;
@@ -75,7 +70,7 @@ impl<T: Messageable> Message<T> {
75
70
self . header . size = self . size ( ) ;
76
71
}
77
72
78
- /// This will reinterpet the end of the message body's bytes as the type yo urequest ,
73
+ /// This will reinterpet the end of the message body's bytes as the type you request ,
79
74
/// there is no built in validation at the moment.
80
75
pub fn pull < V : Pod > ( & mut self ) -> Result < V , MessageError > {
81
76
let bytes = std:: mem:: size_of :: < V > ( ) ;
@@ -89,12 +84,6 @@ impl<T: Messageable> Message<T> {
89
84
let new_len = self . body . len ( ) - bytes;
90
85
91
86
let out: V = unsafe {
92
- // SAFETY:
93
- // We know the message has enough bytes to pull an instance of V out based on the
94
- // checks done above
95
- let data_ptr = self . body . as_ptr ( ) . add ( new_len) ;
96
- let byte_slice: & [ u8 ] = std:: slice:: from_raw_parts ( data_ptr, bytes) ;
97
-
98
87
// SAFETY:
99
88
// We know that the slice of bytes has the proper number of bytes to transmute into an
100
89
// instance of `V` due to the above code deriving the values based on the `size_of` calls
@@ -104,7 +93,11 @@ impl<T: Messageable> Message<T> {
104
93
// This call will reinterpet the bytes as an instance of `V`, there is currently not
105
94
// parity check on the result of this reinterpetation, the burden of doing some
106
95
// validation is currently on the caller
107
- std:: mem:: transmute_copy ( & byte_slice[ 0 ] )
96
+ self . body
97
+ . as_ptr ( )
98
+ . offset ( new_len as isize )
99
+ . cast :: < V > ( )
100
+ . read_unaligned ( )
108
101
} ;
109
102
110
103
self . body . resize ( new_len, 0 ) ;
@@ -150,7 +143,8 @@ impl<T: Messageable> From<&[u8]> for Message<T> {
150
143
panic ! ( "no, this is not header" ) ;
151
144
}
152
145
153
- let header: MessageHeader < T > = unsafe { std:: mem:: transmute_copy ( & bytes[ 0 ] ) } ;
146
+ let header: MessageHeader < T > =
147
+ unsafe { bytes. as_ptr ( ) . cast :: < MessageHeader < T > > ( ) . read_unaligned ( ) } ;
154
148
155
149
if header. size != bytes_len as u32 {
156
150
panic ! (
0 commit comments