pub struct Packet { /* private fields */ }
Expand description

Gives structure to data to be sent or received from stream.

Packet is the lowest abstraction above buffer in this library.

Fields

  • size – size of the whole packet in number of bytes. It is u16 so that packet can not have size over u16::MAX
  • kindkind of packet.
  • content – data stored in the packet.

Implementations

Creates a new Packet.

Returns an Error if created packet would be bigger than MAX_PACKET_SIZE.

To avoid this check, use Packet::new_unchecked.

Size of packet is derived from content.

Examples

Content packet is created like that.

    let packet = Packet::new(PacketKind::End, Bytes::from([1_u8, 2_u8]))?;

Creates a new Packet without checking if resulting packet size is not bigger than MAX_PACKET_SIZE.

Size of packet is derived from content.

Sends self via stream.

This takes ownership of self.

Sends self to given address on UdpSocket.

This takes ownership of self.

Sends self to connected UdpSocket.

This takes ownership of self.

Receives a Packet from stream.

Receives a Packet and a SocketAddr on a UdpSocket.

Receives a Packet on a connected UdpSocket.

Peeks on data on TcpStream if those data are valid, Packet is created.

Since TcpStream::peek is used to retrieve data, calling this again returns the same data.

Peeks on data on socket, if valid data are received a Packet is created and returned together with a SocketAddr.

Since UdpSocket::peek_from is used to retrieve data, calling this again returns the same data.

Peeks on data on a connected socket, if valid data are received a Packet is created.

Since UdpSocket::peek is used to retrieve data, calling this again returns the same data.

Returns number of packets needed to send data with given byte length.

Takes Bytes and returns Vec of Bytes each with maximum length of MAX_CONTENT_SIZE.

Returns size.

Returns kind.

Returns content.

Content is cloned.

Returns a reference to content.

Returns a mutable reference to content.

Takes ownership of self and returns content.

Returns maximum number of bytes that can one Packet hold as u16. This is global value for every Packet that can be changed with Packet::set_max_size.

Minimum value is 5 bytes, 2 for packet size(u16), 2 for packet kind, and at least 1 for content.

This accesses a mutable static with AtomicU16 to provide global access to this data and an ability to change its value for specific needs, but since AtomicU16 is used it is capped by u16::MAX.

Sets MAX_PACKET_SIZE to size.

Minimum value is 5 bytes, 2 for packet size(u16), 2 for packet kind, and at least 1 for content.

For more information look at Packet::max_size.

Returns minimal number of bytes that is every packet guaranteed to have, 2 bytes are for its size and 2 for its kind as u16.

Maximum amount of bytes that a Packet can use for its content, its lower than MAX_PACKET_SIZE by PACKET_DESCRIPTION_SIZE.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

Deserialize this value from the given Serde deserializer. Read more

Converts to this type from the input type.

Creates an implementor from RON. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Serialize this value into the given Serde serializer. Read more

Returns a RON from implementor. Read more

Returns a pretty formatted RON from implementor. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

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

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.