[−][src]Trait coap_message::MutableWritableMessage
A message that allows later manipulation of a once set payload, and later truncation.
This is a bit of an unsorted bag that needs further cleanup (FIXME) -- most of this is
motivated by block-wise and write-in-place. Might need a bit of reshape, possibly something
like a once-callable .write_payload(|d: &mut [u8]| { write_to(d); Ok(bytes_written)})
. Does
this need a hint of the length to allocate for implementations that don't pre-allocate the
message? Is 1024 a good enough value to not pass it?
The available_space is only needed where applications want to use up the last byte by not zero-padding the Block2 option to its szx=0 equivalent.
Can that be efficiently be replaced with something like this, and can it be optimized down to the hand-written counting-of-option-bytes that's involved in the use of available_space?
let mut m = allocated_message; for szx in 6..0 { snap = m.snapshot(); m.add_option(BLOCK2, ...); m.add_option(..., ...); if let Ok(_) = m.write_payload(|p| { if (p.len() < 1 << (4 + szx)) { return Err(()); } let written = write_block(...); Ok(written) }) { break; } else { m = m.revert_to(snap); } } else { panic!("Allocated space doesn't even suffice for 16 byte payload"); }
Required methods
fn available_space(&self) -> usize
Number of bytes available for additional options, payload marker and payload
fn payload_mut(&mut self) -> &mut [u8]
fn truncate(&mut self, len: usize)
fn mutate_options<F>(&mut self, callback: F) where
F: FnMut(Self::OptionNumber, &mut [u8]),
F: FnMut(Self::OptionNumber, &mut [u8]),