From 7652b682d12f68f9dccc6012bf343def1118a5b3 Mon Sep 17 00:00:00 2001 From: leemeo3 <117142323+leemeo3@users.noreply.github.com> Date: Fri, 12 Jun 2026 12:01:54 +0900 Subject: [PATCH] Apply zerocopy migration formatting --- .../src/api/boilerplate/byte_slice_ext.rs | 8 +-- rosenpass/src/msgs.rs | 58 ++++++++++--------- rosenpass/src/protocol/protocol.rs | 9 ++- wireguard-broker/src/api/msgs.rs | 4 +- 4 files changed, 42 insertions(+), 37 deletions(-) diff --git a/rosenpass/src/api/boilerplate/byte_slice_ext.rs b/rosenpass/src/api/boilerplate/byte_slice_ext.rs index 7bfd20d1..89655489 100644 --- a/rosenpass/src/api/boilerplate/byte_slice_ext.rs +++ b/rosenpass/src/api/boilerplate/byte_slice_ext.rs @@ -199,9 +199,7 @@ pub trait ByteSliceRefExt: ByteSlice { } /// Shorthand for the typed use of [ZerocopySliceExt::zk_parse_prefix]. - fn supply_keypair_response_from_prefix( - self, - ) -> anyhow::Result> + fn supply_keypair_response_from_prefix(self) -> anyhow::Result> where Self: SplitByteSlice, { @@ -209,9 +207,7 @@ pub trait ByteSliceRefExt: ByteSlice { } /// Shorthand for the typed use of [ZerocopySliceExt::zk_parse_suffix]. - fn supply_keypair_response_from_suffix( - self, - ) -> anyhow::Result> + fn supply_keypair_response_from_suffix(self) -> anyhow::Result> where Self: SplitByteSlice, { diff --git a/rosenpass/src/msgs.rs b/rosenpass/src/msgs.rs index fc40fc90..71e3f82e 100644 --- a/rosenpass/src/msgs.rs +++ b/rosenpass/src/msgs.rs @@ -7,9 +7,8 @@ //! to the concept of lenses in function programming; more on that here: //! [https://sinusoid.es/misc/lager/lenses.pdf](https://sinusoid.es/misc/lager/lenses.pdf) //! To achieve this we utilize the zerocopy library. -//! use std::mem::size_of; -use zerocopy::{IntoBytes, FromBytes, KnownLayout, Immutable}; +use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout}; use super::RosenpassError; use rosenpass_cipher_traits::primitives::{Aead as _, Kem}; @@ -50,9 +49,9 @@ pub type MsgEnvelopeCookie = [u8; COOKIE_SIZE]; /// # Examples /// /// ``` -/// use rosenpass::msgs::{Envelope, InitHello}; -/// use zerocopy::{IntoBytes, FromBytes, Ref, FromZeros, KnownLayout, Immutable}; /// use memoffset::offset_of; +/// use rosenpass::msgs::{Envelope, InitHello}; +/// use zerocopy::{FromBytes, FromZeros, Immutable, IntoBytes, KnownLayout, Ref}; /// /// // Zero-initialization /// let mut ih = Envelope::::new_zeroed(); @@ -62,7 +61,7 @@ pub type MsgEnvelopeCookie = [u8; COOKIE_SIZE]; /// /// // Edit as binary /// ih.as_bytes_mut()[offset_of!(Envelope, msg_type)] = 23; -/// assert_eq!(ih.msg_type, 23);; +/// assert_eq!(ih.msg_type, 23); /// /// // Conversion to bytes /// let mut ih2 = ih.as_bytes().to_owned(); @@ -105,9 +104,9 @@ pub struct Envelope { /// [Envelope] contains some extra examples on how to use structures from the [::zerocopy] crate. /// /// ``` -/// use rosenpass::msgs::{Envelope, InitHello}; -/// use zerocopy::{IntoBytes, FromBytes, Ref, FromZeros, KnownLayout, Immutable}; /// use memoffset::span_of; +/// use rosenpass::msgs::{Envelope, InitHello}; +/// use zerocopy::{FromBytes, FromZeros, Immutable, IntoBytes, KnownLayout, Ref}; /// /// // Zero initialization /// let mut ih = Envelope::::new_zeroed(); @@ -117,13 +116,13 @@ pub struct Envelope { /// /// // Set value on byte representation /// ih[span_of!(Envelope, payload)][span_of!(InitHello, sidi)] -/// .copy_from_slice(&[1,2,3,4]); +/// .copy_from_slice(&[1, 2, 3, 4]); /// /// // Conversion from bytes /// let ih = Ref::<&mut [u8], Envelope>::new(ih).unwrap(); /// /// // Check that write above on byte representation was effective -/// assert_eq!(ih.payload.sidi, [1,2,3,4]); +/// assert_eq!(ih.payload.sidi, [1, 2, 3, 4]); /// ``` #[repr(packed)] #[derive(IntoBytes, FromBytes, KnownLayout, Immutable)] @@ -154,9 +153,9 @@ pub struct InitHello { /// [Envelope] contains some extra examples on how to use structures from the [::zerocopy] crate. /// /// ``` -/// use rosenpass::msgs::{Envelope, RespHello}; -/// use zerocopy::{IntoBytes, FromBytes, Ref, FromZeros, KnownLayout, Immutable}; /// use memoffset::span_of; +/// use rosenpass::msgs::{Envelope, RespHello}; +/// use zerocopy::{FromBytes, FromZeros, Immutable, IntoBytes, KnownLayout, Ref}; /// /// // Zero initialization /// let mut ih = Envelope::::new_zeroed(); @@ -166,13 +165,13 @@ pub struct InitHello { /// /// // Set value on byte representation /// ih[span_of!(Envelope, payload)][span_of!(RespHello, sidi)] -/// .copy_from_slice(&[1,2,3,4]); +/// .copy_from_slice(&[1, 2, 3, 4]); /// /// // Conversion from bytes /// let ih = Ref::<&mut [u8], Envelope>::new(ih).unwrap(); /// /// // Check that write above on byte representation was effective -/// assert_eq!(ih.payload.sidi, [1,2,3,4]); +/// assert_eq!(ih.payload.sidi, [1, 2, 3, 4]); /// ``` #[repr(packed)] #[derive(IntoBytes, FromBytes, KnownLayout, Immutable)] @@ -205,9 +204,9 @@ pub struct RespHello { /// [Envelope] contains some extra examples on how to use structures from the [::zerocopy] crate. /// /// ``` -/// use rosenpass::msgs::{Envelope, InitConf}; -/// use zerocopy::{IntoBytes, FromBytes, Ref, FromZeros, KnownLayout, Immutable}; /// use memoffset::span_of; +/// use rosenpass::msgs::{Envelope, InitConf}; +/// use zerocopy::{FromBytes, FromZeros, Immutable, IntoBytes, KnownLayout, Ref}; /// /// // Zero initialization /// let mut ih = Envelope::::new_zeroed(); @@ -217,13 +216,13 @@ pub struct RespHello { /// /// // Set value on byte representation /// ih[span_of!(Envelope, payload)][span_of!(InitConf, sidi)] -/// .copy_from_slice(&[1,2,3,4]); +/// .copy_from_slice(&[1, 2, 3, 4]); /// /// // Conversion from bytes /// let ih = Ref::<&mut [u8], Envelope>::new(ih).unwrap(); /// /// // Check that write above on byte representation was effective -/// assert_eq!(ih.payload.sidi, [1,2,3,4]); +/// assert_eq!(ih.payload.sidi, [1, 2, 3, 4]); /// ``` #[repr(packed)] #[derive(IntoBytes, FromBytes, KnownLayout, Immutable, Debug)] @@ -263,9 +262,9 @@ pub struct InitConf { /// [Envelope] contains some extra examples on how to use structures from the [::zerocopy] crate. /// /// ``` -/// use rosenpass::msgs::{Envelope, EmptyData}; -/// use zerocopy::{IntoBytes, FromBytes, Ref, FromZeros, KnownLayout, Immutable}; /// use memoffset::span_of; +/// use rosenpass::msgs::{EmptyData, Envelope}; +/// use zerocopy::{FromBytes, FromZeros, Immutable, IntoBytes, KnownLayout, Ref}; /// /// // Zero initialization /// let mut ih = Envelope::::new_zeroed(); @@ -275,13 +274,13 @@ pub struct InitConf { /// /// // Set value on byte representation /// ih[span_of!(Envelope, payload)][span_of!(EmptyData, sid)] -/// .copy_from_slice(&[1,2,3,4]); +/// .copy_from_slice(&[1, 2, 3, 4]); /// /// // Conversion from bytes /// let ih = Ref::<&mut [u8], Envelope>::new(ih).unwrap(); /// /// // Check that write above on byte representation was effective -/// assert_eq!(ih.payload.sid, [1,2,3,4]); +/// assert_eq!(ih.payload.sid, [1, 2, 3, 4]); /// ``` #[repr(packed)] #[derive(IntoBytes, FromBytes, KnownLayout, Immutable, Clone, Copy)] @@ -377,21 +376,26 @@ pub struct CookieReply { /// use rosenpass::msgs::MsgType; /// use rosenpass::msgs::MsgType as M; /// -/// let values = [M::InitHello, M::RespHello, M::InitConf, M::EmptyData, M::CookieReply]; +/// let values = [ +/// M::InitHello, +/// M::RespHello, +/// M::InitConf, +/// M::EmptyData, +/// M::CookieReply, +/// ]; /// let values_u8 = values.map(|v| -> u8 { v.into() }); /// /// // Can be converted to and from u8 using [::std::convert::Into] or [::std::convert::From] /// for v in values.iter().copied() { -/// let v_u8 : u8 = v.into(); -/// let v2 : MsgType = v_u8.try_into()?; +/// let v_u8: u8 = v.into(); +/// let v2: MsgType = v_u8.try_into()?; /// assert_eq!(v, v2); /// } /// /// // Converting an unsupported type produces an error -/// let invalid_values = (u8::MIN..=u8::MAX) -/// .filter(|v| !values_u8.contains(v)); +/// let invalid_values = (u8::MIN..=u8::MAX).filter(|v| !values_u8.contains(v)); /// for v in invalid_values { -/// let res : Result = v.try_into(); +/// let res: Result = v.try_into(); /// assert!(res.is_err()); /// } /// diff --git a/rosenpass/src/protocol/protocol.rs b/rosenpass/src/protocol/protocol.rs index 5f559117..1ce3a619 100644 --- a/rosenpass/src/protocol/protocol.rs +++ b/rosenpass/src/protocol/protocol.rs @@ -423,7 +423,9 @@ pub struct KnownResponse, } -impl Debug for KnownResponse { +impl Debug + for KnownResponse +{ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("KnownResponse") .field("received_at", &self.received_at) @@ -508,7 +510,10 @@ impl KnownResponseHasher { /// # Panic & Safety /// /// Panics in case of a problem with this underlying hash function - pub fn hash(&self, msg: &Envelope) -> KnownResponseHash { + pub fn hash( + &self, + msg: &Envelope, + ) -> KnownResponseHash { let data = &msg.as_bytes()[span_of!(Envelope, msg_type..cookie)]; // This function is only used internally and results are not propagated // to outside the peer. Thus, it uses SHAKE256 exclusively. diff --git a/wireguard-broker/src/api/msgs.rs b/wireguard-broker/src/api/msgs.rs index 0000a405..1f6b5465 100644 --- a/wireguard-broker/src/api/msgs.rs +++ b/wireguard-broker/src/api/msgs.rs @@ -3,7 +3,7 @@ use std::str::{Utf8Error, from_utf8}; -use zerocopy::{IntoBytes, FromBytes, KnownLayout, Immutable}; +use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout}; /// The number of bytes reserved for overhead when packaging data. pub const ENVELOPE_OVERHEAD: usize = 1 + 3; @@ -27,7 +27,6 @@ pub struct Envelope { /// Message format for requests to set a pre-shared key. /// # Example -/// #[repr(packed)] #[derive(IntoBytes, FromBytes, KnownLayout, Immutable)] pub struct SetPskRequest { @@ -191,6 +190,7 @@ impl From for SetPskResponseReturnCode { /// let typ: u8 = 0x01; // Usually specifically set or comes out of a message. /// let typ = MsgType::try_from(typ)?; /// let MsgType::SetPsk = typ; // Assert type. +/// /// # Ok::<(), InvalidMessageTypeError>(()) /// ``` #[repr(u8)]