Skip to content

Commit 488b3ad

Browse files
wanglei01slp
wanglei01
authored andcommitted
fix warning: unaligned_references
fix warning, when compiling with 1.53.0 ``` warning: reference to packed field is unaligned --> src/vhost_user/message.rs:252:53 | 252 | unsafe { std::mem::transmute_copy::<u32, R>(&self.request) } | ^^^^^^^^^^^^^ | = note: `#[warn(unaligned_references)]` on by default = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #82523 <rust-lang/rust#82523> = note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) ``` Signed-off-by: wanglei <[email protected]>
1 parent c1f77c7 commit 488b3ad

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

coverage_config_x86_64.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"coverage_score": 82.2, "exclude_path": "src/vhost_kern/", "crate_features": "vhost-user-master,vhost-user-slave"}
1+
{"coverage_score": 82.3, "exclude_path": "src/vhost_kern/", "crate_features": "vhost-user-master,vhost-user-slave"}

src/vhost_user/message.rs

+27-5
Original file line numberDiff line numberDiff line change
@@ -223,16 +223,37 @@ bitflags! {
223223
/// Common message header for vhost-user requests and replies.
224224
/// A vhost-user message consists of 3 header fields and an optional payload. All numbers are in the
225225
/// machine native byte order.
226-
#[allow(safe_packed_borrows)]
227226
#[repr(packed)]
228-
#[derive(Debug, Clone, Copy, PartialEq)]
227+
#[derive(Copy)]
229228
pub(super) struct VhostUserMsgHeader<R: Req> {
230229
request: u32,
231230
flags: u32,
232231
size: u32,
233232
_r: PhantomData<R>,
234233
}
235234

235+
impl<R: Req> Debug for VhostUserMsgHeader<R> {
236+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
237+
f.debug_struct("Point")
238+
.field("request", &{ self.request })
239+
.field("flags", &{ self.flags })
240+
.field("size", &{ self.size })
241+
.finish()
242+
}
243+
}
244+
245+
impl<R: Req> Clone for VhostUserMsgHeader<R> {
246+
fn clone(&self) -> VhostUserMsgHeader<R> {
247+
*self
248+
}
249+
}
250+
251+
impl<R: Req> PartialEq for VhostUserMsgHeader<R> {
252+
fn eq(&self, other: &Self) -> bool {
253+
self.request == other.request && self.flags == other.flags && self.size == other.size
254+
}
255+
}
256+
236257
impl<R: Req> VhostUserMsgHeader<R> {
237258
/// Create a new instance of `VhostUserMsgHeader`.
238259
pub fn new(request: R, flags: u32, size: u32) -> Self {
@@ -249,7 +270,7 @@ impl<R: Req> VhostUserMsgHeader<R> {
249270
/// Get message type.
250271
pub fn get_code(&self) -> R {
251272
// It's safe because R is marked as repr(u32).
252-
unsafe { std::mem::transmute_copy::<u32, R>(&self.request) }
273+
unsafe { std::mem::transmute_copy::<u32, R>(&{ self.request }) }
253274
}
254275

255276
/// Set message type.
@@ -803,7 +824,6 @@ impl DescStateSplit {
803824
}
804825

805826
/// Inflight I/O queue region for split virtqueues
806-
#[allow(safe_packed_borrows)]
807827
#[repr(packed)]
808828
pub struct QueueRegionSplit {
809829
/// Features flags of this region
@@ -868,7 +888,6 @@ impl DescStatePacked {
868888
}
869889

870890
/// Inflight I/O queue region for packed virtqueues
871-
#[allow(safe_packed_borrows)]
872891
#[repr(packed)]
873892
pub struct QueueRegionPacked {
874893
/// Features flags of this region
@@ -994,7 +1013,10 @@ mod tests {
9941013
hdr.set_version(0x1);
9951014
assert!(hdr.is_valid());
9961015

1016+
// Test Debug, Clone, PartiaEq trait
9971017
assert_eq!(hdr, hdr.clone());
1018+
assert_eq!(hdr.clone().get_code(), hdr.get_code());
1019+
assert_eq!(format!("{:?}", hdr.clone()), format!("{:?}", hdr));
9981020
}
9991021

10001022
#[test]

0 commit comments

Comments
 (0)