Trait GenericPacketTrait

Source
pub trait GenericPacketTrait {
    // Required methods
    fn size(&self) -> usize;
    fn to_buffers(&self) -> Vec<IoSlice<'_>>;
}
Expand description

MIT License

Copyright (c) 2025 Takatoshi Kondo

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Required Methods§

Source

fn size(&self) -> usize

Source

fn to_buffers(&self) -> Vec<IoSlice<'_>>

Implementors§

Source§

impl GenericPacketTrait for mqtt_protocol_core::mqtt::packet::v3_1_1::Connack

Implementation of GenericPacketTrait for generic packet operations

Provides a common interface for packet operations that can be used generically across different MQTT packet types. This allows for uniform handling of packets in collections and generic contexts.

This trait is particularly useful when working with mixed packet types or implementing packet processing pipelines that need to handle various MQTT packet types uniformly.

§Methods

  • size(): Returns the total packet size in bytes
  • to_buffers(): Returns IoSlice buffers for efficient network I/O

§Examples

use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::packet::GenericPacketTrait;
use mqtt_protocol_core::mqtt::result_code::ConnectReturnCode;

let connack = mqtt::packet::v3_1_1::Connack::builder()
    .session_present(false)
    .return_code(ConnectReturnCode::Accepted)
    .build()
    .unwrap();

// Use generic trait methods
let size = connack.size();
let buffers = connack.to_buffers();

// Can be used in generic contexts
fn process_packet<T: GenericPacketTrait>(packet: &T) {
    println!("Packet size: {} bytes", packet.size());
}

process_packet(&connack);
Source§

impl GenericPacketTrait for mqtt_protocol_core::mqtt::packet::v3_1_1::Connect

Implementation of GenericPacketTrait for CONNECT packets

This trait provides a generic interface for packet operations, allowing CONNECT packets to be used polymorphically with other packet types.

The implementation delegates to the specific CONNECT packet methods.

Source§

impl GenericPacketTrait for mqtt_protocol_core::mqtt::packet::v3_1_1::Disconnect

Implements the generic packet trait for DISCONNECT packets

This trait provides a common interface for all MQTT packet types, allowing them to be used polymorphically in packet processing code. The implementation delegates to the specific DISCONNECT methods.

§Examples

use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::packet::GenericPacketTrait;

let disconnect = mqtt::packet::v3_1_1::Disconnect::new();

// Use through the generic trait
let size = disconnect.size();
let buffers = disconnect.to_buffers();
assert_eq!(size, 2);
assert_eq!(buffers.len(), 2);
Source§

impl GenericPacketTrait for mqtt_protocol_core::mqtt::packet::v3_1_1::Pingreq

Implements the generic packet trait for PINGREQ packets

This trait provides a common interface for all MQTT packet types, allowing them to be used polymorphically in generic contexts.

§Methods

  • size(): Returns the packet size in bytes
  • to_buffers(): Returns I/O slices for network transmission
Source§

impl GenericPacketTrait for mqtt_protocol_core::mqtt::packet::v3_1_1::Pingresp

Implements the generic packet trait for PINGRESP packets

This trait provides a common interface for all MQTT packet types, allowing them to be used polymorphically in generic contexts such as packet serialization, transmission, and size calculation.

§Methods

  • size(): Returns the packet size in bytes
  • to_buffers(): Returns I/O slices for network transmission

§Examples

use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::prelude::*;

let pingresp = mqtt::packet::v3_1_1::Pingresp::new();
let generic_packet: &dyn mqtt::packet::GenericPacketTrait = &pingresp;
assert_eq!(generic_packet.size(), 2);
Source§

impl GenericPacketTrait for Auth

GenericPacketTrait implementation for AUTH packets

Implements the common packet interface that allows AUTH packets to be used polymorphically with other MQTT packet types. This trait provides standardized methods for packet size calculation and buffer generation.

Source§

impl GenericPacketTrait for mqtt_protocol_core::mqtt::packet::v5_0::Connack

Implementation of GenericPacketTrait for generic packet operations

Provides a common interface for packet operations that can be used generically across different MQTT packet types. This allows for uniform handling of packets in collections and generic contexts.

§Methods

  • size(): Returns the total packet size in bytes
  • to_buffers(): Returns IoSlice buffers for efficient network I/O

§Examples

use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::packet::GenericPacketTrait;
use mqtt_protocol_core::mqtt::result_code::ConnectReasonCode;

