From 8e9a5a0dbbd50b08f99aa9526987c0f31d203ab3 Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Thu, 11 Feb 2021 15:55:00 +0000 Subject: [PATCH] vm-migration: Use vm-memory's ByteValued instead of own AsBytes This addresses a newly added clippy issue: error: methods called `as_*` usually take self by reference or self by mutable reference; consider choosing a less ambiguous name --> vm-migration/src/protocol.rs:57:34 | 57 | fn as_mut_bytes(p: &mut T) -> &mut [u8] { | ^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#wrong_self_convention error: aborting due to 2 previous errors error: could not compile `vm-migration` Signed-off-by: Rob Bradford --- Cargo.lock | 1 + vm-migration/Cargo.toml | 1 + vm-migration/src/lib.rs | 1 + vm-migration/src/protocol.rs | 31 +++++++++---------------------- 4 files changed, 12 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7384fd43d..b8c503904 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1573,6 +1573,7 @@ dependencies = [ "serde_derive", "serde_json", "thiserror", + "vm-memory", ] [[package]] diff --git a/vm-migration/Cargo.toml b/vm-migration/Cargo.toml index ed732ceb2..8ba5a6013 100644 --- a/vm-migration/Cargo.toml +++ b/vm-migration/Cargo.toml @@ -10,3 +10,4 @@ thiserror = "1.0" serde = {version = ">=1.0.27", features = ["rc"] } serde_derive = ">=1.0.27" serde_json = ">=1.0.9" +vm-memory = { version = "0.5.0", features = ["backend-mmap", "backend-atomic"] } diff --git a/vm-migration/src/lib.rs b/vm-migration/src/lib.rs index e25ace17d..097335493 100644 --- a/vm-migration/src/lib.rs +++ b/vm-migration/src/lib.rs @@ -7,6 +7,7 @@ extern crate serde; extern crate thiserror; #[macro_use] extern crate serde_derive; +extern crate vm_memory; use thiserror::Error; diff --git a/vm-migration/src/protocol.rs b/vm-migration/src/protocol.rs index 6c896fc02..96f2d7eac 100644 --- a/vm-migration/src/protocol.rs +++ b/vm-migration/src/protocol.rs @@ -4,6 +4,7 @@ // use crate::MigratableError; +use vm_memory::ByteValued; // Migration protocol // 1: Source establishes communication with destination (file socket or TCP connection.) @@ -47,22 +48,8 @@ impl Default for Command { } } -trait AsBytes { - fn as_bytes(p: &T) -> &[u8] { - unsafe { - std::slice::from_raw_parts((p as *const T) as *const u8, std::mem::size_of::()) - } - } - - fn as_mut_bytes(p: &mut T) -> &mut [u8] { - unsafe { - std::slice::from_raw_parts_mut((p as *mut T) as *mut u8, std::mem::size_of::()) - } - } -} - #[repr(C)] -#[derive(Default)] +#[derive(Default, Copy, Clone)] pub struct Request { command: Command, padding: [u8; 6], @@ -112,19 +99,19 @@ impl Request { pub fn read_from(fd: &mut dyn Read) -> Result { let mut request = Request::default(); - fd.read_exact(Self::as_mut_bytes(&mut request)) + fd.read_exact(Self::as_mut_slice(&mut request)) .map_err(MigratableError::MigrateSocket)?; Ok(request) } pub fn write_to(&self, fd: &mut dyn Write) -> Result<(), MigratableError> { - fd.write_all(Self::as_bytes(self)) + fd.write_all(Self::as_slice(self)) .map_err(MigratableError::MigrateSocket) } } -impl AsBytes for Request {} +unsafe impl ByteValued for Request {} #[repr(u16)] #[derive(Copy, Clone, PartialEq)] @@ -141,7 +128,7 @@ impl Default for Status { } #[repr(C)] -#[derive(Default)] +#[derive(Default, Copy, Clone)] pub struct Response { status: Status, padding: [u8; 6], @@ -171,19 +158,19 @@ impl Response { pub fn read_from(fd: &mut dyn Read) -> Result { let mut response = Response::default(); - fd.read_exact(Self::as_mut_bytes(&mut response)) + fd.read_exact(Self::as_mut_slice(&mut response)) .map_err(MigratableError::MigrateSocket)?; Ok(response) } pub fn write_to(&self, fd: &mut dyn Write) -> Result<(), MigratableError> { - fd.write_all(Self::as_bytes(self)) + fd.write_all(Self::as_slice(self)) .map_err(MigratableError::MigrateSocket) } } -impl AsBytes for Response {} +unsafe impl ByteValued for Response {} #[repr(C)] pub struct MemoryRange {