Trait UnsignedInteger

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

    const MSB_BIT: Self;
    const LSB_BIT: Self;
    const ALL: Self;

    // Required methods
    fn as_non_negative(self) -> Self::Signed;
    fn as_negative(self, bits: u32) -> Self::Signed;
    fn as_negative_fixed<const BITS: u32>(self) -> Self::Signed;
    fn checked_shl(self, rhs: u32) -> Option<Self>;
    fn checked_shr(self, rhs: u32) -> Option<Self>;

    // Provided methods
    fn shl_default(self, rhs: u32) -> Self { ... }
    fn shr_default(self, rhs: u32) -> Self { ... }
}
Expand description

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

Required Associated Constants§

Source

const MSB_BIT: Self

This type’s most-significant bit

Source

const LSB_BIT: Self

This type’s least significant bit

Source

const ALL: Self

This type with all bits set

Required Associated Types§

Source

type Signed: SignedInteger<Unsigned = Self>

The signed variant of ourself

Required Methods§

Source

fn as_non_negative(self) -> Self::Signed

Given a twos-complement value, return this value is a non-negative signed number. The location of the sign bit depends on the stream’s endianness and is not stored in the result.

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

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

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

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

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

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

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

fn checked_shl(self, rhs: u32) -> Option<Self>

Checked shift left

Source

fn checked_shr(self, rhs: u32) -> Option<Self>

Checked shift right

Provided Methods§

Source

fn shl_default(self, rhs: u32) -> Self

Shift left up to our length in bits

If rhs equals our length in bits, returns default

Source

fn shr_default(self, rhs: u32) -> Self

Shift left up to our length in bits

If rhs equals our length in bits, returns zero

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 UnsignedInteger for u8

Source§

const MSB_BIT: Self = 128u8

Source§

const LSB_BIT: Self = 1u8

Source§

const ALL: Self = 255u8

Source§

type Signed = i8

Source§

fn as_non_negative(self) -> Self::Signed

Source§

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

Source§

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

Source§

fn checked_shl(self, rhs: u32) -> Option<Self>

Source§

fn checked_shr(self, rhs: u32) -> Option<Self>

Source§

impl UnsignedInteger for u16

Source§

const MSB_BIT: Self = 32_768u16

Source§

const LSB_BIT: Self = 1u16

Source§

const ALL: Self = 65_535u16

Source§

type Signed = i16

Source§

fn as_non_negative(self) -> Self::Signed

Source§

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

Source§

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

Source§

fn checked_shl(self, rhs: u32) -> Option<Self>

Source§

fn checked_shr(self, rhs: u32) -> Option<Self>

Source§

impl UnsignedInteger for u32

Source§

const MSB_BIT: Self = 2_147_483_648u32

Source§

const LSB_BIT: Self = 1u32

Source§

const ALL: Self = 4_294_967_295u32

Source§

type Signed = i32

Source§

fn as_non_negative(self) -> Self::Signed

Source§

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

Source§

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

Source§

fn checked_shl(self, rhs: u32) -> Option<Self>

Source§

fn checked_shr(self, rhs: u32) -> Option<Self>

Source§

impl UnsignedInteger for u64

Source§

const MSB_BIT: Self = 9_223_372_036_854_775_808u64

Source§

const LSB_BIT: Self = 1u64

Source§

const ALL: Self = 18_446_744_073_709_551_615u64

Source§

type Signed = i64

Source§

fn as_non_negative(self) -> Self::Signed

Source§

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

Source§

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

Source§

fn checked_shl(self, rhs: u32) -> Option<Self>

Source§

fn checked_shr(self, rhs: u32) -> Option<Self>

Source§

impl UnsignedInteger for u128

Source§

const MSB_BIT: Self = 170_141_183_460_469_231_731_687_303_715_884_105_728u128

Source§

const LSB_BIT: Self = 1u128

Source§

const ALL: Self = 340_282_366_920_938_463_463_374_607_431_768_211_455u128

Source§

type Signed = i128

Source§

fn as_non_negative(self) -> Self::Signed

Source§

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

Source§

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

Source§

fn checked_shl(self, rhs: u32) -> Option<Self>

Source§

fn checked_shr(self, rhs: u32) -> Option<Self>

Implementors§