pub trait LinExpConversion<C: Debug + PartialEq + Copy + Clone>: Into<C> + Copy + Clone {
    const NUM_MANT_BITS: u8;
    const NUM_EXP_BITS: u8;
    const EXP_INCR: u32 = 3u32;
    const MANT_BITMASK: u32 = _;
    const EXP_BITMASK: u32 = _;
    const SWITCHPOINT: u32 = _;
    const MANT_PREFIX: u32 = _;
    const MAX_VALUE: u32 = _;

    // Required method
    fn lossy_try_from(value: C) -> Result<Self, MldError>;

    // Provided methods
    fn to_expanded(code: u16) -> u32 { ... }
    fn lossy_try_from_expanded(value: u32) -> Result<u16, MldError> { ... }
    fn exact_try_from(value: C) -> Result<Self, MldError>
       where Self: Sized { ... }
}
Expand description

The trait converts a code to a floating point value: in a linear fashion up to [SWITCHPOINT] and then using a floating point representation to allow the conversion of larger values. In MLD and IGMP there are different codes that follow this pattern, e.g. QQIC, ResponseDelay (RFC 3376 section 4.1, RFC 3810 section 5.1), which all convert a code with the following underlying structure:

  0    NUM_EXP_BITS       NUM_MANT_BITS
 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 |X|      exp      |          mant         |
 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

This trait simplifies the implementation by providing methods to perform the conversion.

Required Associated Constants§

source

const NUM_MANT_BITS: u8

Number of bits used for the mantissa.

source

const NUM_EXP_BITS: u8

Number of bits used for the exponent.

Provided Associated Constants§

source

const EXP_INCR: u32 = 3u32

How much the exponent needs to be incremented when performing the exponential conversion.

source

const MANT_BITMASK: u32 = _

Bitmask for the mantissa.

source

const EXP_BITMASK: u32 = _

Bitmask for the exponent.

source

const SWITCHPOINT: u32 = _

First value for which we start the exponential conversion.

source

const MANT_PREFIX: u32 = _

Prefix for capturing the mantissa.

source

const MAX_VALUE: u32 = _

Maximum value the code supports.

Required Methods§

source

fn lossy_try_from(value: C) -> Result<Self, MldError>

Perform a lossy conversion from the C type. Not all values in C can be exactly represented using the code and they will be rounded to a code that represents a value close the provided one.

Provided Methods§

source

fn to_expanded(code: u16) -> u32

Converts the provided code to a value: in a linear way until Self::SWITCHPOINT and using a floating representation for larger values.

source

fn lossy_try_from_expanded(value: u32) -> Result<u16, MldError>

Performs a lossy conversion from [value].

The function will always succeed for values within the valid range. However, the code might not exactly represent the provided input. E.g. a value of MAX_VALUE - 1 cannot be exactly represented with a corresponding code, due the exponential representation. However, the function will be able to provide a code representing a value close to the provided one.

If stronger guarantees are needed consider using [exact_try_from_expanded].

source

fn exact_try_from(value: C) -> Result<Self, MldError>
where Self: Sized,

Performs an exact conversion from [value].

The function will succeed only for values within the valid range that can be exactly represented by the produced code. E.g. a value of FLOATING_POINT_MAX_VALUE - 1 cannot be exactly represented with a corresponding, code due the exponential representation. In this case, the function will return an error.

If a lossy conversion can be tolerated consider using [lossy_try_from_expanded].

Object Safety§

This trait is not object safe.

Implementors§