completed final 04 traits exercise

This commit is contained in:
2025-03-04 09:37:35 +05:30
parent 7e998862fa
commit 8d1342c831
2 changed files with 86 additions and 1 deletions

View File

@@ -8,3 +8,88 @@
// It should be possible to print its debug representation. // It should be possible to print its debug representation.
// //
// Tests are located in the `tests` folder—pay attention to the visibility of your types and methods. // Tests are located in the `tests` folder—pay attention to the visibility of your types and methods.
use std::ops::Add;
#[derive(Debug, Clone, Copy)]
pub struct SaturatingU16 {
value: u16,
}
impl From<u16> for SaturatingU16 {
fn from(value: u16) -> Self {
return SaturatingU16 { value };
}
}
impl From<u8> for SaturatingU16 {
fn from(value: u8) -> Self {
return SaturatingU16 {
value: value.into(),
};
}
}
impl From<&u16> for SaturatingU16 {
fn from(value: &u16) -> Self {
return SaturatingU16 {
value: value.clone(),
};
}
}
impl From<&u8> for SaturatingU16 {
fn from(value: &u8) -> Self {
return SaturatingU16 {
value: value.clone().into(),
};
}
}
impl Add for SaturatingU16 {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
SaturatingU16 {
value: self.value.saturating_add(rhs.value),
}
}
}
impl Add<u16> for SaturatingU16 {
type Output = Self;
fn add(self, rhs: u16) -> Self::Output {
SaturatingU16 {
value: self.value.saturating_add(rhs),
}
}
}
impl Add<&u16> for SaturatingU16 {
type Output = Self;
fn add(self, rhs: &u16) -> Self::Output {
SaturatingU16 {
value: self.value.saturating_add(rhs.clone()),
}
}
}
impl Add<&Self> for SaturatingU16 {
type Output = Self;
fn add(self, rhs: &Self) -> Self::Output {
SaturatingU16 {
value: self.value.saturating_add(rhs.value.clone()),
}
}
}
impl PartialEq for SaturatingU16 {
fn eq(&self, other: &Self) -> bool {
self.value == other.value
}
}
impl PartialEq<u16> for SaturatingU16 {
fn eq(&self, other: &u16) -> bool {
self.value == other.clone()
}
}

View File

@@ -1,6 +1,6 @@
fn intro() -> &'static str { fn intro() -> &'static str {
// TODO: fix me 👇 // TODO: fix me 👇
"I'm ready to __!" "I'm ready to refine the `Ticket` type!"
} }
#[cfg(test)] #[cfg(test)]