mirror of
https://github.com/rosenpass/rosenpass.git
synced 2026-02-04 19:11:52 -08:00
chore: Split utils for zerocopy in protocol into own file
This commit is contained in:
@@ -81,6 +81,7 @@ pub mod basic_types;
|
||||
pub mod constants;
|
||||
pub mod testutils;
|
||||
pub mod timing;
|
||||
pub mod zerocopy;
|
||||
|
||||
#[allow(clippy::module_inception)]
|
||||
mod protocol;
|
||||
|
||||
@@ -45,6 +45,7 @@ use super::constants::{
|
||||
RETRANSMIT_DELAY_GROWTH, RETRANSMIT_DELAY_JITTER,
|
||||
};
|
||||
use super::timing::{has_happened, Timing, BCE, UNENDING};
|
||||
use super::zerocopy::{truncating_cast_into, truncating_cast_into_nomut};
|
||||
|
||||
#[cfg(feature = "trace_bench")]
|
||||
use rosenpass_util::trace_bench::Trace as _;
|
||||
@@ -3949,17 +3950,3 @@ impl CryptoServer {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Used to parse a network message using [zerocopy]
|
||||
pub fn truncating_cast_into<T: FromBytes>(
|
||||
buf: &mut [u8],
|
||||
) -> Result<Ref<&mut [u8], T>, RosenpassError> {
|
||||
Ref::new(&mut buf[..size_of::<T>()]).ok_or(RosenpassError::BufferSizeMismatch)
|
||||
}
|
||||
|
||||
/// Used to parse a network message using [zerocopy], mutably
|
||||
pub fn truncating_cast_into_nomut<T: FromBytes>(
|
||||
buf: &[u8],
|
||||
) -> Result<Ref<&[u8], T>, RosenpassError> {
|
||||
Ref::new(&buf[..size_of::<T>()]).ok_or(RosenpassError::BufferSizeMismatch)
|
||||
}
|
||||
|
||||
@@ -14,8 +14,9 @@ use crate::msgs::{EmptyData, Envelope, InitConf, InitHello, MsgType, RespHello,
|
||||
use super::{
|
||||
basic_types::{MsgBuf, SPk, SSk, SymKey},
|
||||
constants::REKEY_AFTER_TIME_RESPONDER,
|
||||
truncating_cast_into, truncating_cast_into_nomut, CryptoServer, HandleMsgResult,
|
||||
HostIdentification, KnownInitConfResponsePtr, PeerPtr, PollResult, ProtocolVersion,
|
||||
zerocopy::{truncating_cast_into, truncating_cast_into_nomut},
|
||||
CryptoServer, HandleMsgResult, HostIdentification, KnownInitConfResponsePtr, PeerPtr,
|
||||
PollResult, ProtocolVersion,
|
||||
};
|
||||
|
||||
struct VecHostIdentifier(Vec<u8>);
|
||||
|
||||
21
rosenpass/src/protocol/zerocopy.rs
Normal file
21
rosenpass/src/protocol/zerocopy.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
//! Helpers for working with the zerocopy crate
|
||||
|
||||
use std::mem::size_of;
|
||||
|
||||
use zerocopy::{FromBytes, Ref};
|
||||
|
||||
use crate::RosenpassError;
|
||||
|
||||
/// Used to parse a network message using [zerocopy]
|
||||
pub fn truncating_cast_into<T: FromBytes>(
|
||||
buf: &mut [u8],
|
||||
) -> Result<Ref<&mut [u8], T>, RosenpassError> {
|
||||
Ref::new(&mut buf[..size_of::<T>()]).ok_or(RosenpassError::BufferSizeMismatch)
|
||||
}
|
||||
|
||||
/// Used to parse a network message using [zerocopy], mutably
|
||||
pub fn truncating_cast_into_nomut<T: FromBytes>(
|
||||
buf: &[u8],
|
||||
) -> Result<Ref<&[u8], T>, RosenpassError> {
|
||||
Ref::new(&buf[..size_of::<T>()]).ok_or(RosenpassError::BufferSizeMismatch)
|
||||
}
|
||||
Reference in New Issue
Block a user