From 82088715b454d8cf197b9c54c31525ca0cb57a05 Mon Sep 17 00:00:00 2001 From: JChoy <jchoyeclipse@gmail.com> Date: Sat, 31 Aug 2024 04:02:38 +0900 Subject: [PATCH] feat: implement Display trait for i257 (#327) <!--- Please provide a general summary of your changes in the title above --> ## Pull Request type <!-- Please try to limit your pull request to one type; submit multiple pull requests if needed. --> Please check the type of change your PR introduces: - [ ] Bugfix - [x] Feature - [ ] Code style update (formatting, renaming) - [ ] Refactoring (no functional changes, no API changes) - [ ] Build-related changes - [ ] Documentation content changes - [ ] Other (please describe): ## What is the current behavior? <!-- Please describe the current behavior that you are modifying, or link to a relevant issue. --> Issue Number: N/A ## What is the new behavior? <!-- Please describe the behavior or changes that are being added by this PR. --> - Implemented Display trait for `i257` ## Does this introduce a breaking change? - [ ] Yes - [x] No <!-- If this does introduce a breaking change, please describe the impact and migration path for existing applications below. --> ## Other information <!-- Any other information that is important to this PR, such as screenshots of how the component looks before and after the change. --> --- packages/math/src/i257.cairo | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/packages/math/src/i257.cairo b/packages/math/src/i257.cairo index 694f2cac..325c5796 100644 --- a/packages/math/src/i257.cairo +++ b/packages/math/src/i257.cairo @@ -1,3 +1,4 @@ +use core::fmt::{Display, Formatter, Error}; use core::num::traits::Zero; use core::ops::{AddAssign, SubAssign, MulAssign, DivAssign, RemAssign}; @@ -353,3 +354,14 @@ impl FeltIntoI257 of Into<felt252, i257> { i257 { abs: self.into(), is_negative: false } } } + +// Implements the Display trait for i257. +pub impl DisplayI257Impl of Display<i257> { + fn fmt(self: @i257, ref f: Formatter) -> Result<(), Error> { + if *self.is_negative { + write!(f, "-")?; + } + self.abs.fmt(ref f) + } +} +