#macro-derive #osu #traits #bancho #packets #read #derive-debug

bancho-packets-derive

为bancho-packets的BanchoPacket特性提供派生宏

2个不稳定版本

0.2.0 2023年3月20日
0.1.0 2023年1月13日

#30 in #osu


用于 bancho-packets

MIT 协议

9KB
85 代码行

bancho-packets-derive

可用派生

  • ReadPacket: 此派生宏将为结构体实现BanchoPacketRead特性。
  • WritePacket: 此派生宏将为结构体实现BanchoPacketRead特性。
  • PacketLength: 此派生宏将为结构体实现BanchoPacketLength特性。

示例

use bancho_packets::{ReadPacket, PacketReader, PayloadReader};

#[derive(Debug, Clone, ReadPacket)]
/// [`BanchoMessage`] is the message structure of the bancho client.
pub struct BanchoMessage {
    pub sender: String,
    pub content: String,
    pub target: String,
    pub sender_id: i32,
}

// Now we can use [`PayloadReader`] to read the [`BanchoMessage`] from bytes.
let mut reader = PacketReader::new(&[
    1, 0, 0, 20, 0, 0, 0, 11, 0, 11, 6, 228, 189, 160, 229, 165, 189,
    11, 4, 35, 111, 115, 117, 0, 0, 0, 0,
]);
let packet = reader.next().unwrap();

let mut payload_reader = PayloadReader::new(packet.payload.unwrap());
let message = payload_reader.read::<BanchoMessage>();

println!("{:?}: {:?}", packet.id, message);

依赖

~1.5MB
~35K SLoC