Skip to main content

Frame

Struct Frame 

Source
pub struct Frame<T> { /* private fields */ }
Expand description

An immutable view into a SOFH-enclosed message, complete with its encoding type tag and message payload.

§Type parameters

Frame is generic over T, which must implement AsRef<[u8]> and is the payload type. Common choices for T are &'a [u8] for some lifetime 'a, bytes::Bytes, and bytes::BytesMut.

Implementations§

Source§

impl<T> Frame<T>
where T: AsRef<[u8]>,

Source

pub fn new(encoding_type: u16, payload: T) -> Self

Creates a new Frame with the given encoding_type and payload.

§Panics

This function panics if message.len() > u32::MAX as usize - 6, which would cause encoding issues.

§Examples
use minisofh::Frame;

let frame = Frame::new(0xF500, b"{}" as &[u8]);
assert_eq!(frame.payload().len(), 2);
Source

pub fn encoding_type(&self) -> u16

Returns the 16-bits encoding type of self. You may want to convert this value to an EncodingType, which allows for nice pattern matching.

§Examples
use minisofh::{EncodingType, Frame};

let frame = Frame::new(0xF500, &[] as &[u8]);
let encoding_type = EncodingType::new(frame.encoding_type());
assert_eq!(encoding_type, Some(EncodingType::Json));
Source

pub fn payload(&self) -> &T

Returns an immutable reference to the payload of self, i.e. without its header.

§Examples
use minisofh::Frame;

let frame = Frame::new(0x0, &[42u8] as &[u8]);
assert_eq!(frame.payload(), &[42]);
Source

pub fn payload_mut(&mut self) -> &mut T

Returns a mutable reference to the payload of self.

Source

pub fn serialize<W>(&self, writer: &mut W) -> Result<usize>
where W: Write,

Serializes self to a Writer. This requires copying and thus is potentially expensive.

§Examples
use minisofh::Frame;

// Message_Length:
//            ~~~~~~~~~~~~~
// Encoding_Type:           ~~~~~~~~~
// Message:                           ~~
let bytes = &[0u8, 0, 0, 7, 0x0, 0x0, 42] as &[u8];
let frame = Frame::<&[u8]>::deserialize(bytes).unwrap();
let buffer = &mut Vec::new();
frame.serialize(buffer).unwrap();
assert_eq!(&buffer[..], bytes);
Source§

impl<'a> Frame<&'a [u8]>

Source

pub fn deserialize(data: &'a [u8]) -> Result<Self, Error>

Tries to deserialize a Frame from data. Returns an Err if invalid. Zero-copy.

This function ignores trailing bytes that are not part of the message.

§Examples
use minisofh::Frame;

// Message_Length:                        ~~~~~~~~~~~
// Encoding_Type:                                     ~~~~~~~~~
// Message:                                                     ~~
let frame = Frame::<&[u8]>::deserialize(&[0, 0, 0, 7, 0x0, 0x0, 42]).unwrap();
assert_eq!(frame.payload(), &[42]);

Trait Implementations§

Source§

impl<T: Clone> Clone for Frame<T>

Source§

fn clone(&self) -> Frame<T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for Frame<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Encoder<Frame<T>> for TokioCodec
where T: AsRef<[u8]>,

Available on crate feature utils-tokio-codec only.
Source§

type Error = Error

The type of encoding errors. Read more
Source§

fn encode( &mut self, frame: Frame<T>, dst: &mut BytesMut, ) -> Result<(), Self::Error>

Encodes a frame into the buffer provided. Read more
Source§

impl<T: PartialEq> PartialEq for Frame<T>

Source§

fn eq(&self, other: &Frame<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Copy> Copy for Frame<T>

Source§

impl<T: Eq> Eq for Frame<T>

Source§

impl<T> StructuralPartialEq for Frame<T>

Auto Trait Implementations§

§

impl<T> Freeze for Frame<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Frame<T>
where T: RefUnwindSafe,

§

impl<T> Send for Frame<T>
where T: Send,

§

impl<T> Sync for Frame<T>
where T: Sync,

§

impl<T> Unpin for Frame<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Frame<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for Frame<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.