This repository has been archived on 2025-11-23. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
rustex/exercises/04_traits/12_copy/src/lib.rs
2025-03-02 08:27:03 +05:30

44 lines
870 B
Rust

// TODO: implement the necessary traits to make the test compile and pass.
// You *can't* modify the test.
use std::ops::Add;
#[derive(Clone, Copy, Debug)]
pub struct WrappingU32 {
value: u32,
}
impl PartialEq for WrappingU32 {
fn eq(&self, other: &Self) -> bool {
self.value == other.value
}
}
impl Add for WrappingU32 {
type Output = WrappingU32;
fn add(self, rhs: Self) -> Self::Output {
WrappingU32 {
value: self.value.wrapping_add(rhs.value),
}
}
}
impl WrappingU32 {
pub fn new(value: u32) -> Self {
Self { value }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_ops() {
let x = WrappingU32::new(42);
let y = WrappingU32::new(31);
let z = WrappingU32::new(u32::MAX);
assert_eq!(x + y + y + z, WrappingU32::new(103));
}
}