Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compat and BorrowCompat Debug and Display implementations #670

Merged
merged 4 commits into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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!")
}
}