let connack = mqtt::packet::v5_0::Connack::builder()
    .session_present(false)
    .reason_code(ConnectReasonCode::Success)
    .build()
    .unwrap();

// Use generic trait methods
let size = connack.size();
let buffers = connack.to_buffers();
Source§

impl GenericPacketTrait for mqtt_protocol_core::mqtt::packet::v5_0::Connect

Implements generic packet behavior for CONNECT packets

This trait provides common functionality shared across all MQTT packet types, enabling polymorphic handling of different packet types through a common interface. It provides methods for getting packet size and converting to I/O buffers.

§Purpose

This implementation allows CONNECT packets to be used polymorphically with other MQTT packet types in generic contexts, such as packet processing pipelines.

§Examples

use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::packet::GenericPacketTrait;

let connect = mqtt::packet::v5_0::Connect::builder()
    .client_id("generic-client")
    .build()
    .unwrap();

// Use as generic packet
let size = connect.size();
let buffers = connect.to_buffers();
Source§

impl GenericPacketTrait for mqtt_protocol_core::mqtt::packet::v5_0::Disconnect

Implements the generic packet trait for DISCONNECT packets

This trait provides a common interface for all MQTT packet types, allowing them to be used polymorphically in packet processing code.

§Examples

use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::packet::GenericPacketTrait;

let disconnect = mqtt::packet::v5_0::Disconnect::builder()
    .build()
    .unwrap();

// Use through the generic trait
let size = disconnect.size();
let buffers = disconnect.to_buffers();
Source§

impl GenericPacketTrait for mqtt_protocol_core::mqtt::packet::v5_0::Pingreq

Implements the generic packet trait for PINGREQ packets

This trait provides a common interface for all MQTT packet types, allowing them to be used polymorphically in generic contexts.

§Methods

  • size(): Returns the packet size in bytes
  • to_buffers(): Returns I/O slices for network transmission
Source§

impl GenericPacketTrait for mqtt_protocol_core::mqtt::packet::v5_0::Pingresp

Implements the generic packet trait for PINGRESP packets

This trait provides a common interface for all MQTT packet types, allowing them to be used polymorphically in generic contexts.

§Methods

  • size(): Returns the packet size in bytes
  • to_buffers(): Returns I/O slices for network transmission
Source§

impl<PacketIdType> GenericPacketTrait for GenericPacket<PacketIdType>
where PacketIdType: IsPacketId + Serialize,

Source§

impl<PacketIdType> GenericPacketTrait for GenericStorePacket<PacketIdType>
where PacketIdType: IsPacketId + Serialize,

Source§

impl<PacketIdType> GenericPacketTrait for mqtt_protocol_core::mqtt::packet::v3_1_1::GenericPuback<PacketIdType>
where PacketIdType: IsPacketId,

GenericPacketTrait implementation for GenericPuback.

Provides the core packet interface methods required by the MQTT protocol library. This trait enables the PUBACK packet to be used generically alongside other packet types in the protocol processing pipeline.

§Trait Methods

  • size(): Returns the total packet size in bytes
  • to_buffers(): Converts the packet to I/O slices for transmission
Source§

impl<PacketIdType> GenericPacketTrait for mqtt_protocol_core::mqtt::packet::v3_1_1::GenericPubcomp<PacketIdType>
where PacketIdType: IsPacketId,

GenericPacketTrait implementation for PUBCOMP packets.

This trait provides a unified interface for all MQTT packet types, enabling generic packet handling and processing across different packet implementations.

§Examples

use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::prelude::*;

let pubcomp = mqtt::packet::v3_1_1::Pubcomp::builder()
    .packet_id(1u16)
    .build()
    .unwrap();

// Use generic packet interface
let packet_size = pubcomp.size();
let buffers = pubcomp.to_buffers();
assert!(packet_size >= 4); // Minimum size for PUBCOMP
Source§

impl<PacketIdType> GenericPacketTrait for mqtt_protocol_core::mqtt::packet::v3_1_1::GenericPublish<PacketIdType>
where PacketIdType: IsPacketId,

GenericPacketTrait implementation for PUBLISH packets

Provides common packet functionality including size calculation and buffer conversion for network transmission. This trait is used by the generic packet handling infrastructure.

Source§

impl<PacketIdType> GenericPacketTrait for mqtt_protocol_core::mqtt::packet::v3_1_1::GenericPubrec<PacketIdType>
where PacketIdType: IsPacketId,

