Apply zerocopy migration formatting

This commit is contained in:
leemeo3
2026-06-12 12:29:13 +09:00
parent ce6888febd
commit 7652b682d1
4 changed files with 42 additions and 37 deletions
@@ -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<Ref<Self, SupplyKeypairResponse>>
fn supply_keypair_response_from_prefix(self) -> anyhow::Result<Ref<Self, SupplyKeypairResponse>>
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<Ref<Self, SupplyKeypairResponse>>
fn supply_keypair_response_from_suffix(self) -> anyhow::Result<Ref<Self, SupplyKeypairResponse>>
where
Self: SplitByteSlice,
{
+31 -27
View File
@@ -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::<InitHello>::new_zeroed();
@@ -62,7 +61,7 @@ pub type MsgEnvelopeCookie = [u8; COOKIE_SIZE];
///
/// // Edit as binary
/// ih.as_bytes_mut()[offset_of!(Envelope<InitHello>, 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<M: IntoBytes + FromBytes + KnownLayout + Immutable> {
/// [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::<InitHello>::new_zeroed();
@@ -117,13 +116,13 @@ pub struct Envelope<M: IntoBytes + FromBytes + KnownLayout + Immutable> {
///
/// // Set value on byte representation
/// ih[span_of!(Envelope<InitHello>, 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<InitHello>>::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::<RespHello>::new_zeroed();
@@ -166,13 +165,13 @@ pub struct InitHello {
///
/// // Set value on byte representation
/// ih[span_of!(Envelope<RespHello>, 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<RespHello>>::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::<InitConf>::new_zeroed();
@@ -217,13 +216,13 @@ pub struct RespHello {
///
/// // Set value on byte representation
/// ih[span_of!(Envelope<InitConf>, 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<InitConf>>::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::<EmptyData>::new_zeroed();
@@ -275,13 +274,13 @@ pub struct InitConf {
///
/// // Set value on byte representation
/// ih[span_of!(Envelope<EmptyData>, 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<EmptyData>>::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<MsgType, _> = v.try_into();
/// let res: Result<MsgType, _> = v.try_into();
/// assert!(res.is_err());
/// }
///
+7 -2
View File
@@ -423,7 +423,9 @@ pub struct KnownResponse<ResponseType: IntoBytes + FromBytes + KnownLayout + Imm
pub response: Envelope<ResponseType>,
}
impl<ResponseType: IntoBytes + FromBytes + KnownLayout + Immutable> Debug for KnownResponse<ResponseType> {
impl<ResponseType: IntoBytes + FromBytes + KnownLayout + Immutable> Debug
for KnownResponse<ResponseType>
{
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<Msg: IntoBytes + FromBytes + KnownLayout + Immutable>(&self, msg: &Envelope<Msg>) -> KnownResponseHash {
pub fn hash<Msg: IntoBytes + FromBytes + KnownLayout + Immutable>(
&self,
msg: &Envelope<Msg>,
) -> KnownResponseHash {
let data = &msg.as_bytes()[span_of!(Envelope<Msg>, msg_type..cookie)];
// This function is only used internally and results are not propagated
// to outside the peer. Thus, it uses SHAKE256 exclusively.
+2 -2
View File
@@ -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<M: IntoBytes + FromBytes + KnownLayout + Immutable> {
/// 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<SetPskResult> 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)]