From 73258a773595d9c0b8367f8a63968a1e8a528849 Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Sat, 21 Oct 2023 12:53:43 +0200 Subject: [PATCH] Compat and BorrowCompat Debug and Display implementations (#670) * feat: added Display and Debug implementations for Compat and BorrowCompat * chore: added Compat and BorrowCompat Display and Debug tests * chore: fixed imports and linter errors --- src/features/serde/mod.rs | 36 +++++++++++++++++++++++++ tests/serde.rs | 55 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 90 insertions(+), 1 deletion(-) diff --git a/src/features/serde/mod.rs b/src/features/serde/mod.rs index 5777c8f1..7293f476 100644 --- a/src/features/serde/mod.rs +++ b/src/features/serde/mod.rs @@ -216,6 +216,24 @@ where } } +impl core::fmt::Debug for Compat +where + T: core::fmt::Debug, +{ + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + f.debug_tuple("Compat").field(&self.0).finish() + } +} + +impl core::fmt::Display for Compat +where + T: core::fmt::Display, +{ + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + self.0.fmt(f) + } +} + /// Wrapper struct that implements [BorrowDecode] and [Encode] on any type that implements serde's [Deserialize] and [Serialize] respectively. This is mostly used on `&[u8]` and `&str`, for other types consider using [Compat] instead. /// /// [BorrowDecode]: ../de/trait.BorrowDecode.html @@ -252,3 +270,21 @@ where Ok(()) } } + +impl core::fmt::Debug for BorrowCompat +where + T: core::fmt::Debug, +{ + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + f.debug_tuple("BorrowCompat").field(&self.0).finish() + } +} + +impl core::fmt::Display for BorrowCompat +where + T: core::fmt::Display, +{ + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + self.0.fmt(f) + } +} diff --git a/tests/serde.rs b/tests/serde.rs index db6744cf..65864228 100644 --- a/tests/serde.rs +++ b/tests/serde.rs @@ -144,7 +144,10 @@ fn test_serialize_deserialize_owned_data() { #[cfg(feature = "derive")] mod derive { - use bincode::{Decode, Encode}; + use bincode::{ + serde::{BorrowCompat, Compat}, + Decode, Encode, + }; use serde_derive::{Deserialize, Serialize}; #[derive(Serialize, Deserialize, PartialEq, Eq, Debug)] @@ -198,4 +201,54 @@ mod derive { 2, ); } + + #[test] + fn test_vec_compat_debug() { + let compat = Compat(vec![0, 1, 2, 3]); + let debug_view = format!("{:?}", compat); + assert_eq!(debug_view, "Compat([0, 1, 2, 3])") + } + + #[test] + fn test_i32_compat_debug() { + let compat = Compat(1337_i32); + let debug_view = format!("{:?}", compat); + assert_eq!(debug_view, "Compat(1337)") + } + + #[test] + fn test_i32_compat_display() { + let compat = Compat(1337_i32); + let debug_view = format!("{}", compat); + assert_eq!(debug_view, "1337") + } + + #[test] + fn test_f32_compat_display() { + let compat = Compat(1.5_f32); + let debug_view = format!("{}", compat); + assert_eq!(debug_view, "1.5") + } + + #[test] + fn test_vec_borrow_compat_debug() { + let vector = vec![0, 1, 2, 3]; + let borrow_compat = BorrowCompat(&vector); + let debug_view = format!("{:?}", borrow_compat); + assert_eq!(debug_view, "BorrowCompat([0, 1, 2, 3])") + } + + #[test] + fn test_str_borrow_compat_debug() { + let borrow_compat = BorrowCompat("Hello World!"); + let debug_view = format!("{:?}", borrow_compat); + assert_eq!(debug_view, "BorrowCompat(\"Hello World!\")") + } + + #[test] + fn test_str_borrow_compat_display() { + let borrow_compat = BorrowCompat("Hello World!"); + let debug_view = format!("{}", borrow_compat); + assert_eq!(debug_view, "Hello World!") + } }