Struct coap_lite::Packet

source ·
pub struct Packet {
    pub header: Header,
    pub payload: Vec<u8>,
    /* private fields */
}
Expand description

The CoAP packet.

Fields§

§header: Header§payload: Vec<u8>

Implementations§

source§

impl Packet

source

pub const MAX_SIZE: usize = 1_280usize

Maximum allowed packet size. By default limited to 1280 so that CoAP packets can be sent over TCP or UDP.

source

pub fn new() -> Packet

Creates a new packet.

source

pub fn options(&self) -> Iter<'_, u16, LinkedList<Vec<u8>>>

Returns an iterator over the options of the packet.

source

pub fn set_token(&mut self, token: Vec<u8>)

Sets the token.

source

pub fn get_token(&self) -> &[u8]

Returns the token.

source

pub fn set_option(&mut self, tp: CoapOption, value: LinkedList<Vec<u8>>)

Sets an option’s values.

source

pub fn set_options_as<T: OptionValueType>( &mut self, tp: CoapOption, value: LinkedList<T> )

Sets an option’s values using a structured option value format.

source

pub fn get_option(&self, tp: CoapOption) -> Option<&LinkedList<Vec<u8>>>

Returns an option’s values.

source

pub fn get_options_as<T: OptionValueType>( &self, tp: CoapOption ) -> Option<LinkedList<Result<T, IncompatibleOptionValueFormat>>>

Returns an option’s values all decoded using the specified structured option value format.

source

pub fn get_first_option(&self, tp: CoapOption) -> Option<&Vec<u8>>

Returns an option’s first value as a convenience when only one is expected.

source

pub fn get_first_option_as<T: OptionValueType>( &self, tp: CoapOption ) -> Option<Result<T, IncompatibleOptionValueFormat>>

Returns an option’s first value as a convenience when only one is expected.

source

pub fn add_option(&mut self, tp: CoapOption, value: Vec<u8>)

Adds an option value.

source

pub fn add_option_as<T: OptionValueType>(&mut self, tp: CoapOption, value: T)

Adds an option value using a structured option value format.

source

pub fn clear_option(&mut self, tp: CoapOption)

Removes an option.

source

pub fn clear_all_options(&mut self)

Removes all options.

source

pub fn set_content_format(&mut self, cf: ContentFormat)

Sets the content-format.

source

pub fn get_content_format(&self) -> Option<ContentFormat>

Returns the content-format.

source

pub fn set_observe_value(&mut self, value: u32)

Sets the value of the observe option.

source

pub fn get_observe_value( &self ) -> Option<Result<u32, IncompatibleOptionValueFormat>>

Returns the value of the observe option.

source

pub fn from_bytes(buf: &[u8]) -> Result<Packet, MessageError>

Decodes a byte slice and constructs the equivalent packet.

source

pub fn to_bytes(&self) -> Result<Vec<u8>, MessageError>

Returns a vector of bytes representing the Packet.

source

pub fn to_bytes_with_limit(&self, limit: usize) -> Result<Vec<u8>, MessageError>

Returns a vector of bytes representing the Packet, using a custom limit instead of Packet::MAX_SIZE for the message size check.

source

pub fn to_bytes_unlimited(&self) -> Result<Vec<u8>, MessageError>

Returns a vector of bytes representing the Packet, skipping the message size check against Packet::MAX_SIZE.

Trait Implementations§

source§

impl Clone for Packet

source§

fn clone(&self) -> Packet

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Packet

source§

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

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

impl Default for Packet

source§

fn default() -> Packet

Returns the “default value” for a type. Read more
source§

impl MinimalWritableMessage for Packet

§

type Code = MessageClass

§

type OptionNumber = CoapOption

§

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)
§

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)
§

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§

fn set_code(&mut self, code: Self::Code)

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

fn add_option( &mut self, option: Self::OptionNumber, data: &[u8] ) -> Result<(), Self::AddOptionError>

Add an option to the message Read more
source§

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

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 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 MinimalWritableMessage for Packet

§

type Code = MessageClass

§

type OptionNumber = CoapOption

source§

fn set_code(&mut self, code: Self::Code)

source§

fn add_option(&mut self, option: Self::OptionNumber, data: &[u8])

Add an option to the message Read more
source§

fn set_payload(&mut self, payload: &[u8])

source§

fn set_from_message<M>(&mut self, msg: &M)
where M: ReadableMessage,

Copy code, options and payload in from a readable message Read more
source§

fn add_option_str(&mut self, number: Self::OptionNumber, value: &str)

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

fn add_option_uint<U>(&mut self, number: Self::OptionNumber, value: U)
where U: Ux,

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

impl MutableWritableMessage for Packet

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], Self::SetPayloadError>

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

fn truncate(&mut self, length: usize) -> Result<(), Self::SetPayloadError>

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 MutableWritableMessage for Packet

source§

fn available_space(&self) -> usize

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

fn payload_mut(&mut self) -> &mut [u8]

👎Deprecated since 0.1.1: Use payload_mut_with_len instead
Legacy method for mutable access to the payload Read more
source§

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

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

fn truncate(&mut self, length: usize)

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 Packet

§

type Code = MessageClass

§

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

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

type OptionsIter<'a> = MessageOptionAdapter<'a>

source§

fn code(&self) -> Self::Code

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(&self) -> Self::OptionsIter<'_>

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

impl ReadableMessage for Packet

§

type Code = MessageClass

§

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

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

type OptionsIter<'a> = MessageOptionAdapter<'a>

source§

fn code(&self) -> Self::Code

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(&self) -> Self::OptionsIter<'_>

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

impl SeekWritableMessage for Packet

source§

impl SeekWritableMessage for Packet

source§

impl WithSortedOptions for Packet

source§

impl WithSortedOptions for Packet

Auto Trait Implementations§

§

impl Freeze for Packet

§

impl RefUnwindSafe for Packet

§

impl Send for Packet

§

impl Sync for Packet

§

impl Unpin for Packet

§

impl UnwindSafe for Packet

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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

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

§

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.