Skip to main content

UnsignedInteger

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

Source§

const LSB_BIT: Self = 1

Source§

const ALL: Self = u8::MAX

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

Source§

const LSB_BIT: Self = 1

Source§

const ALL: Self = u16::MAX

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

Source§

const LSB_BIT: Self = 1

Source§

const ALL: Self = u32::MAX

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

Source§

const LSB_BIT: Self = 1

Source§

const ALL: Self = u64::MAX

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

Source§

const LSB_BIT: Self = 1

Source§

const ALL: Self = u128::MAX

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§