diff options
author | wanglei01 <wanglei01@alibaba-inc.com> | 2021-07-29 11:30:49 +0800 |
---|---|---|
committer | Sergio Lopez <slp@sinrega.org> | 2021-07-29 08:53:15 +0200 |
commit | 488b3adc2f26d150def93949d51cfdd37528a37d (patch) | |
tree | 433a2c15bdc57f513bfbb5998b987da21ae5e367 | |
parent | c1f77c778bc482395bb1f9d0d4961e67f30c83eb (diff) | |
download | vmm_vhost-488b3adc2f26d150def93949d51cfdd37528a37d.tar.gz |
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
<https://github.com/rust-lang/rust/issues/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 <wllenyj@linux.alibaba.com>
-rw-r--r-- | coverage_config_x86_64.json | 2 | ||||
-rw-r--r-- | src/vhost_user/message.rs | 32 |
2 files changed, 28 insertions, 6 deletions
diff --git a/coverage_config_x86_64.json b/coverage_config_x86_64.json index 8f24950..c3e6939 100644 --- a/coverage_config_x86_64.json +++ b/coverage_config_x86_64.json @@ -1 +1 @@ -{"coverage_score": 82.2, "exclude_path": "src/vhost_kern/", "crate_features": "vhost-user-master,vhost-user-slave"} +{"coverage_score": 82.3, "exclude_path": "src/vhost_kern/", "crate_features": "vhost-user-master,vhost-user-slave"} diff --git a/src/vhost_user/message.rs b/src/vhost_user/message.rs index f6eead2..fc33e1b 100644 --- a/src/vhost_user/message.rs +++ b/src/vhost_user/message.rs @@ -223,9 +223,8 @@ bitflags! { /// Common message header for vhost-user requests and replies. /// A vhost-user message consists of 3 header fields and an optional payload. All numbers are in the /// machine native byte order. -#[allow(safe_packed_borrows)] #[repr(packed)] -#[derive(Debug, Clone, Copy, PartialEq)] +#[derive(Copy)] pub(super) struct VhostUserMsgHeader<R: Req> { request: u32, flags: u32, @@ -233,6 +232,28 @@ pub(super) struct VhostUserMsgHeader<R: Req> { _r: PhantomData<R>, } +impl<R: Req> Debug for VhostUserMsgHeader<R> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Point") + .field("request", &{ self.request }) + .field("flags", &{ self.flags }) + .field("size", &{ self.size }) + .finish() + } +} + +impl<R: Req> Clone for VhostUserMsgHeader<R> { + fn clone(&self) -> VhostUserMsgHeader<R> { + *self + } +} + +impl<R: Req> PartialEq for VhostUserMsgHeader<R> { + fn eq(&self, other: &Self) -> bool { + self.request == other.request && self.flags == other.flags && self.size == other.size + } +} + impl<R: Req> VhostUserMsgHeader<R> { /// Create a new instance of `VhostUserMsgHeader`. pub fn new(request: R, flags: u32, size: u32) -> Self { @@ -249,7 +270,7 @@ impl<R: Req> VhostUserMsgHeader<R> { /// Get message type. pub fn get_code(&self) -> R { // It's safe because R is marked as repr(u32). - unsafe { std::mem::transmute_copy::<u32, R>(&self.request) } + unsafe { std::mem::transmute_copy::<u32, R>(&{ self.request }) } } /// Set message type. @@ -803,7 +824,6 @@ impl DescStateSplit { } /// Inflight I/O queue region for split virtqueues -#[allow(safe_packed_borrows)] #[repr(packed)] pub struct QueueRegionSplit { /// Features flags of this region @@ -868,7 +888,6 @@ impl DescStatePacked { } /// Inflight I/O queue region for packed virtqueues -#[allow(safe_packed_borrows)] #[repr(packed)] pub struct QueueRegionPacked { /// Features flags of this region @@ -994,7 +1013,10 @@ mod tests { hdr.set_version(0x1); assert!(hdr.is_valid()); + // Test Debug, Clone, PartiaEq trait assert_eq!(hdr, hdr.clone()); + assert_eq!(hdr.clone().get_code(), hdr.get_code()); + assert_eq!(format!("{:?}", hdr.clone()), format!("{:?}", hdr)); } #[test] |