use chrono::{DateTime, Utc};
pub trait ExtendBytes {
fn append_raw_bytes(&self, buf: &mut Vec<u8>);
}
impl<T> ExtendBytes for Vec<T>
where T: ExtendBytes
{
fn append_raw_bytes(&self, buf: &mut Vec<u8>) {
for t in self {
t.append_raw_bytes(buf);
}
}
}
impl<T> ExtendBytes for [T]
where T: ExtendBytes
{
fn append_raw_bytes(&self, buf: &mut Vec<u8>) {
for t in self {
t.append_raw_bytes(buf);
}
}
}
impl ExtendBytes for str {
fn append_raw_bytes(&self, buf: &mut Vec<u8>) {
buf.extend(self.as_bytes())
}
}
impl ExtendBytes for &str {
fn append_raw_bytes(&self, buf: &mut Vec<u8>) {
buf.extend(self.as_bytes())
}
}
impl ExtendBytes for String {
fn append_raw_bytes(&self, buf: &mut Vec<u8>) {
buf.extend(self.as_bytes())
}
}
impl ExtendBytes for i8 {
fn append_raw_bytes(&self, buf: &mut Vec<u8>) {
let bytes = self.to_le_bytes();
buf.extend_from_slice(&bytes);
}
}
impl ExtendBytes for i16 {
fn append_raw_bytes(&self, buf: &mut Vec<u8>) {
let bytes = self.to_le_bytes();
buf.extend_from_slice(&bytes);
}
}
impl ExtendBytes for i32 {
fn append_raw_bytes(&self, buf: &mut Vec<u8>) {
let bytes = self.to_le_bytes();
buf.extend_from_slice(&bytes);
}
}
impl ExtendBytes for i128 {
fn append_raw_bytes(&self, buf: &mut Vec<u8>) {
let bytes = self.to_le_bytes();
buf.extend_from_slice(&bytes);
}
}
impl ExtendBytes for u8 {
fn append_raw_bytes(&self, buf: &mut Vec<u8>) {
let bytes = self.to_le_bytes();
buf.extend_from_slice(&bytes);
}
}
impl ExtendBytes for u16 {
fn append_raw_bytes(&self, buf: &mut Vec<u8>) {
let bytes = self.to_le_bytes();
buf.extend_from_slice(&bytes);
}
}
impl ExtendBytes for u32 {
fn append_raw_bytes(&self, buf: &mut Vec<u8>) {
let bytes = self.to_le_bytes();
buf.extend_from_slice(&bytes);
}
}
impl ExtendBytes for u64 {
fn append_raw_bytes(&self, buf: &mut Vec<u8>) {
let bytes = self.to_le_bytes();
buf.extend_from_slice(&bytes);
}
}
impl ExtendBytes for u128 {
fn append_raw_bytes(&self, buf: &mut Vec<u8>) {
let bytes = self.to_le_bytes();
buf.extend_from_slice(&bytes);
}
}
impl ExtendBytes for bool {
fn append_raw_bytes(&self, buf: &mut Vec<u8>) {
buf.extend_from_slice(if *self { &[1u8] } else { &[0u8] });
}
}
impl ExtendBytes for DateTime<Utc> {
fn append_raw_bytes(&self, buf: &mut Vec<u8>) {
let bytes = self.timestamp().to_le_bytes();
buf.extend_from_slice(&bytes);
}
}