pub struct Sv2Frame<T, B> { /* private fields */ }
Expand description
Abstraction for a Sv2 frame.
Represents a regular Sv2 frame, used for all communication outside of the Noise protocol
handshake process. It contains a Header
and a message payload, which can be serialized for
encoding and transmission or decoded and deserialized upon receipt.
Implementations§
Source§impl<T, B> Sv2Frame<T, B>
impl<T, B> Sv2Frame<T, B>
Sourcepub fn serialize(self, dst: &mut [u8]) -> Result<(), Error>
pub fn serialize(self, dst: &mut [u8]) -> Result<(), Error>
Writes the serialized Sv2Frame
into dst
.
This operation when called on an already serialized frame is very cheap. When called on a non serialized frame, it is not so cheap (because it serializes it).
Sourcepub fn payload(&mut self) -> &mut [u8] ⓘ
pub fn payload(&mut self) -> &mut [u8] ⓘ
Returns the message payload.
self
can be either serialized (self.serialized
is Some()
) or deserialized
(self.serialized
is None
, self.payload
is Some()
).
This function is only intended as a fast way to get a reference to an already serialized payload. If the frame has not yet been serialized, this function should never be used (it will panic).
Sourcepub fn get_header(&self) -> Option<Header>
pub fn get_header(&self) -> Option<Header>
Sv2Frame
always returns Some(self.header)
.
Sourcepub fn from_bytes(bytes: B) -> Result<Sv2Frame<T, B>, isize>
pub fn from_bytes(bytes: B) -> Result<Sv2Frame<T, B>, isize>
Tries to build a Sv2Frame
from raw bytes.
It assumes the raw bytes represent a serialized Sv2Frame
frame (Self.serialized
).
Returns a Sv2Frame
on success, or the number of the bytes needed to complete the frame
as an error. Self.serialized
is Some
, but nothing is assumed or checked about the
correctness of the payload.
Sourcepub fn from_bytes_unchecked(bytes: B) -> Sv2Frame<T, B>
pub fn from_bytes_unchecked(bytes: B) -> Sv2Frame<T, B>
Constructs an Sv2Frame
from raw bytes without performing byte content validation.
Sourcepub fn size_hint(bytes: &[u8]) -> isize
pub fn size_hint(bytes: &[u8]) -> isize
After parsing bytes
into a Header
, this function helps to determine if the
msg_length
field is correctly representing the size of the frame.
- Returns
0
if the byte slice is of the expected size according to the header. - Returns a negative value if the byte slice is shorter than expected; this value represents how many bytes are missing.
- Returns a positive value if the byte slice is longer than expected; this value indicates the surplus of bytes beyond the expected size.
Sourcepub fn encoded_length(&self) -> usize
pub fn encoded_length(&self) -> usize
If Sv2Frame
is serialized, returns the length of self.serialized
, otherwise, returns
the length of self.payload
.