feat: rust proto implementation

This commit is contained in:
Strix 2023-10-15 18:06:20 +02:00
parent 1e68840042
commit e4b508ae86
No known key found for this signature in database
GPG key ID: 49B2E37B8915B774
12 changed files with 381 additions and 0 deletions

View file

@ -0,0 +1,45 @@
use std::fmt::{Formatter, LowerHex};
#[derive(Debug, Eq, PartialEq)]
pub struct Identifier {
pub bytes: [u8; 4],
}
impl LowerHex for Identifier {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{:x}{:x}{:x}{:x}",
self.bytes[0], self.bytes[1], self.bytes[2], self.bytes[3]
)
}
}
impl From<&[u8]> for Identifier {
fn from(value: &[u8]) -> Self {
let mut bytes = [0_u8; 4];
bytes[..value.len()].copy_from_slice(value);
Identifier {
bytes: [bytes[0], bytes[1], bytes[2], bytes[3]],
}
}
}
impl From<Vec<u8>> for Identifier {
fn from(value: Vec<u8>) -> Self {
Identifier::from(value.as_slice())
}
}
impl From<u32> for Identifier {
fn from(value: u32) -> Self {
Identifier {
bytes: [
(value >> 24) as u8,
(value >> 16) as u8,
(value >> 8) as u8,
(value >> 0) as u8,
],
}
}
}