Skip to main content

CastSigned

Trait CastSigned 

Source
pub trait CastSigned: Sized {
    type Signed;

    // Required method
    fn cast_signed(self) -> Self::Signed;
}
Expand description

Reinterprets an unsigned integer’s bits as its signed counterpart.

For value-checked conversion to the signed counterpart, use CheckedCast / SaturatingCast / StrictCast with the signed target type.

Required Associated Types§

Source

type Signed

The signed counterpart type (e.g. i32 for u32).

Required Methods§

Source

fn cast_signed(self) -> Self::Signed

Returns the bit pattern of self reinterpreted as its signed counterpart (two’s complement: values above Signed::MAX become negative).

use const_num_traits::CastSigned;

assert_eq!(CastSigned::cast_signed(255u8), -1i8);
assert_eq!(CastSigned::cast_signed(127u8), 127i8);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl CastSigned for u8

Source§

impl CastSigned for u16

Source§

impl CastSigned for u32

Source§

impl CastSigned for u64

Source§

impl CastSigned for u128

Source§

impl CastSigned for usize

Implementors§