Implementation of GenericPacketTrait for PUBREC packets.

This trait provides common packet functionality such as size calculation and buffer serialization. It allows PUBREC packets to be treated uniformly with other MQTT packet types in generic contexts.

§Examples

use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::prelude::*;

let pubrec = mqtt::packet::v3_1_1::Pubrec::builder()
    .packet_id(123u16)
    .build()
    .unwrap();

// Use trait methods
let size = pubrec.size();
let buffers = pubrec.to_buffers();
Source§

impl<PacketIdType> GenericPacketTrait for mqtt_protocol_core::mqtt::packet::v3_1_1::GenericPubrel<PacketIdType>
where PacketIdType: IsPacketId,

Generic packet trait implementation for PUBREL packets.

This implementation provides the generic packet interface required by the MQTT protocol library framework. It enables PUBREL packets to be used polymorphically with other packet types in the system.

§Methods

  • size(): Returns the total packet size in bytes
  • to_buffers(): Returns I/O slices for efficient transmission
Source§

impl<PacketIdType> GenericPacketTrait for mqtt_protocol_core::mqtt::packet::v3_1_1::GenericSuback<PacketIdType>
where PacketIdType: IsPacketId,

GenericPacketTrait implementation for GenericSuback

Provides the standard packet interface methods for SUBACK packets. This trait allows SUBACK packets to be used polymorphically with other MQTT packet types.

§Examples

use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::packet::GenericPacketTrait;
use mqtt_protocol_core::mqtt::result_code::SubackReturnCode;

let suback = mqtt::packet::v3_1_1::Suback::builder()
    .packet_id(42u16)
    .return_codes(vec![SubackReturnCode::SuccessMaximumQos1])
    .build()
    .unwrap();

// Use trait methods
let size = suback.size();
let buffers = suback.to_buffers();
Source§

impl<PacketIdType> GenericPacketTrait for mqtt_protocol_core::mqtt::packet::v3_1_1::GenericSubscribe<PacketIdType>
where PacketIdType: IsPacketId,

Generic packet trait implementation for SUBSCRIBE packets

Provides the common packet interface used by the MQTT protocol handler. This allows SUBSCRIBE packets to be treated uniformly with other packet types for size calculation and serialization operations.

§Methods

  • size(): Returns the total packet size in bytes
  • to_buffers(): Returns I/O slices for efficient transmission
Source§

impl<PacketIdType> GenericPacketTrait for mqtt_protocol_core::mqtt::packet::v3_1_1::GenericUnsuback<PacketIdType>
where PacketIdType: IsPacketId,

GenericPacketTrait implementation for GenericUnsuback

Provides the standard packet interface methods for UNSUBACK packets. This trait allows UNSUBACK packets to be used polymorphically with other MQTT packet types.

§Examples

use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::packet::GenericPacketTrait;

let unsuback = mqtt::packet::v3_1_1::Unsuback::builder()
    .packet_id(42u16)
    .build()
    .unwrap();

// Use trait methods
let size = unsuback.size();
let buffers = unsuback.to_buffers();
Source§

impl<PacketIdType> GenericPacketTrait for mqtt_protocol_core::mqtt::packet::v3_1_1::GenericUnsubscribe<PacketIdType>
where PacketIdType: IsPacketId,

Generic packet trait implementation for UNSUBSCRIBE packets

Provides common packet operations required by the MQTT protocol framework. This trait allows UNSUBSCRIBE packets to be used polymorphically with other packet types in the protocol implementation.

Source§

impl<PacketIdType> GenericPacketTrait for mqtt_protocol_core::mqtt::packet::v5_0::GenericPuback<PacketIdType>
where PacketIdType: IsPacketId,

GenericPacketTrait implementation for GenericPuback.

Provides the core packet interface methods required by the MQTT protocol library. This trait enables the PUBACK packet to be used generically alongside other packet types in the protocol processing pipeline.

§Trait Methods

  • size(): Returns the total packet size in bytes
  • to_buffers(): Converts the packet to I/O slices for transmission
Source§

impl<PacketIdType> GenericPacketTrait for mqtt_protocol_core::mqtt::packet::v5_0::GenericPubcomp<PacketIdType>
where PacketIdType: IsPacketId,

GenericPacketTrait implementation for PUBCOMP packets.

This trait provides a unified interface for all MQTT packet types, enabling generic packet handling and processing.

