use std::cmp::Ordering;
use std::fmt;
use std::mem::size_of;
use std::ops::Deref;
use crate::codec::SketchBytes;
use crate::codec::SketchSlice;
use crate::codec::assert::insufficient_data;
use crate::error::Error;
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, PartialOrd)]
pub struct ReqFloat<T>(T);
impl<T> ReqFloat<T> {
#[inline(always)]
pub fn into_inner(self) -> T {
self.0
}
}
impl<T> Deref for ReqFloat<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T: fmt::Debug> fmt::Debug for ReqFloat<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl<T: fmt::Display> fmt::Display for ReqFloat<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl ReqFloat<f32> {
#[inline(always)]
pub fn new(value: f32) -> Result<Self, Error> {
if value.is_nan() {
Err(Error::invalid_argument("REQ float must not be NaN"))
} else {
Ok(Self(value))
}
}
}
impl Eq for ReqFloat<f32> {}
impl Ord for ReqFloat<f32> {
#[inline(always)]
fn cmp(&self, other: &Self) -> Ordering {
self.0.partial_cmp(&other.0).unwrap()
}
}
impl ReqFloat<f64> {
#[inline(always)]
pub fn new(value: f64) -> Result<Self, Error> {
if value.is_nan() {
Err(Error::invalid_argument("REQ float must not be NaN"))
} else {
Ok(Self(value))
}
}
}
impl Eq for ReqFloat<f64> {}
impl Ord for ReqFloat<f64> {
#[inline(always)]
fn cmp(&self, other: &Self) -> Ordering {
self.0.partial_cmp(&other.0).unwrap()
}
}
pub trait ReqValue: Sized {
fn serialize_size(item: &Self) -> usize;
fn serialize_value(&self, bytes: &mut SketchBytes);
fn deserialize_value(cursor: &mut SketchSlice<'_>) -> Result<Self, Error>;
}
impl ReqValue for i32 {
fn serialize_size(_item: &Self) -> usize {
size_of::<Self>()
}
fn serialize_value(&self, bytes: &mut SketchBytes) {
bytes.write_i32_le(*self);
}
fn deserialize_value(cursor: &mut SketchSlice<'_>) -> Result<Self, Error> {
cursor
.read_i32_le()
.map_err(insufficient_data("failed to read i32 from REQ sketch"))
}
}
impl ReqValue for i64 {
fn serialize_size(_item: &Self) -> usize {
size_of::<Self>()
}
fn serialize_value(&self, bytes: &mut SketchBytes) {
bytes.write_i64_le(*self);
}
fn deserialize_value(cursor: &mut SketchSlice<'_>) -> Result<Self, Error> {
cursor
.read_i64_le()
.map_err(insufficient_data("failed to read i64 from REQ sketch"))
}
}
impl ReqValue for u32 {
fn serialize_size(_item: &Self) -> usize {
size_of::<Self>()
}
fn serialize_value(&self, bytes: &mut SketchBytes) {
bytes.write_u32_le(*self);
}
fn deserialize_value(cursor: &mut SketchSlice<'_>) -> Result<Self, Error> {
cursor
.read_u32_le()
.map_err(insufficient_data("failed to read u32 from REQ sketch"))
}
}
impl ReqValue for u64 {
fn serialize_size(_item: &Self) -> usize {
size_of::<Self>()
}
fn serialize_value(&self, bytes: &mut SketchBytes) {
bytes.write_u64_le(*self);
}
fn deserialize_value(cursor: &mut SketchSlice<'_>) -> Result<Self, Error> {
cursor
.read_u64_le()
.map_err(insufficient_data("failed to read u64 from REQ sketch"))
}
}
impl ReqValue for ReqFloat<f32> {
fn serialize_size(_item: &Self) -> usize {
size_of::<Self>()
}
fn serialize_value(&self, bytes: &mut SketchBytes) {
bytes.write_f32_le(self.0);
}
fn deserialize_value(cursor: &mut SketchSlice<'_>) -> Result<Self, Error> {
let value = cursor
.read_f32_le()
.map_err(insufficient_data("failed to read f32 from REQ sketch"))?;
Self::new(value).map_err(|_| Error::deserial("REQ float must not be NaN"))
}
}
impl ReqValue for ReqFloat<f64> {
fn serialize_size(_item: &Self) -> usize {
size_of::<Self>()
}
fn serialize_value(&self, bytes: &mut SketchBytes) {
bytes.write_f64_le(self.0);
}
fn deserialize_value(cursor: &mut SketchSlice<'_>) -> Result<Self, Error> {
let value = cursor
.read_f64_le()
.map_err(insufficient_data("failed to read f64 from REQ sketch"))?;
Self::new(value).map_err(|_| Error::deserial("REQ float must not be NaN"))
}
}