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§
Sourcetype Unsigned: UnsignedInteger<Signed = Self>
type Unsigned: UnsignedInteger<Signed = Self>
The unsigned variant of ourself
Required Methods§
Sourcefn is_negative(self) -> bool
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());
Sourcefn as_non_negative(self) -> Self::Unsigned
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);
Sourcefn as_negative(self, bits: u32) -> Self::Unsigned
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);
Sourcefn as_negative_fixed<const BITS: u32>(self) -> Self::Unsigned
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.