From df1e20eb23f8c62242acad82d0da683023dc2d78 Mon Sep 17 00:00:00 2001 From: Yongqian Li <67243+yongqli@users.noreply.github.com> Date: Fri, 14 Jan 2022 21:08:49 -0500 Subject: [PATCH] Support formatting parameters in Display implementations and added tests --- src/tinystr16.rs | 2 +- src/tinystr4.rs | 2 +- src/tinystr8.rs | 2 +- tests/main.rs | 3 +++ 4 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/tinystr16.rs b/src/tinystr16.rs index 7d25d6e..df664f1 100644 --- a/src/tinystr16.rs +++ b/src/tinystr16.rs @@ -305,7 +305,7 @@ impl TinyStr16 { impl fmt::Display for TinyStr16 { #[inline(always)] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.deref()) + self.deref().fmt(f) } } diff --git a/src/tinystr4.rs b/src/tinystr4.rs index 57c6612..9a52510 100644 --- a/src/tinystr4.rs +++ b/src/tinystr4.rs @@ -287,7 +287,7 @@ impl TinyStr4 { impl fmt::Display for TinyStr4 { #[inline(always)] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.deref()) + self.deref().fmt(f) } } diff --git a/src/tinystr8.rs b/src/tinystr8.rs index 3440f40..9e8b090 100644 --- a/src/tinystr8.rs +++ b/src/tinystr8.rs @@ -298,7 +298,7 @@ impl TinyStr8 { impl fmt::Display for TinyStr8 { #[inline(always)] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.deref()) + self.deref().fmt(f) } } diff --git a/tests/main.rs b/tests/main.rs index 80f0234..6901058 100644 --- a/tests/main.rs +++ b/tests/main.rs @@ -208,6 +208,7 @@ fn tiny4_display() { write!(result, "{}", s).unwrap(); assert_eq!(result, "abcd"); assert_eq!(format!("{}", s), "abcd"); + assert_eq!(format!("{:>8}", s), format!("{:>8}", result)); } #[test] @@ -388,6 +389,7 @@ fn tiny8_display() { write!(result, "{}", s).unwrap(); assert_eq!(result, "abcdef"); assert_eq!(format!("{}", s), "abcdef"); + assert_eq!(format!("{:>8}", s), format!("{:>8}", result)); } #[test] @@ -586,6 +588,7 @@ fn tiny16_display() { write!(result, "{}", s).unwrap(); assert_eq!(result, "abcdefghijkl"); assert_eq!(format!("{}", s), "abcdefghijkl"); + assert_eq!(format!("{:>14}", s), format!("{:>14}", result)); } #[test]