Struct Message

Source
pub struct Message { /* private fields */ }
Expand description

A rustean representation of a BMessage

The Message class is identified by a u32 what value, that can be set during the creation of a message, and by using the set_what() method. In C++, this value is usually generated by using a 4 character literal. In Rust there is the haiku_constant! macro that simulates the behaviour.

The message can be further enriched by adding data. To add data, use the add_data() method. All data is identified by a label. Every type that implements the Flattenable trait may be added. It is possible to add more than one object to each label, creating an array-like construction. You may only add multiple values of the same type. Adding differing types will fail.

In order to read the data, you may use the find_data() method

Further manipulation of the data can be done with the remove_data() and remove_field() methods.

Implementations§

Source§

impl Message

Source

pub fn new(what: u32) -> Self

Create a new message with the signature what

Source

pub fn what(&self) -> u32

Get the current identifier of the message

Source

pub fn set_what(&mut self, what: u32)

Set a new identifier for the message

Source

pub fn add_data<T: Flattenable<T>>( &mut self, name: &str, data: &T, ) -> Result<()>

Add data to the message.

Every type that implements the Flattenable trait may be added. The name parameter identifies the name with which the data will be associated. You may repeatedly reuse this method, to add more values to the name, as long as all the data is of the same type.

This method will return an error of ErrorKind::InvalidInput when you are trying to add data to an existing identifier, with a different type.

Source

pub fn find_data<T: Flattenable<T>>( &self, name: &str, index: usize, ) -> Result<T>

Retrieve an object that is stored in the message

You may retrieve any object that implements the Flattenable interface. There may be more than one value stored, so you can use the index parameter to iterate through the object. The first value is at index 0.

This method will return ErrorKind::NotFound when the name is not in this message, or it is of a different type. Additionally, if the index is out of range, it will return ErrorKind::InvalidInput.

Source

pub fn replace_data<T: Flattenable<T>>( &mut self, name: &str, index: usize, data: &T, ) -> Result<()>

Replace existing data in the message with a new value.

The requirement is that the data of the type exists under the name, at the index.

It returns ErrorKind::NotFound if the name does not exist, or it has a different type. ErrorKind::InvalidInput if the index is out of range, and otherwise any error that the Flattenable trait implementation returns.

Source

pub fn remove_data(&mut self, name: &str, index: usize) -> Result<()>

Remove data for name at index

In case you are removing the only element at name, the whole field will be removed, effectively doing the same as remove_field().

This will return ErrorKind::NotFound when the identifier does not exist, and ErrorKind::InvalidInput when the index is out of range.

Source

pub fn remove_field(&mut self, name: &str) -> Result<()>

Remove all data for a field

This removes all data stored at the identfier name. It will return ErrorKind::NotFound if there is no data stored at name.

Source

pub fn get_info(&self, name: &str) -> Option<(u32, usize, bool)>

Retrieve the type, the number of items and whether or not it is fixed data

This method returns a tuple consisting of the type_code, the number of items and whether or not the data size is fixed, or None if there is no data.

Source

pub fn is_empty(&self) -> bool

Check if the message has data associated with it

Source

pub fn is_system(&self) -> bool

Check if the message is a system message

System messages have a what code that is built up from the ‘_’ (underscore) character, and three capital letters. Example: _BED Because of this fact it is advised not to give your message codes this structure.

Source

pub fn is_reply(&self) -> bool

Check if the message is a reply message

Source

pub fn was_delivered(&self) -> bool

Check if this message was delivered through a messenger

Source

pub fn is_source_waiting(&self) -> bool

Check if the source is waiting for a reply

Source

pub fn is_source_remote(&self) -> bool

Check if the source is another application than the current

Source

pub fn get_return_address(&self) -> Option<Messenger>

Get a Messenger to the sender of this message

Trait Implementations§

Source§

impl Debug for Message

Source§

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

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

impl Flattenable<Message> for Message

Source§

fn type_code() -> u32

The type code is a unique identifier that identifies the flattened data
Source§

fn flattened_size(&self) -> usize

Return the size of the flattened type
Source§

fn is_fixed_size() -> bool

Check if flattened objects of this type are always a fixed size
Source§

fn flatten(&self) -> Vec<u8>

Return a flattened version of this object
Source§

fn unflatten(buffer: &[u8]) -> Result<Message>

Unflatten an object from a stream

Auto Trait Implementations§

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