From 8d1342c831863a3b9a76d00324cb926becfb5ccf Mon Sep 17 00:00:00 2001 From: Phani Pavan K Date: Tue, 4 Mar 2025 09:37:35 +0530 Subject: [PATCH] completed final 04 traits exercise --- exercises/04_traits/14_outro/src/lib.rs | 85 ++++++++++++++++++++++ exercises/05_ticket_v2/00_intro/src/lib.rs | 2 +- 2 files changed, 86 insertions(+), 1 deletion(-) diff --git a/exercises/04_traits/14_outro/src/lib.rs b/exercises/04_traits/14_outro/src/lib.rs index 547fd94..1bbfa16 100644 --- a/exercises/04_traits/14_outro/src/lib.rs +++ b/exercises/04_traits/14_outro/src/lib.rs @@ -8,3 +8,88 @@ // 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. + +use std::ops::Add; + +#[derive(Debug, Clone, Copy)] +pub struct SaturatingU16 { + value: u16, +} + +impl From for SaturatingU16 { + fn from(value: u16) -> Self { + return SaturatingU16 { value }; + } +} + +impl From 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 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 for SaturatingU16 { + fn eq(&self, other: &u16) -> bool { + self.value == other.clone() + } +} diff --git a/exercises/05_ticket_v2/00_intro/src/lib.rs b/exercises/05_ticket_v2/00_intro/src/lib.rs index ce1f75f..ff614d1 100644 --- a/exercises/05_ticket_v2/00_intro/src/lib.rs +++ b/exercises/05_ticket_v2/00_intro/src/lib.rs @@ -1,6 +1,6 @@ fn intro() -> &'static str { // TODO: fix me 👇 - "I'm ready to __!" + "I'm ready to refine the `Ticket` type!" } #[cfg(test)]