pub struct MessageMut<EM: MessageBufferMut> { /* private fields */ }Expand description
A CoAP message 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, as long as it does not truncate it. 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).
When viewed through the coap_message::ReadableMessage trait, this will behave as if writing
had stopped (but still allows writes after reading); in particular, the payload will be shown
as empty. (This may be obvious from most points of view, but when coming from a
coap_message::MutableWritableMessage point of view where payloads can only ever be truncated and not
made longer, this clarification is relevant).
The type is covariant over its lifetime. This also means that we can never add any methods
where we move references into Self, but we don’t do this anyway: The lifetime 'a only serves
to describe the memory backing it; data is moved in there, not stored by reference. This
property is accessible through downcasting.
§Invariants
The message in the EM is always a well-formed CoAP message. This is upheld by construction or
checked at creation time (when existing messages are consumed). As the underlying
MessageBufferMut can be implemented without unsafe, this crate can only panic (but not
invoke undefined behavior) when that is violated.
Implementations§
Source§impl<'a> MessageMut<SliceBufferMut<'a>>
impl<'a> MessageMut<SliceBufferMut<'a>>
Sourcepub fn new_in_slice(code: &'a mut u8, tail: &'a mut [u8]) -> Self
pub fn new_in_slice(code: &'a mut u8, tail: &'a mut [u8]) -> Self
Constructor of a message buffer around exclusive memory.
This is a short-cut for creating a SliceBufferMut and passing it to
Self::new_empty().
Sourcepub fn new_from_encoded_slice(
code: &'a mut u8,
tail: &'a mut [u8],
) -> Result<Self, ParsingError>
pub fn new_from_encoded_slice( code: &'a mut u8, tail: &'a mut [u8], ) -> Result<Self, ParsingError>
Creates a MutableWritableMessage on a buffer that already contains a serialized message.
While this is generally not useful (as the message has been completed and there is no room for anything more), 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.
If the message has been parsed as a Message around a mutable back-end
before, it is preferable to use its
.into_mutable() method, as that can avoid the
full parsing that is needed to uphold this type’s guarantees.
§Errors
… are returned if the input slice does not contain a well-formed message.
Source§impl<T: MessageBufferMut> MessageMut<T>
impl<T: MessageBufferMut> MessageMut<T>
Sourcepub fn new_empty(encoded: T) -> Self
pub fn new_empty(encoded: T) -> Self
Constructor of a message buffer around any MessageBufferMut.
Sourcepub fn downcast_from<M: MinimalWritableMessage>(
generic: &mut M,
) -> Option<&mut Self>
Available on crate feature downcast only.
pub fn downcast_from<M: MinimalWritableMessage>( generic: &mut M, ) -> Option<&mut Self>
downcast only.Returns a full Self from input that is expected (but not known at type level) to be one.
Source§impl<EM: MessageBufferMut> MessageMut<EM>
impl<EM: MessageBufferMut> MessageMut<EM>
Sourcepub fn insert_option(
&mut self,
number: u16,
data: &[u8],
) -> Result<(), WriteError>
pub fn insert_option( &mut self, number: u16, data: &[u8], ) -> Result<(), WriteError>
Inserts an option.
This behaves similarly to add_option(), but without the limitation of needing to
uphold option ordering manually.
This comes at the cost of iterating over all options and re-encoding one.
§Errors
This function returns a WriteError if there isn’t sufficient space left for the
insertion.
Sourcepub fn retain_options<P>(&mut self, predicate: P)
pub fn retain_options<P>(&mut self, predicate: P)
Retains all options the predicate applies to, the rest will be removed.
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Discards anything that has been written in the message, and allows any operation that would
be allowed after calling Self::new_empty().
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 untruncate(&mut self, added_len: usize) -> Result<(), WriteError>
pub fn untruncate(&mut self, added_len: usize) -> Result<(), WriteError>
Expands the bounds of the payload by added_len.
Similar to payload_mut_with_len(),
but it has no restriction on length (beyond the length of the underlying buffer).
§Errors
… are raised if there is insufficient space for extending the payload.
Trait Implementations§
Source§impl<EM: Clone + MessageBufferMut> Clone for MessageMut<EM>
impl<EM: Clone + MessageBufferMut> Clone for MessageMut<EM>
Source§fn clone(&self) -> MessageMut<EM>
fn clone(&self) -> MessageMut<EM>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<EM: MessageBufferMut> Debug for MessageMut<EM>
impl<EM: MessageBufferMut> Debug for MessageMut<EM>
Source§impl<EM: MessageBufferMut> Format for MessageMut<EM>
Available on crate feature defmt only.
impl<EM: MessageBufferMut> Format for MessageMut<EM>
defmt only.Source§impl<EM: MessageBufferMut> MinimalWritableMessage for MessageMut<EM>
impl<EM: MessageBufferMut> MinimalWritableMessage for MessageMut<EM>
Source§type Code = u8
type Code = u8
Self::set_code()Source§type OptionNumber = u16
type OptionNumber = u16
Source§type UnionError = WriteError
type UnionError = WriteError
Code and OptionNumber, can be .into()ed. Read moreSource§type AddOptionError = WriteError
type AddOptionError = WriteError
Source§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 with_static_type_annotation(
&mut self,
) -> Option<RefMutWithStaticType<'_, Self>>
fn with_static_type_annotation( &mut self, ) -> Option<RefMutWithStaticType<'_, Self>>
Source§fn promote_to_mutable_writable_message(&mut self) -> Option<&mut Self>
fn promote_to_mutable_writable_message(&mut self) -> Option<&mut Self>
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<EM: MessageBufferMut> MutableWritableMessage for MessageMut<EM>
impl<EM: MessageBufferMut> MutableWritableMessage for MessageMut<EM>
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<EM: MessageBufferMut> ReadableMessage for MessageMut<EM>
impl<EM: MessageBufferMut> ReadableMessage for MessageMut<EM>
Source§type Code = u8
type Code = u8
Self::code()Source§type MessageOption<'b> = MessageOption<'b>
where
Self: 'b
type MessageOption<'b> = MessageOption<'b> where Self: 'b
Source§type OptionsIter<'b> = OptionsIter<'b>
where
Self: 'b
type OptionsIter<'b> = OptionsIter<'b> where Self: 'b
Self::options()Source§fn options(&self) -> <Self as ReadableMessage>::OptionsIter<'_>
fn options(&self) -> <Self as ReadableMessage>::OptionsIter<'_>
Source§fn with_static_type_annotation(&self) -> Option<RefWithStaticType<'_, Self>>
fn with_static_type_annotation(&self) -> Option<RefWithStaticType<'_, Self>>
impl<EM: MessageBufferMut> WithSortedOptions for MessageMut<EM>
Auto Trait Implementations§
impl<EM> Freeze for MessageMut<EM>where
EM: Freeze,
impl<EM> RefUnwindSafe for MessageMut<EM>where
EM: RefUnwindSafe,
impl<EM> Send for MessageMut<EM>where
EM: Send,
impl<EM> Sync for MessageMut<EM>where
EM: Sync,
impl<EM> Unpin for MessageMut<EM>where
EM: Unpin,
impl<EM> UnsafeUnpin for MessageMut<EM>where
EM: UnsafeUnpin,
impl<EM> UnwindSafe for MessageMut<EM>where
EM: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<M> ShowMessageExt for Mwhere
M: ReadableMessage,
impl<M> ShowMessageExt for Mwhere
M: ReadableMessage,
Source§fn show(&self) -> ShowMessage<'_, Self>
fn show(&self) -> ShowMessage<'_, Self>
core::fmt::Debug imlementation, and also provide
[defmt_0_3::Format] if the defmt_0_3 feature is selected. Read more