rust: Improve code formatting

Do some formatting that are in experimental stage but improve the code
readability.

Use rustfmt with a nightly toolchain and enable:

format_code_in_doc_comments = true
reorder_impl_items = true
comment_width = 100
wrap_comments = true
normalize_comments = true

(see .rustfmt.toml)

Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
This commit is contained in:
Steffen Eiden
2024-05-24 13:18:52 +02:00
parent dea5f80215
commit 503e241db1
16 changed files with 78 additions and 71 deletions
+4 -4
View File
@@ -16,15 +16,15 @@ use zerocopy::{AsBytes, BigEndian, FromBytes, FromZeroes, U64};
pub trait Flags<T>: From<T> + for<'a> From<&'a T> {
/// Set the specified bit to one.
/// # Panics
///Panics if bit is >= 64
/// Panics if bit is >= 64
fn set_bit(&mut self, bit: u8);
/// Set the specified bit to zero.
/// # Panics
///Panics if bit is >= 64
/// Panics if bit is >= 64
fn unset_bit(&mut self, bit: u8);
/// Test if the specified bit is set.
/// # Panics
///Panics if bit is >= 64
/// Panics if bit is >= 64
fn is_set(&self, bit: u8) -> bool;
}
@@ -361,7 +361,7 @@ pub fn parse_hex(hex_str: &str) -> Vec<u8> {
pub fn pv_guest_bit_set() -> bool {
#[cfg(not(target_arch = "s390x"))]
return false;
//s390 branch
// s390 branch
let v = std::fs::read("/sys/firmware/uv/prot_virt_guest").unwrap_or_else(|_| vec![0]);
let v: u8 = String::from_utf8_lossy(&v[..1]).parse().unwrap_or(0);
v == 1
+4 -5
View File
@@ -162,7 +162,8 @@ pub enum UvcSuccess {
RC_MORE_DATA = UvDevice::RC_MORE_DATA,
}
/// The UvDevice is a (virtual) device on s390 machines to send Ultravisor commands(UVCs) from userspace.
/// The UvDevice is a (virtual) device on s390 machines to send Ultravisor commands(UVCs) from
/// userspace.
///
/// On s390 machines with Ultravisor enabled (Secure Execution guest & hosts) the device at
/// `/dev/uv` will accept ioctls.
@@ -184,16 +185,14 @@ pub enum UvcSuccess {
/// # Ok(())
/// # }
/// // do something with the result
///
///
/// ```
#[derive(Debug)]
pub struct UvDevice(File);
impl UvDevice {
const RC_SUCCESS: u16 = 0x0001;
const RC_MORE_DATA: u16 = 0x0100;
const PATH: &'static str = "/dev/uv";
const RC_MORE_DATA: u16 = 0x0100;
const RC_SUCCESS: u16 = 0x0001;
/// Open the uvdevice located at `/dev/uv`
///
+5 -6
View File
@@ -30,7 +30,6 @@ pub const UVIO_IOCTL_LOCK_SECRETS_NR: u8 = 4;
/// Ultravisor.
///
/// `flags` is currently unused and to be set zero
///
#[repr(C)]
#[derive(Debug)]
pub struct uvio_ioctl_cb {
@@ -89,11 +88,11 @@ pub const UVIO_ATT_UID_LEN: usize = 0x10;
#[repr(C)]
#[derive(Debug, AsBytes, FromZeroes, FromBytes)]
pub struct uvio_attest {
pub arcb_addr: u64, //in
pub meas_addr: u64, //out
pub add_data_addr: u64, //out
pub user_data: [u8; UVIO_ATT_USER_DATA_LEN], //in
pub config_uid: [u8; UVIO_ATT_UID_LEN], //out
pub arcb_addr: u64, // in
pub meas_addr: u64, // out
pub add_data_addr: u64, // out
pub user_data: [u8; UVIO_ATT_USER_DATA_LEN], // in
pub config_uid: [u8; UVIO_ATT_UID_LEN], // out
pub arcb_len: u32,
pub meas_len: u32,
pub add_data_len: u32,
-1
View File
@@ -24,7 +24,6 @@ use zerocopy::{AsBytes, FromZeroes};
///
/// Note that bit 0 is always zero for `supp_uv_cmds`
/// as there is no corresponding Info UV-call.
///
#[derive(Debug)]
pub struct UvDeviceInfo {
supp_uvio_cmds: Lsb0Flags64,
+21 -16
View File
@@ -37,7 +37,7 @@ impl Serialize for SecretId {
where
S: Serializer,
{
//calls Display at one point
// calls Display at one point
ser.serialize_str(&self.to_string())
}
}
@@ -151,8 +151,8 @@ pub struct SecretList {
}
impl<'a> IntoIterator for &'a SecretList {
type Item = &'a SecretEntry;
type IntoIter = Iter<'a, SecretEntry>;
type Item = &'a SecretEntry;
fn into_iter(self) -> Self::IntoIter {
self.iter()
@@ -160,8 +160,8 @@ impl<'a> IntoIterator for &'a SecretList {
}
impl IntoIterator for SecretList {
type Item = SecretEntry;
type IntoIter = IntoIter<Self::Item>;
type Item = SecretEntry;
fn into_iter(self) -> Self::IntoIter {
self.secrets.into_iter()
@@ -233,11 +233,11 @@ impl SecretList {
let num_s = r.read_u16::<BigEndian>()?;
let total_num_secrets = r.read_u16::<BigEndian>()? as usize;
let mut v: Vec<SecretEntry> = Vec::with_capacity(num_s as usize);
r.seek(std::io::SeekFrom::Current(12))?; //skip reserved bytes
r.seek(std::io::SeekFrom::Current(12))?; // skip reserved bytes
let mut buf = [0u8; SecretEntry::STRUCT_SIZE];
for _ in 0..num_s {
r.read_exact(&mut buf)?;
//cannot fail. buffer has the same size as the secret entry
// cannot fail. buffer has the same size as the secret entry
let secr = SecretEntry::read_from(buf.as_slice()).unwrap();
v.push(secr);
}
@@ -250,6 +250,7 @@ impl SecretList {
impl TryFrom<ListCmd> for SecretList {
type Error = Error;
fn try_from(mut list: ListCmd) -> Result<SecretList> {
SecretList::decode(&mut Cursor::new(list.data().unwrap())).map_err(Error::InvSecretList)
}
@@ -292,11 +293,11 @@ pub enum ListableSecretType {
}
impl ListableSecretType {
const RESERVED_0: u16 = 0x0000;
/// UV type id for a null secret
pub const NULL: u16 = 0x0001;
/// UV type id for an association secret
pub const ASSOCIATION: u16 = 0x0002;
/// UV type id for a null secret
pub const NULL: u16 = 0x0001;
const RESERVED_0: u16 = 0x0000;
}
impl Display for ListableSecretType {
@@ -338,9 +339,11 @@ where
impl<'de> serde::de::Visitor<'de> for FieldVisitor {
type Value = [u8; SecretId::ID_SIZE];
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("a `32 bytes long hexstring` prepended with 0x")
}
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
@@ -370,8 +373,8 @@ mod test {
#[test]
fn dump_secret_entry() {
const EXP: &[u8] = &[
0x00, 0x01, 0x00, 0x02, //idx + type
0x00, 0x00, 0x00, 0x20, //len
0x00, 0x01, 0x00, 0x02, // idx + type
0x00, 0x00, 0x00, 0x20, // len
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // reserved
// id
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -394,10 +397,11 @@ mod test {
let buf = [
0x00u8, 0x01, // num secr stored
0x01, 0x12, // total num secrets
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //reserved
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, // reserved
// secret
0x00, 0x01, 0x00, 0x02, //idx + type
0x00, 0x00, 0x00, 0x20, //len
0x00, 0x01, 0x00, 0x02, // idx + type
0x00, 0x00, 0x00, 0x20, // len
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // reserved
// id
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -426,10 +430,11 @@ mod test {
const EXP: &[u8] = &[
0x00, 0x01, // num secr stored
0x01, 0x12, // total num secrets
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //reserved
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, // reserved
// secret
0x00, 0x01, 0x00, 0x02, //idx + type
0x00, 0x00, 0x00, 0x20, //len
0x00, 0x01, 0x00, 0x02, // idx + type
0x00, 0x00, 0x00, 0x20, // len
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // reserved
// id
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+9 -1
View File
@@ -38,6 +38,7 @@ impl IoctlCtx {
self.exp_cmd = cmd;
self
}
pub fn set_mdfy<F>(&mut self, mdfy: F) -> &mut Self
where
F: FnMut(&mut ffi::uvio_ioctl_cb) -> c_int + 'static + Send + Sync,
@@ -45,6 +46,7 @@ impl IoctlCtx {
self.modify = Box::new(mdfy);
self
}
pub fn reset(&mut self) -> bool {
let old = self.called;
self.called = false;
@@ -86,6 +88,7 @@ impl ffi::uvio_ioctl_cb {
);
self
}
fn size_eq(&self, exp: u32) -> &Self {
assert_eq!(
self.argument_len, exp,
@@ -94,10 +97,12 @@ impl ffi::uvio_ioctl_cb {
);
self
}
fn set_rc(&mut self, rc: u16) -> &mut Self {
self.uv_rc = rc;
self
}
fn set_rrc(&mut self, rrc: u16) -> &mut Self {
self.uv_rrc = rrc;
self
@@ -112,9 +117,11 @@ impl UvCmd for TestCmd {
fn cmd(&self) -> u64 {
TEST_CMD
}
fn rc_fmt(&self, _rc: u16, _rrc: u16) -> Option<&'static str> {
None
}
fn data(&mut self) -> Option<&mut [u8]> {
match &mut self.0 {
None => None,
@@ -124,7 +131,8 @@ impl UvCmd for TestCmd {
}
impl UvDevice {
///use some random fd for `uvdevice` its OK, as the ioctl is mocked and never touches the passed file
/// use some random fd for `uvdevice` its OK, as the ioctl is mocked and never touches the
/// passed file
fn test_dev() -> Self {
UvDevice(unsafe { File::from_raw_fd(17) })
}
+1 -2
View File
@@ -23,8 +23,7 @@ use zerocopy::{AsBytes, U16};
/// # ;
/// # assert!(AddSecretMagic::starts_with_magic(magic));
/// # }
///```
///
/// ```
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, AsBytes)]
pub struct AddSecretMagic {