Skip to content

Instantly share code, notes, and snippets.

@xpepermint
Last active October 7, 2021 21:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xpepermint/8204ebe8a65dc7e122cdccb4829cb4b9 to your computer and use it in GitHub Desktop.
Save xpepermint/8204ebe8a65dc7e122cdccb4829cb4b9 to your computer and use it in GitHub Desktop.
Reimplementation of a u32 type in Rust.
use std::ops;
#[derive(Debug, Default, Clone, Copy, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct U32(u32);
impl ops::Add for U32 {
type Output = Self;
fn add(self, rhs: Self) -> Self {
Self(self.0 + rhs.0)
}
}
impl ops::Add<u32> for U32 {
type Output = Self;
fn add(self, rhs: u32) -> Self {
Self(self.0 + rhs)
}
}
impl ops::AddAssign for U32 {
fn add_assign(&mut self, rhs: Self) {
self.0 += rhs.0;
}
}
impl ops::AddAssign<u32> for U32 {
fn add_assign(&mut self, rhs: u32) {
self.0 += rhs;
}
}
impl ops::BitAnd for U32 {
type Output = Self;
fn bitand(self, rhs: Self) -> Self::Output {
Self(self.0 & rhs.0)
}
}
impl ops::BitAnd<u32> for U32 {
type Output = Self;
fn bitand(self, rhs: u32) -> Self::Output {
Self(self.0 & rhs)
}
}
impl ops::BitAndAssign for U32 {
fn bitand_assign(&mut self, rhs: Self) {
self.0 &= rhs.0
}
}
impl ops::BitAndAssign<u32> for U32 {
fn bitand_assign(&mut self, rhs: u32) {
self.0 &= rhs
}
}
impl ops::BitOr for U32 {
type Output = Self;
fn bitor(self, rhs: Self) -> Self::Output {
Self(self.0 | rhs.0)
}
}
impl ops::BitOr<u32> for U32 {
type Output = Self;
fn bitor(self, rhs: u32) -> Self::Output {
Self(self.0 | rhs)
}
}
impl ops::BitOrAssign for U32 {
fn bitor_assign(&mut self, rhs: Self) {
self.0 |= rhs.0
}
}
impl ops::BitOrAssign<u32> for U32 {
fn bitor_assign(&mut self, rhs: u32) {
self.0 |= rhs
}
}
impl ops::BitXor for U32 {
type Output = Self;
fn bitxor(self, rhs: Self) -> Self::Output {
Self(self.0 ^ rhs.0)
}
}
impl ops::BitXor<u32> for U32 {
type Output = Self;
fn bitxor(self, rhs: u32) -> Self::Output {
Self(self.0 ^ rhs)
}
}
impl ops::BitXorAssign for U32 {
fn bitxor_assign(&mut self, rhs: Self) {
self.0 ^= rhs.0
}
}
impl ops::BitXorAssign<u32> for U32 {
fn bitxor_assign(&mut self, rhs: u32) {
self.0 ^= rhs
}
}
impl ops::Div for U32 {
type Output = Self;
fn div(self, rhs: Self) -> Self {
Self(self.0 / rhs.0)
}
}
impl ops::Div<u32> for U32 {
type Output = Self;
fn div(self, rhs: u32) -> Self {
Self(self.0 / rhs)
}
}
impl ops::DivAssign for U32 {
fn div_assign(&mut self, rhs: Self) {
self.0 /= rhs.0;
}
}
impl ops::DivAssign<u32> for U32 {
fn div_assign(&mut self, rhs: u32) {
self.0 /= rhs;
}
}
impl ops::Mul for U32 {
type Output = Self;
fn mul(self, rhs: Self) -> Self {
Self(self.0 * rhs.0)
}
}
impl ops::Mul<u32> for U32 {
type Output = Self;
fn mul(self, rhs: u32) -> Self {
Self(self.0 * rhs)
}
}
impl ops::MulAssign for U32 {
fn mul_assign(&mut self, rhs: Self) {
self.0 *= rhs.0;
}
}
impl ops::MulAssign<u32> for U32 {
fn mul_assign(&mut self, rhs: u32) {
self.0 *= rhs;
}
}
impl ops::Not for U32 {
type Output = Self;
fn not(self) -> Self::Output {
Self(!self.0)
}
}
impl ops::Shl for U32 {
type Output = Self;
fn shl(self, rhs: Self) -> Self::Output {
Self(self.0 << rhs.0)
}
}
impl ops::Shl<u32> for U32 {
type Output = Self;
fn shl(self, rhs: u32) -> Self::Output {
Self(self.0 << rhs)
}
}
impl ops::ShlAssign for U32 {
fn shl_assign(&mut self, rhs: Self) {
self.0 <<= rhs.0;
}
}
impl ops::ShlAssign<u32> for U32 {
fn shl_assign(&mut self, rhs: u32) {
self.0 <<= rhs;
}
}
impl ops::Shr for U32 {
type Output = Self;
fn shr(self, rhs: Self) -> Self::Output {
Self(self.0 >> rhs.0)
}
}
impl ops::Shr<u32> for U32 {
type Output = Self;
fn shr(self, rhs: u32) -> Self::Output {
Self(self.0 >> rhs)
}
}
impl ops::ShrAssign for U32 {
fn shr_assign(&mut self, rhs: Self) {
self.0 >>= rhs.0;
}
}
impl ops::ShrAssign<u32> for U32 {
fn shr_assign(&mut self, rhs: u32) {
self.0 >>= rhs;
}
}
impl ops::Sub for U32 {
type Output = Self;
fn sub(self, rhs: Self) -> Self {
Self(self.0 - rhs.0)
}
}
impl ops::Sub<u32> for U32 {
type Output = Self;
fn sub(self, rhs: u32) -> Self {
Self(self.0 - rhs)
}
}
impl ops::SubAssign for U32 {
fn sub_assign(&mut self, rhs: Self) {
self.0 = rhs.0;
}
}
impl ops::SubAssign<u32> for U32 {
fn sub_assign(&mut self, rhs: u32) {
self.0 = rhs;
}
}
impl From<u32> for U32 {
fn from(v: u32) -> Self {
Self(v)
}
}
impl From<u16> for U32 {
fn from(v: u16) -> Self {
Self(v as u32)
}
}
impl From<u8> for U32 {
fn from(v: u8) -> Self {
Self(v as u32)
}
}
impl From<U32> for u32 {
fn from(v: U32) -> Self {
v.0
}
}
impl From<U32> for u64 {
fn from(v: U32) -> Self {
v.0 as u64
}
}
impl From<U32> for u128 {
fn from(v: U32) -> Self {
v.0 as u128
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn foo() {
let a = U32(100);
let b = U32(200);
println!("+ : {:?}", a + b);
println!("& : {:?}", b & a);
println!("| : {:?}", b | a);
println!("^ : {:?}", b ^ a);
println!("/: {:?}", b / a);
println!("!: {:?}", !b);
println!("*: {:?}", b * a);
println!("..:{:?}", (a..b).contains(&U32(130)));
println!("<<:{:?}", U32(1) << U32(1));
println!(">>:{:?}", U32(1) >> U32(1));
println!("-: {:?}", b - a);
}
}
pub type U32 = u32;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment