AggressiveMinimize

Struct AggressiveMinimize 

Source
pub struct AggressiveMinimize;
Expand description

Encode a given numeric value by aggressively minimising its format.

If the float is finite and its fractional part is zero, it first tries to encode it as an integer. If this conversion fails, it falls back to encoding the value as a float.

§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, AggressiveMinimize};

let mut buf = [0_u8;1];
to_slice_with_config(&1_u16, &mut buf, AggressiveMinimize).unwrap();

let expected = [1_u8]; // 1 encoded in `positive fixint`
assert_eq!(buf,expected);

Floating point without fractional part is encoded as int

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

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

let expected = [1_u8]; // 1 encoded in `positive fixint`
assert_eq!(buf,expected);

f64 is encoded in the same way as f32.

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

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

let expected = [1_u8]; // 1 encoded in `positive fixint`
assert_eq!(buf,expected);

Trait Implementations§

Source§

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

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.