Skip to main content

LosslessMinimize

Struct LosslessMinimize 

Source
pub struct LosslessMinimize;
Expand description

Encode a given numeric value in a lossless minimised format without changing its original format.

This encoder minimises the encoded size of a numeric value without any loss of information or change in its inherent type. For integer types, it encodes the value using the smallest integer format that can exactly represent the original value. For floating-point types, it encodes the value using the smallest floating-point format that preserves its precision.

§Examples

If there is no loss in a smaller format, it is encoded with that value

use serde::Serialize;
use messagepack_serde::ser::{to_slice_with_config, LosslessMinimize};

let mut buf = [0_u8;1];
to_slice_with_config(&1_u16, &mut buf, LosslessMinimize).unwrap();
let expected = [1_u8]; // 1 encoded in `positive fixint`
assert_eq!(buf,expected);

Floating point is encoded as floating point type

use serde::Serialize;
use messagepack_serde::ser::{to_slice_with_config, LosslessMinimize};

let mut buf = [0_u8;5];
to_slice_with_config(&1.0_f32, &mut buf, LosslessMinimize).unwrap();

let expected = [0xca,0x3f,0x80,0x00,0x00]; // 1.0 encoded in `float 32`
assert_eq!(buf,expected);

If floating point can be encoded without loss, it will be encoded in a smaller format

use serde::Serialize;
use messagepack_serde::ser::{to_slice_with_config, LosslessMinimize};

let mut buf = [0_u8;5];
to_slice_with_config(&1.0_f64, &mut buf, LosslessMinimize).unwrap();

let expected = [0xca,0x3f,0x80,0x00,0x00]; // 1.0 encoded in `float 32`
assert_eq!(buf,expected);

0.1 is encoded as float 64 due to loss when converted to `f32

use serde::Serialize;
use messagepack_serde::ser::{to_slice_with_config, LosslessMinimize};

let mut buf = [0_u8;9];
to_slice_with_config(&0.1_f64, &mut buf, LosslessMinimize).unwrap();

let expected = [0xcb,0x3f,0xb9,0x99,0x99,0x99,0x99,0x99,0x9a]; // 0.1 encoded in `float 64`
assert_eq!(buf,expected);

Trait Implementations§

Source§

impl<W: IoWrite> NumEncoder<W> for LosslessMinimize

Source§

fn encode_i8( v: i8, writer: &mut W, ) -> Result<usize, Error<<W as IoWrite>::Error>>

decide encode i8
Source§

fn encode_i16( v: i16, writer: &mut W, ) -> Result<usize, Error<<W as IoWrite>::Error>>

decide encode i16
Source§

fn encode_i32( v: i32, writer: &mut W, ) -> Result<usize, Error<<W as IoWrite>::Error>>

decide encode i32
Source§

fn encode_i64( v: i64, writer: &mut W, ) -> Result<usize, Error<<W as IoWrite>::Error>>

decide encode i64
Source§

fn encode_i128( v: i128, writer: &mut W, ) -> Result<usize, Error<<W as IoWrite>::Error>>

decide encode i128
Source§

fn encode_u8( v: u8, writer: &mut W, ) -> Result<usize, Error<<W as IoWrite>::Error>>

decide encode u8
Source§

fn encode_u16( v: u16, writer: &mut W, ) -> Result<usize, Error<<W as IoWrite>::Error>>

decide encode u16
Source§

fn encode_u32( v: u32, writer: &mut W, ) -> Result<usize, Error<<W as IoWrite>::Error>>

decide encode u32
Source§

fn encode_u64( v: u64, writer: &mut W, ) -> Result<usize, Error<<W as IoWrite>::Error>>

decide encode u64
Source§

fn encode_u128( v: u128, writer: &mut W, ) -> Result<usize, Error<<W as IoWrite>::Error>>

decide encode u128
Source§

fn encode_f32( v: f32, writer: &mut W, ) -> Result<usize, Error<<W as IoWrite>::Error>>

decide encode f32
Source§

fn encode_f64( v: f64, writer: &mut W, ) -> Result<usize, Error<<W as IoWrite>::Error>>

decide encode f64

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.