use std::cmp::Ordering;
use std::fmt::{self, Debug, Formatter};
#[repr(transparent)]
#[derive(Copy)]
#[derive_const(Clone)]
pub struct Sr(u16);
impl Sr {
pub const BITS: u32 = 16;
#[inline(always)]
#[must_use]
pub const fn new() -> Self {
Self::from_u16(0b0000_0000_0000_0000)
}
#[inline]
#[must_use]
pub const fn from_u16(mut value: u16) -> Self {
value &= 0b1111_1111_1111_1111;
Self(value)
}
#[inline(always)]
#[must_use]
pub const fn as_u16(self) -> u16 {
self.0
}
}
impl Debug for Sr {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str("%")?;
for index in (0..Self::BITS).rev() {
const DIGITS: [&str; 2] = [
".",
"1",
];
let digit = self.as_u16() >> index & 0b1;
let digit = DIGITS[usize::from(digit)];
f.write_str(digit)?;
}
Ok(())
}
}
const impl Default for Sr {
#[inline(always)]
fn default() -> Self {
const { Self::new() }
}
}
const impl Eq for Sr {}
const impl Ord for Sr {
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
self.as_u16().cmp(&other.as_u16())
}
}
const impl PartialEq for Sr {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.as_u16().eq(&other.as_u16())
}
}
const impl PartialOrd for Sr {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}