musli_common/int/traits.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
use core::ops::{BitAnd, BitXor, Neg, Shl, Shr};
use musli::Context;
use crate::options::ByteOrder;
use crate::reader::Reader;
use crate::writer::Writer;
/// Trait that encodes common behaviors of unsigned numbers.
pub trait Unsigned:
Copy
+ Shr<u32, Output = Self>
+ Shl<u32, Output = Self>
+ BitXor<Output = Self>
+ BitAnd<Output = Self>
+ Ord
{
/// The number `1` as represented by the current unsigned number.
const ONE: Self;
/// Number of bytes.
const BYTES: u8;
/// Number of bits.
const BITS: u32;
/// The signed representation of this unsigned number.
type Signed: Signed<Unsigned = Self>;
/// Coerce this number bitwise into its signed representation.
fn signed(self) -> Self::Signed;
/// Construct from the first byte.
fn from_byte(byte: u8) -> Self;
/// Coerce into the lowest 8-bits as a byte.
fn as_byte(self) -> u8;
/// Test if this value is smaller than the specified byte.
fn is_smaller_than(self, byte: u8) -> bool;
/// Test if value is zero.
fn is_zero(self) -> bool;
/// Perform a shift-right operation.
fn checked_shr(self, value: u32) -> Option<Self>;
/// Perform a shift-left operation.
fn checked_shl(self, value: u32) -> Option<Self>;
/// Perform a wrapping shift-left operation.
fn wrapping_shl(self, value: u32) -> Self;
/// Perform a checked addition.
fn checked_add(self, value: Self) -> Option<Self>;
/// Perform a wrapping addition.
fn wrapping_add(self, value: Self) -> Self;
}
/// Helper trait for performing I/O over [Unsigned] types.
pub trait UnsignedOps: Unsigned {
/// Write the current byte array to the given writer in little-endian
/// encoding.
fn write_bytes<C, W>(self, cx: &C, writer: W, byte_order: ByteOrder) -> Result<(), C::Error>
where
C: ?Sized + Context,
W: Writer;
/// Read the current value from the reader in little-endian encoding.
fn read_bytes<'de, C, R>(cx: &C, reader: R, byte_order: ByteOrder) -> Result<Self, C::Error>
where
C: ?Sized + Context,
R: Reader<'de>;
}
/// Trait that encodes common behaviors of signed numbers.
pub trait Signed:
Copy
+ Neg<Output = Self>
+ Shr<u32, Output = Self>
+ Shl<u32, Output = Self>
+ BitXor<Output = Self>
{
/// The number of bits in this signed number.
const BITS: u32;
/// The unsigned representation of this number.
type Unsigned: Unsigned<Signed = Self>;
/// Coerce this number bitwise into its unsigned representation.
fn unsigned(self) -> Self::Unsigned;
}
macro_rules! implement {
($signed:ty, $unsigned:ty) => {
impl Signed for $signed {
const BITS: u32 = <$signed>::BITS;
type Unsigned = $unsigned;
fn unsigned(self) -> Self::Unsigned {
self as $unsigned
}
}
impl Unsigned for $unsigned {
const ONE: Self = 1;
const BYTES: u8 = (<$unsigned>::BITS / 8) as u8;
const BITS: u32 = <$signed>::BITS;
type Signed = $signed;
#[inline]
fn signed(self) -> Self::Signed {
self as $signed
}
#[inline]
fn from_byte(byte: u8) -> Self {
byte as $unsigned
}
#[inline]
fn as_byte(self) -> u8 {
self as u8
}
#[inline]
fn is_smaller_than(self, b: u8) -> bool {
self < b as $unsigned
}
#[inline]
fn is_zero(self) -> bool {
self == 0
}
#[inline]
fn checked_shr(self, value: u32) -> Option<Self> {
self.checked_shr(value)
}
#[inline]
fn checked_shl(self, value: u32) -> Option<Self> {
self.checked_shl(value)
}
#[inline]
fn wrapping_shl(self, value: u32) -> Self {
self.wrapping_shl(value)
}
#[inline]
fn checked_add(self, value: Self) -> Option<Self> {
self.checked_add(value)
}
#[inline]
fn wrapping_add(self, value: Self) -> Self {
self.wrapping_add(value)
}
}
};
}
macro_rules! implement_ops {
($signed:ty, $unsigned:ty) => {
implement!($signed, $unsigned);
impl UnsignedOps for $unsigned {
#[inline(always)]
fn write_bytes<C, W>(
self,
cx: &C,
mut writer: W,
byte_order: ByteOrder,
) -> Result<(), C::Error>
where
C: ?Sized + Context,
W: Writer,
{
let bytes = match byte_order {
ByteOrder::NATIVE => self,
_ => <$unsigned>::swap_bytes(self),
};
let bytes = <$unsigned>::to_ne_bytes(bytes);
writer.write_bytes(cx, &bytes)
}
#[inline(always)]
fn read_bytes<'de, C, R>(
cx: &C,
mut reader: R,
byte_order: ByteOrder,
) -> Result<Self, C::Error>
where
C: ?Sized + Context,
R: Reader<'de>,
{
let bytes = reader.read_array(cx)?;
let bytes = <$unsigned>::from_ne_bytes(bytes);
let bytes = match byte_order {
ByteOrder::NATIVE => bytes,
_ => <$unsigned>::swap_bytes(bytes),
};
Ok(bytes)
}
}
};
}
implement_ops!(i8, u8);
implement_ops!(i16, u16);
implement_ops!(i32, u32);
implement_ops!(i64, u64);
implement_ops!(i128, u128);
implement!(isize, usize);