#[cfg(feature = "std")]
use crate::DotF64;
use crate::{SumF32, SumF64};
pub trait Mergeable: Clone {
fn merge(&mut self, other: &Self);
fn count(&self) -> u64;
#[cfg(feature = "std")]
fn encode(&self) -> Vec<u8>;
#[cfg(feature = "std")]
fn decode(bytes: &[u8]) -> Option<Self>
where
Self: Sized;
}
impl Mergeable for SumF64 {
fn merge(&mut self, other: &Self) {
SumF64::merge(self, other);
}
fn count(&self) -> u64 {
SumF64::count(self)
}
#[cfg(feature = "std")]
fn encode(&self) -> Vec<u8> {
self.to_bytes().to_vec()
}
#[cfg(feature = "std")]
fn decode(bytes: &[u8]) -> Option<Self> {
let arr: &[u8; SumF64::BYTES] = bytes.try_into().ok()?;
SumF64::from_bytes(arr)
}
}
impl Mergeable for SumF32 {
fn merge(&mut self, other: &Self) {
SumF32::merge(self, other);
}
fn count(&self) -> u64 {
SumF32::count(self)
}
#[cfg(feature = "std")]
fn encode(&self) -> Vec<u8> {
self.to_bytes().to_vec()
}
#[cfg(feature = "std")]
fn decode(bytes: &[u8]) -> Option<Self> {
let arr: &[u8; SumF32::BYTES] = bytes.try_into().ok()?;
SumF32::from_bytes(arr)
}
}
#[cfg(feature = "std")]
impl Mergeable for DotF64 {
fn merge(&mut self, other: &Self) {
DotF64::merge(self, other);
}
fn count(&self) -> u64 {
self.pairs()
}
fn encode(&self) -> Vec<u8> {
self.to_bytes().to_vec()
}
fn decode(bytes: &[u8]) -> Option<Self> {
let arr: &[u8; SumF64::BYTES + 1] = bytes.try_into().ok()?;
DotF64::from_bytes(arr)
}
}