mirror of https://git.sr.ht/~stygianentity/bincode
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
This commit is contained in:
parent
5301885cce
commit
4933019c44
|
|
@ -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
|
||||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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!")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue