fix OwnedBytes debug panic (#2512)

This commit is contained in:
baishen
2024-10-16 10:31:40 +08:00
committed by GitHub
parent c17e513377
commit 6dfa2df06f

View File

@@ -151,7 +151,7 @@ impl fmt::Debug for OwnedBytes {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// We truncate the bytes in order to make sure the debug string // We truncate the bytes in order to make sure the debug string
// is not too long. // is not too long.
let bytes_truncated: &[u8] = if self.len() > 8 { let bytes_truncated: &[u8] = if self.len() > 10 {
&self.as_slice()[..10] &self.as_slice()[..10]
} else { } else {
self.as_slice() self.as_slice()
@@ -252,6 +252,11 @@ mod tests {
format!("{short_bytes:?}"), format!("{short_bytes:?}"),
"OwnedBytes([97, 98, 99, 100], len=4)" "OwnedBytes([97, 98, 99, 100], len=4)"
); );
let medium_bytes = OwnedBytes::new(b"abcdefghi".as_ref());
assert_eq!(
format!("{medium_bytes:?}"),
"OwnedBytes([97, 98, 99, 100, 101, 102, 103, 104, 105], len=9)"
);
let long_bytes = OwnedBytes::new(b"abcdefghijklmnopq".as_ref()); let long_bytes = OwnedBytes::new(b"abcdefghijklmnopq".as_ref());
assert_eq!( assert_eq!(
format!("{long_bytes:?}"), format!("{long_bytes:?}"),