Struct mles_utils::Msg

source ·
pub struct Msg { /* private fields */ }
Expand description

Msg structure

This structure defines the Mles interface value triplet (uid, channel, message). It is eventually serialized and deserialized by CBOR.

Implementations§

source§

impl Msg

source

pub fn new(uid: String, channel: String, message: Vec<u8>) -> Msg

Create a new Msg object with value triplet.

Example
use mles_utils::Msg;

let msg = Msg::new("My uid".to_string(), "My channel".to_string(), Vec::new());
source

pub fn set_uid(self, uid: String) -> Msg

Set uid for Msg object.

Example
use mles_utils::Msg;

let mut msg = Msg::new("My uid".to_string(), "My channel".to_string(), Vec::new());
let msg = msg.set_uid("New uid".to_string());

assert_eq!("New uid".to_string(), *msg.get_uid());
source

pub fn set_channel(self, channel: String) -> Msg

Set channel for Msg object.

Example
use mles_utils::Msg;

let mut msg = Msg::new("My uid".to_string(), "My channel".to_string(), Vec::new());
let msg = msg.set_channel("New channel".to_string());

assert_eq!("New channel".to_string(), *msg.get_channel());
source

pub fn set_message(self, message: Vec<u8>) -> Msg

Set message for Msg object.

Example
use mles_utils::Msg;

let mut msg = Msg::new("My uid".to_string(), "My channel".to_string(), Vec::new());
let new_message: Vec<u8> = "New message".to_string().into_bytes();
let msg = msg.set_message(new_message);
source

pub fn get_uid(&self) -> &String

Get uid for Msg object. See example for set uid.

source

pub fn get_channel(&self) -> &String

Get channel for Msg object. See example for set channel.

source

pub fn get_message(&self) -> &Vec<u8>

Get message for Msg object.

Example
use mles_utils::Msg;

let mut msg = Msg::new("My uid".to_string(), "My channel".to_string(), Vec::new());
let msg: &Vec<u8> = msg.get_message();
source

pub fn get_message_len(&self) -> usize

Get message len for Msg object.

Example
use mles_utils::Msg;

let mut msg = Msg::new("My uid".to_string(), "My channel".to_string(), Vec::new());
let msg_len: usize = msg.get_message_len();
source

pub fn get_mut_message(&mut self) -> &mut Vec<u8>

Get mutable message reference for Msg object.

Example
use mles_utils::Msg;

let mut msg = Msg::new("My uid".to_string(), "My channel".to_string(), "My
message".to_string().into_bytes());
let message = msg.get_mut_message();
message.extend_from_slice(&" is mutable".to_string().into_bytes());
source

pub fn encode(&self) -> Vec<u8>

Encode Msg object to CBOR.

Errors

If message cannot be encoded, an empty vector is returned.

Example
use mles_utils::Msg;

let msg = Msg::new("My uid".to_string(), "My channel".to_string(), Vec::new());
let encoded_msg: Vec<u8> = msg.encode();
source

pub fn decode(slice: &[u8]) -> Msg

Decode CBOR byte string to Msg object.

Errors

If message cannot be decoded, a Msg structure with empty items is returned.

Example
use mles_utils::Msg;

let msg = Msg::new("My uid".to_string(), "My channel".to_string(), Vec::new());
let encoded_msg: Vec<u8> = msg.encode();
let decoded_msg: Msg = Msg::decode(&encoded_msg);

Trait Implementations§

source§

impl Clone for Msg

source§

fn clone(&self) -> Msg

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for Msg

source§

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

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

impl<'de> Deserialize<'de> for Msg

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Serialize for Msg

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl RefUnwindSafe for Msg

§

impl Send for Msg

§

impl Sync for Msg

§

impl Unpin for Msg

§

impl UnwindSafe for Msg

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> 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,

§

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>,

§

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>,

§

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.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,