Sha256: 624f0c60361f61c62876c9cedbae28ba2996954a6fcb43a470db4e2e3b24c807

Contents?: true

Size: 1.13 KB

Versions: 4

Compression:

Stored size: 1.13 KB

Contents

/// Helper trait used to add `signed()` methods to primitive unsigned integer
/// types.
///
/// The purpose of this trait is to signal the intent that the sign bit of an
/// unsigned integer is intended to be discarded and the value is instead
/// understood to be a "bag of bits" where the conversion to a signed number
/// is intended to be lossless bit-wise. This can be used for example when
/// converting an unsigned integer into a signed integer for constrained reasons
/// outside the scope of the code in question.
pub trait Signed {
    /// The signed integer for this type which has the same width.
    type Signed;

    /// View this unsigned integer as a signed integer of the same width.
    ///
    /// All bits are preserved.
    fn signed(self) -> Self::Signed;
}

macro_rules! impls {
    ($($unsigned:ident => $signed:ident)*) => {$(
        impl Signed for $unsigned {
            type Signed = $signed;

            #[inline]
            fn signed(self) -> $signed {
                self as $signed
            }
        }
    )*}
}

impls! {
    u8 => i8
    u16 => i16
    u32 => i32
    u64 => i64
    u128 => i128
    usize => isize
}

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
wasmtime-29.0.0 ./ext/cargo-vendor/cranelift-entity-0.116.1/src/signed.rs
wasmtime-28.0.0 ./ext/cargo-vendor/cranelift-entity-0.115.0/src/signed.rs
wasmtime-27.0.0 ./ext/cargo-vendor/cranelift-entity-0.114.0/src/signed.rs
wasmtime-26.0.0 ./ext/cargo-vendor/cranelift-entity-0.113.0/src/signed.rs