§Examples

use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::prelude::*;

let pubcomp = mqtt::packet::v5_0::Pubcomp::builder()
    .packet_id(1u16)
    .build()
    .unwrap();

// Use generic packet interface
let packet_size = pubcomp.size();
let buffers = pubcomp.to_buffers();
Source§

impl<PacketIdType> GenericPacketTrait for mqtt_protocol_core::mqtt::packet::v5_0::GenericPublish<PacketIdType>
where PacketIdType: IsPacketId,

Generic packet trait implementation for PUBLISH packets

Provides the common packet interface used by the MQTT protocol implementation for size calculation and buffer generation.

Source§

impl<PacketIdType> GenericPacketTrait for mqtt_protocol_core::mqtt::packet::v5_0::GenericPubrec<PacketIdType>
where PacketIdType: IsPacketId,

Implementation of GenericPacketTrait for PUBREC packets.

This trait provides common packet functionality such as size calculation and buffer serialization. It allows PUBREC packets to be treated uniformly with other MQTT packet types in generic contexts.

§Examples

use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::prelude::*;

let pubrec = mqtt::packet::v5_0::Pubrec::builder()
    .packet_id(123u16)
    .build()
    .unwrap();

// Use trait methods
let size = pubrec.size();
let buffers = pubrec.to_buffers();
Source§

impl<PacketIdType> GenericPacketTrait for mqtt_protocol_core::mqtt::packet::v5_0::GenericPubrel<PacketIdType>
where PacketIdType: IsPacketId,

Generic packet trait implementation for PUBREL packets.

This implementation provides the generic packet interface required by the MQTT protocol library framework. It enables PUBREL packets to be used polymorphically with other packet types in the system.

§Methods

  • size(): Returns the total packet size in bytes
  • to_buffers(): Returns I/O slices for efficient transmission
Source§

impl<PacketIdType> GenericPacketTrait for mqtt_protocol_core::mqtt::packet::v5_0::GenericSuback<PacketIdType>
where PacketIdType: IsPacketId,

GenericPacketTrait implementation for GenericSuback

Provides the standard packet interface methods for SUBACK packets. This trait allows SUBACK packets to be used polymorphically with other MQTT packet types.

§Examples

use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::packet::GenericPacketTrait;
use mqtt_protocol_core::mqtt::result_code::SubackReasonCode;

let suback = mqtt::packet::v5_0::Suback::builder()
    .packet_id(42u16)
    .reason_codes(vec![SubackReasonCode::GrantedQos1])
    .build()
    .unwrap();

// Use trait methods
let size = suback.size();
let buffers = suback.to_buffers();
Source§

impl<PacketIdType> GenericPacketTrait for mqtt_protocol_core::mqtt::packet::v5_0::GenericSubscribe<PacketIdType>
where PacketIdType: IsPacketId,

Generic packet trait implementation for SUBSCRIBE packets

Provides the common packet interface used by the MQTT protocol handler. This allows SUBSCRIBE packets to be treated uniformly with other packet types for size calculation and serialization operations.

§Methods

  • size(): Returns the total packet size in bytes
  • to_buffers(): Returns I/O slices for efficient transmission
Source§

impl<PacketIdType> GenericPacketTrait for mqtt_protocol_core::mqtt::packet::v5_0::GenericUnsuback<PacketIdType>
where PacketIdType: IsPacketId,

GenericPacketTrait implementation for GenericUnsuback

Provides the standard packet interface methods for UNSUBACK packets. This trait allows UNSUBACK packets to be used polymorphically with other MQTT packet types.

§Examples

use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::packet::GenericPacketTrait;
use mqtt_protocol_core::mqtt::result_code::UnsubackReasonCode;

let unsuback = mqtt::packet::v5_0::Unsuback::builder()
    .packet_id(42u16)
    .reason_codes(vec![UnsubackReasonCode::Success])
    .build()
    .unwrap();

// Use trait methods
let size = unsuback.size();
let buffers = unsuback.to_buffers();
Source§

impl<PacketIdType> GenericPacketTrait for mqtt_protocol_core::mqtt::packet::v5_0::GenericUnsubscribe<PacketIdType>
where PacketIdType: IsPacketId,

Generic packet trait implementation for UNSUBSCRIBE packets

Provides common packet operations required by the MQTT protocol framework. This trait allows UNSUBSCRIBE packets to be used polymorphically with other packet types in the protocol implementation.