JaneDoe-ZS/nap_proto/src/lib.rs

69 lines
1.2 KiB
Rust
Raw Normal View History

2024-07-20 12:13:04 +00:00
pub use proto_gen::XorFields;
use std::collections::HashMap;
pub trait XorFields {
fn xor_fields(&mut self);
}
pub trait CmdID {
const CMD_ID: u16;
fn get_cmd_id(&self) -> u16 {
Self::CMD_ID
}
}
pub trait NapMessage: prost::Message + XorFields + CmdID {}
impl<T: prost::Message + XorFields + CmdID> NapMessage for T {}
include!("../out/_.rs");
include!("../out/bin.rs");
// "repeated bytes"
impl XorFields for Vec<Vec<u8>> {
fn xor_fields(&mut self) {}
}
impl<T> XorFields for Vec<T>
where
T: XorFields,
{
fn xor_fields(&mut self) {
for item in self.iter_mut() {
item.xor_fields();
}
}
}
impl<K, V> XorFields for HashMap<K, V>
where
V: XorFields,
{
fn xor_fields(&mut self) {
for value in self.values_mut() {
value.xor_fields();
}
}
}
impl<T> XorFields for Option<T>
where
T: XorFields,
{
fn xor_fields(&mut self) {
if let Some(value) = self.as_mut() {
value.xor_fields();
}
}
}
impl<T> XorFields for Box<T>
where
T: XorFields,
{
#[allow(unconditional_recursion)]
fn xor_fields(&mut self) {
self.xor_fields();
}
}