[][src]Struct xaynet::message::MessageBuffer

pub struct MessageBuffer<T> { /* fields omitted */ }

A wrapper around a buffer that contains a Message.

It provides getters and setters to access the different fields of the message safely.

Examples

Reading a sum message

use xaynet::message::{Tag, Flags, MessageBuffer};
use std::convert::TryFrom;
let mut bytes = vec![
    0x01, // tag = 1
    0x00, // flags = 0
    0x00, 0x00, // reserved bytes, which are ignored
];
bytes.extend(vec![0xaa; 32]); // coordinator public key
bytes.extend(vec![0xbb; 32]); // participant public key
// Payload: a sum message contains a signature and an ephemeral public key
bytes.extend(vec![0x11; 32]); // signature
bytes.extend(vec![0x22; 32]); // public key

let buffer = MessageBuffer::new(&bytes).unwrap();
assert!(!buffer.has_certificate());
assert_eq!(Tag::try_from(buffer.tag()).unwrap(), Tag::Sum);
assert_eq!(buffer.flags(), Flags::empty());
assert!(buffer.certificate().is_none());
assert_eq!(buffer.coordinator_pk(), vec![0xaa; 32].as_slice());
assert_eq!(buffer.participant_pk(), vec![0xbb; 32].as_slice());
assert_eq!(buffer.payload(), [vec![0x11; 32], vec![0x22; 32]].concat().as_slice());

Writing a sum message

use xaynet::message::{Tag, Flags, MessageBuffer};
use std::convert::TryFrom;
let mut expected = vec![
    0x01, // tag = 1
    0x00, // flags = 0
    0x00, 0x00, // reserved bytes, which are ignored
];
expected.extend(vec![0xaa; 32]); // coordinator public key
expected.extend(vec![0xbb; 32]); // participant public key
// Payload: a sum message contains a signature and an ephemeral public key
expected.extend(vec![0x11; 32]); // signature
expected.extend(vec![0x22; 32]); // public key

let mut bytes = vec![0; expected.len()];
let mut buffer = MessageBuffer::new_unchecked(&mut bytes);
buffer.set_tag(Tag::Sum.into());
buffer.set_flags(Flags::empty());
buffer
    .coordinator_pk_mut()
    .copy_from_slice(vec![0xaa; 32].as_slice());
buffer
    .participant_pk_mut()
    .copy_from_slice(vec![0xbb; 32].as_slice());
buffer
    .payload_mut()
    .copy_from_slice([vec![0x11; 32], vec![0x22; 32]].concat().as_slice());
assert_eq!(expected, bytes);

Implementations

impl<T: AsRef<[u8]>> MessageBuffer<T>[src]

pub fn new(bytes: T) -> Result<Self, DecodeError>[src]

Performs bound checks for the various message fields on bytes and returns a new MessageBuffer.

Errors

Fails if the bytes are smaller than a minimal-sized message buffer.

pub fn new_unchecked(bytes: T) -> Self[src]

Returns a MessageBuffer without performing any bound checks.

This means accessing the various fields may panic if the data is invalid.

pub fn check_buffer_length(&self) -> Result<(), DecodeError>[src]

Performs bound checks to ensure the fields can be accessed without panicking.

pub fn has_certificate(&self) -> bool[src]

Checks whether this header contains a certificate.

Panics

Accessing the field may panic if the buffer has not been checked before.

pub fn tag(&self) -> u8[src]

Gets the tag field.

Panics

Accessing the field may panic if the buffer has not been checked before.

pub fn flags(&self) -> Flags[src]

Gets the flags field.

Panics

Accessing the field may panic if the buffer has not been checked before.

impl<'a, T: AsRef<[u8]> + ?Sized> MessageBuffer<&'a T>[src]

pub fn certificate(&self) -> Option<LengthValueBuffer<&'a [u8]>>[src]

Gets a slice to the certificate.

Errors

If the header doesn't contain any certificate, None is returned.

Panics

Accessing the field may panic if the buffer has not been checked before.

pub fn coordinator_pk(&self) -> &'a [u8][src]

Gets the coordinator public key field.

Panics

Accessing the field may panic if the buffer has not been checked before.

pub fn participant_pk(&self) -> &'a [u8][src]

Gets the participant public key field.

Panics

Accessing the field may panic if the buffer has not been checked before.

pub fn payload(&self) -> &'a [u8][src]

Gets the rest of the message.

Panics

Accessing the field may panic if the buffer has not been checked before.

impl<T: AsMut<[u8]> + AsRef<[u8]>> MessageBuffer<T>[src]

pub fn set_tag(&mut self, value: u8)[src]

Sets the tag field.

Panics

Accessing the field may panic if the buffer has not been checked before.

pub fn set_flags(&mut self, value: Flags)[src]

Sets the flags field.

Panics

Accessing the field may panic if the buffer has not been checked before.

pub fn certificate_mut(&mut self) -> Option<LengthValueBuffer<&mut [u8]>>[src]

Gets a mutable reference to the certificate field.

Panics

Accessing the field may panic if the buffer has not been checked before.

pub fn coordinator_pk_mut(&mut self) -> &mut [u8][src]

Gets a mutable reference to the coordinator public key field.

Panics

Accessing the field may panic if the buffer has not been checked before.

pub fn participant_pk_mut(&mut self) -> &mut [u8][src]

Gets a mutable reference to the participant public key field.

Panics

Accessing the field may panic if the buffer has not been checked before.

pub fn payload_mut(&mut self) -> &mut [u8][src]

Gets a mutable reference to the rest of the message.

Panics

Accessing the field may panic if the buffer has not been checked before.

Auto Trait Implementations

impl<T> RefUnwindSafe for MessageBuffer<T> where
    T: RefUnwindSafe

impl<T> Send for MessageBuffer<T> where
    T: Send

impl<T> Sync for MessageBuffer<T> where
    T: Sync

impl<T> Unpin for MessageBuffer<T> where
    T: Unpin

impl<T> UnwindSafe for MessageBuffer<T> where
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> Instrument for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>, 

impl<T> WithSubscriber for T[src]