Skip to content

Commit

Permalink
Compat and BorrowCompat Debug and Display implementations (#670)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
aegroto committed Oct 21, 2023
1 parent a3ea6b1 commit 73258a7
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
36 changes: 36 additions & 0 deletions src/features/serde/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,24 @@ where
}
}

impl<T> core::fmt::Debug for Compat<T>
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<T> core::fmt::Display for Compat<T>
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
Expand Down Expand Up @@ -252,3 +270,21 @@ where
Ok(())
}
}

impl<T> core::fmt::Debug for BorrowCompat<T>
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<T> core::fmt::Display for BorrowCompat<T>
where
T: core::fmt::Display,
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.0.fmt(f)
}
}
55 changes: 54 additions & 1 deletion tests/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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!")
}
}

0 comments on commit 73258a7

Please sign in to comment.