Struct HeapMessage

Source
pub struct HeapMessage { /* private fields */ }
Available on crate feature alloc only.
Expand description

A heap CoAP message backed by allocated memory

It stores its payload in a Vec, and uses a BTreeMap<_, Vec<_>> to store all the individual option values.

It offers a few methods for direct manipulation of options, even out of sequence, that can not be expected from a general message buffer and are thus not captured in traits.

use coap_message::{MinimalWritableMessage, ReadableMessage};
let mut m = HeapMessage::new();
m.set_code(1);
m.add_option(11, b".well-known");
m.add_option(11, b"core");

let mut m2 = HeapMessage::new();
m2.set_from_message(&m);
assert!(m.code() == 1);

Implementations§

Source§

impl HeapMessage

Source

pub fn new() -> Self

Source

pub fn change_option( &mut self, optnum: u16, occurrence: usize, value: impl Into<Vec<u8>>, )

Replace the occurrence’th value of the optnum option in the message

Panics if there’s not occurrence + 1 options of that number.

Source

pub fn remove_option(&mut self, optnum: u16, occurrence: usize)

Remove the occurrence’th option of option number optnum

Panics if there’s not occurrence + 1 options of that number.

Trait Implementations§

Source§

impl Debug for HeapMessage

Source§

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

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

impl MinimalWritableMessage for HeapMessage

Source§

type AddOptionError = Infallible

Error returned when an option can not be added (eg. for lack of space, or because an option of a higher number or even the payload was already set)
Source§

type SetPayloadError = Infallible

Error returned when setting the payload (eg. for lack of space, or when a message of that type does not take a payload)
Source§

type UnionError = Infallible

Error type into which either of the other errors, as well as the errors for conversion of the Code and OptionNumber, can be .into()ed. Read more
Source§

type Code = u8

Source§

type OptionNumber = u16

Source§

fn set_code(&mut self, code: u8)

Set the CoAP code of the message (in a request, that is the request method)
Source§

fn add_option(&mut self, optnum: u16, data: &[u8]) -> Result<(), Infallible>

Add an option to the message Read more
Source§

fn set_payload(&mut self, payload: &[u8]) -> Result<(), Infallible>

Set the payload to the message Read more
Source§

fn set_from_message<M>(&mut self, msg: &M) -> Result<(), Self::UnionError>

Copy code, options and payload in from a readable message
Source§

fn add_option_str( &mut self, number: Self::OptionNumber, value: &str, ) -> Result<(), Self::AddOptionError>

Shortcut for add_option(self, number, value.as_bytes()). Read more
Source§

fn add_option_uint<U>( &mut self, number: Self::OptionNumber, value: U, ) -> Result<(), Self::AddOptionError>
where U: Unsigned + ToBytes,

Shortcut for add_option on a buffer containing the uint encoded value Read more
Source§

fn with_static_type_annotation( &mut self, ) -> Option<RefMutWithStaticType<'_, Self>>

Type ID of Self or a ’static version of Self Read more
Source§

fn promote_to_mutable_writable_message( &mut self, ) -> Option<&mut (impl MinimalWritableMessage<Code = Self::Code, OptionNumber = Self::OptionNumber, AddOptionError = Self::AddOptionError, SetPayloadError = Self::SetPayloadError, UnionError = Self::UnionError> + MutableWritableMessage)>

Tries to obtain a MutableWritableMessage from self. Read more
Source§

fn convert_code_error(e: <Self::Code as Code>::Error) -> Self::UnionError

Auxiliary function for converting Self::Code::Error Read more
Source§

fn convert_option_number_error( e: <Self::OptionNumber as OptionNumber>::Error, ) -> Self::UnionError

Auxiliary function for converting Self::OptionNumber::Error Read more
Source§

fn convert_add_option_error(e: Self::AddOptionError) -> Self::UnionError

Auxiliary function for converting Self::AddOptionError Read more
Source§

fn convert_set_payload_error(e: Self::SetPayloadError) -> Self::UnionError

Auxiliary function for converting Self::SetPayloadError Read more
Source§

impl MutableWritableMessage for HeapMessage

Source§

fn available_space(&self) -> usize

Number of bytes available for additional options, payload marker and payload
Source§

fn payload_mut_with_len(&mut self, len: usize) -> Result<&mut [u8], Infallible>

Memory-map len bytes of the payload for writing Read more
Source§

fn truncate(&mut self, len: usize) -> Result<(), Infallible>

Truncate an already-set payload to the given length; that payload must have been written to before using MinimalWritableMessage::set_payload, or with a suitable MutableWritableMessage::payload_mut_with_len call.
Source§

fn mutate_options<F>(&mut self, callback: F)
where F: FnMut(Self::OptionNumber, &mut [u8]),

Apply a callback to all options in sequence Read more
Source§

impl ReadableMessage for HeapMessage

Source§

type Code = u8

Source§

type MessageOption<'a> = MessageOption<'a>

Type of an individual option, indiciating its option number and value
Source§

type OptionsIter<'a> = ReadCursor<'a>

Source§

fn code(&self) -> u8

Get the code (request method or response code) of the message Read more
Source§

fn payload(&self) -> &[u8]

Get the payload set in the message Read more
Source§

fn options<'m>(&'m self) -> ReadCursor<'m>

Produce all options in arbitrary order as an iterator Read more
Source§

fn with_static_type_annotation(&self) -> Option<RefWithStaticType<'_, Self>>

Type ID of Self or a ’static version of Self Read more
Source§

impl SeekWritableMessage for HeapMessage

Source§

impl WithSortedOptions for HeapMessage

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<M> ShowMessageExt for M
where M: ReadableMessage,

Source§

fn show(&self) -> ShowMessage<'_, Self>

Wraps the message to have a core::fmt::Debug imlementation, and also provide [defmt_0_3::Format] if the defmt_0_3 feature is selected. 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.