pub trait SignedVint: Into<i64> + Copy {
    // Provided methods
    fn as_signed_vint(&self) -> Result<Vec<u8>, ToolError> { ... }
    fn as_signed_vint_with_length(
        &self,
        length: usize
    ) -> Result<Vec<u8>, ToolError> { ... }
}
Expand description

Trait to enable easy serialization to a signed vint.

This is only available for types that can be cast as i64. A signed vint can be written as a variable number of bytes just like a regular vint, but the value portion of the vint is expressed in two’s complement notation.

For example, the decimal number “-33” would be written as [0xDF = 1101 1111]. This value is determined by first taking the two’s complement of 33 [0x21 = 0010 0001] but only using the bits available for the vint value. In this case, that is 7 bits (because the vint marker takes up the 8th bit). The two’s complement is [101 1111]. A handy calculator for two’s complement can be found here. Once the two’s complement has been found, simply prepend the vint marker as usual to get [1101 1111 = 0xDF].

Some more examples:

use ebml_iterable::tools::SignedVint;

assert_eq!(vec![0xDF], (-33i64).as_signed_vint().unwrap());
assert_eq!(vec![0x40, 0xC8], (200i64).as_signed_vint().unwrap());
assert_eq!(vec![0x7F, 0x38], (-200i64).as_signed_vint().unwrap());
assert_eq!(vec![0xFF], (-1i64).as_signed_vint().unwrap());

Provided Methods§

source

fn as_signed_vint(&self) -> Result<Vec<u8>, ToolError>

Returns a representation of the current value as a vint array.

§Errors

This can return an error if the value is outside of the range that can be represented as a vint.

source

fn as_signed_vint_with_length( &self, length: usize ) -> Result<Vec<u8>, ToolError>

Returns a representation of the current value as a vint array with a specified length.

§Errors

This can return an error if the value is outside of the range that can be represented as a vint.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl SignedVint for i8

source§

impl SignedVint for i16

source§

impl SignedVint for i32

source§

impl SignedVint for i64

Implementors§