pub struct Message<'a> { /* private fields */ }
Expand description
A message writing into a preallocated buffer
Implementations§
source§impl<'a> Message<'a>
impl<'a> Message<'a>
A CoAP messag that resides in contiguous mutable memory
Exceeding the guarantees of MutableWritableMessage, this does allow some out-of-sequence invocations: Even after payload has been written to, options can be added, memmove’ing (right-rotating) the written payload, possibly truncating it out of the buffer. This is needed to accommodate libOSCORE’s requirements (because while libOSCORE can also do without a memmove-ing message, that’d require its use through WritableMessage to adhere to in-OSCORE write sequence conventions, making the whole situation no easier on the coap-message abstraction). Data will only be moved if an option is added after content has been set, so this comes at no runtime cost for those who do not need it. (It may be later turned into a feature. Then, the memmove code would be removed; carrying the latest option number in the WriteState should come at no extra cost due to the struct’s content and alignment).
pub fn new(code: &'a mut u8, tail: &'a mut [u8]) -> Self
sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Discard anything that has been written in the message, and allow any operation that would
be allowed after calling Self::new()
.
This is a short-cut to messages that can be snapshotted and rewound, and a band-aid until that is available in traits.
sourcepub fn new_from_existing(code: &'a mut u8, tail: &'a mut [u8]) -> Self
pub fn new_from_existing(code: &'a mut u8, tail: &'a mut [u8]) -> Self
Create a MutableWritableMessage on a buffer that already contains a serialized message
While this is generally not useful (a parsed message has its paylod already set, and if there’s no payload, there’s no space to add options or any payload), it allows mutable access to the option bytes and the payload. This is primarily useful in situations when data is processed in place, eg. decrypted (in OSCORE), or CBOR is shifted around to get contiguous slices out of indefinite length strings.
This uses the same strategy for errors as crate::inmemory::Message: Validation happens incrementally.
Trait Implementations§
source§impl<'a> MinimalWritableMessage for Message<'a>
impl<'a> MinimalWritableMessage for Message<'a>
§type Code = u8
type Code = u8
Self::set_code()
§type OptionNumber = u16
type OptionNumber = u16
§type UnionError = WriteError
type UnionError = WriteError
Code
and OptionNumber
, can be .into()
ed. Read more§type AddOptionError = WriteError
type AddOptionError = WriteError
§type SetPayloadError = WriteError
type SetPayloadError = WriteError
source§fn set_code(&mut self, code: u8)
fn set_code(&mut self, code: u8)
source§fn add_option(&mut self, number: u16, data: &[u8]) -> Result<(), WriteError>
fn add_option(&mut self, number: u16, data: &[u8]) -> Result<(), WriteError>
source§fn set_payload(&mut self, payload: &[u8]) -> Result<(), WriteError>
fn set_payload(&mut self, payload: &[u8]) -> Result<(), WriteError>
source§fn set_from_message<M>(&mut self, msg: &M) -> Result<(), Self::UnionError>where
M: ReadableMessage + WithSortedOptions,
fn set_from_message<M>(&mut self, msg: &M) -> Result<(), Self::UnionError>where
M: ReadableMessage + WithSortedOptions,
source§fn add_option_str(
&mut self,
number: Self::OptionNumber,
value: &str
) -> Result<(), Self::AddOptionError>
fn add_option_str( &mut self, number: Self::OptionNumber, value: &str ) -> Result<(), Self::AddOptionError>
add_option(self, number, value.as_bytes())
. Read moresource§fn add_option_uint<U>(
&mut self,
number: Self::OptionNumber,
value: U
) -> Result<(), Self::AddOptionError>
fn add_option_uint<U>( &mut self, number: Self::OptionNumber, value: U ) -> Result<(), Self::AddOptionError>
add_option
on a buffer containing the uint encoded value Read moresource§fn convert_code_error(e: <Self::Code as Code>::Error) -> Self::UnionError
fn convert_code_error(e: <Self::Code as Code>::Error) -> Self::UnionError
Self::Code::Error
Read moresource§fn convert_option_number_error(
e: <Self::OptionNumber as OptionNumber>::Error
) -> Self::UnionError
fn convert_option_number_error( e: <Self::OptionNumber as OptionNumber>::Error ) -> Self::UnionError
Self::OptionNumber::Error
Read moresource§fn convert_add_option_error(e: Self::AddOptionError) -> Self::UnionError
fn convert_add_option_error(e: Self::AddOptionError) -> Self::UnionError
Self::AddOptionError
Read moresource§fn convert_set_payload_error(e: Self::SetPayloadError) -> Self::UnionError
fn convert_set_payload_error(e: Self::SetPayloadError) -> Self::UnionError
Self::SetPayloadError
Read moresource§impl<'a> MutableWritableMessage for Message<'a>
impl<'a> MutableWritableMessage for Message<'a>
source§fn available_space(&self) -> usize
fn available_space(&self) -> usize
source§fn payload_mut_with_len(&mut self, len: usize) -> Result<&mut [u8], WriteError>
fn payload_mut_with_len(&mut self, len: usize) -> Result<&mut [u8], WriteError>
len
bytes of the payload for writing Read moresource§fn truncate(&mut self, len: usize) -> Result<(), WriteError>
fn truncate(&mut self, len: usize) -> Result<(), WriteError>
MinimalWritableMessage::set_payload
, or with a suitable MutableWritableMessage::payload_mut_with_len
call.source§impl<'a> ReadableMessage for Message<'a>
impl<'a> ReadableMessage for Message<'a>
§type Code = u8
type Code = u8
Self::code()
§type MessageOption<'b> = MessageOption<'b>
where
Self: 'b
type MessageOption<'b> = MessageOption<'b> where Self: 'b
§type OptionsIter<'b> = OptionsIter<'b>
where
Self: 'b
type OptionsIter<'b> = OptionsIter<'b> where Self: 'b
Self::options()