Trait SignedInteger

Source
pub trait SignedInteger: Numeric {
    type Unsigned: UnsignedInteger<Signed = Self>;

    // Required methods
    fn is_negative(self) -> bool;
    fn as_non_negative(self) -> Self::Unsigned;
    fn as_negative(self, bits: u32) -> Self::Unsigned;
    fn as_negative_fixed<const BITS: u32>(self) -> Self::Unsigned;
}
Expand description

This trait extends many common signed integer types so that they can be used with the bitstream handling traits.

This trait was formerly named SignedNumeric in 2.X.X code. If backwards-compatibility is needed one can import SignedInteger as SignedNumeric.

Required Associated Types§

Source

type Unsigned: UnsignedInteger<Signed = Self>

The unsigned variant of ourself

Required Methods§

Source

fn is_negative(self) -> bool

Returns true if this value is negative

§Example
use bitstream_io::SignedInteger;
assert!(!1i8.is_negative());
assert!((-1i8).is_negative());
Source

fn as_non_negative(self) -> Self::Unsigned

Returns ourself as a non-negative value. The location of the sign bit depends on the stream’s endianness and is not stored in the result.

§Example
use bitstream_io::SignedInteger;
assert_eq!(1i8.as_non_negative(), 0b00000001u8);
Source

fn as_negative(self, bits: u32) -> Self::Unsigned

Given a negative value and a certain number of bits, returns this value as a twos-complement positive number. The location of the sign bit depends on the stream’s endianness and is not stored in the result.

§Example
use bitstream_io::SignedInteger;
assert_eq!((-1i8).as_negative(8), 0b01111111u8);
Source

fn as_negative_fixed<const BITS: u32>(self) -> Self::Unsigned

Given a negative value and a certain number of bits, returns this value as a twos-complement positive number.

§Example
use bitstream_io::SignedInteger;
assert_eq!((-1i8).as_negative_fixed::<8>(), 0b01111111u8);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl SignedInteger for i8

Source§

type Unsigned = u8

Source§

fn is_negative(self) -> bool

Source§

fn as_non_negative(self) -> Self::Unsigned

Source§

fn as_negative(self, bits: u32) -> Self::Unsigned

Source§

fn as_negative_fixed<const BITS: u32>(self) -> Self::Unsigned

Source§

impl SignedInteger for i16

Source§

type Unsigned = u16

Source§

fn is_negative(self) -> bool

Source§

fn as_non_negative(self) -> Self::Unsigned

Source§

fn as_negative(self, bits: u32) -> Self::Unsigned

Source§

fn as_negative_fixed<const BITS: u32>(self) -> Self::Unsigned

Source§

impl SignedInteger for i32

Source§

type Unsigned = u32

Source§

fn is_negative(self) -> bool

Source§

fn as_non_negative(self) -> Self::Unsigned

Source§

fn as_negative(self, bits: u32) -> Self::Unsigned

Source§

fn as_negative_fixed<const BITS: u32>(self) -> Self::Unsigned

Source§

impl SignedInteger for i64

Source§

type Unsigned = u64

Source§

fn is_negative(self) -> bool

Source§

fn as_non_negative(self) -> Self::Unsigned

Source§

fn as_negative(self, bits: u32) -> Self::Unsigned

Source§

fn as_negative_fixed<const BITS: u32>(self) -> Self::Unsigned

Source§

impl SignedInteger for i128

Source§

type Unsigned = u128

Source§

fn is_negative(self) -> bool

Source§

fn as_non_negative(self) -> Self::Unsigned

Source§

fn as_negative(self, bits: u32) -> Self::Unsigned

Source§

fn as_negative_fixed<const BITS: u32>(self) -> Self::Unsigned

Implementors§