diff --git a/src/lib.rs b/src/lib.rs index 17273fe..1b7e111 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,7 +18,7 @@ macro_rules! format_args { pub use crate::{ args::{pub_exports::*, Arguments}, - formatter::Formatter, + formatter::{DebugList, DebugMap, DebugSet, DebugStruct, DebugTuple, Formatter}, opts::FmtOpts, }; @@ -114,3 +114,10 @@ mod tests { assert_eq!(result, "a: {6}"); } } + +pub mod uwu { + fn test_format_debug_hex() { + assert_eq!(format!("{:02x?}", b"Foo\0"), "[46, 6f, 6f, 00]"); + assert_eq!(format!("{:02X?}", b"Foo\0"), "[46, 6F, 6F, 00]"); + } +} diff --git a/src/opts.rs b/src/opts.rs index 2266aa9..760c422 100644 --- a/src/opts.rs +++ b/src/opts.rs @@ -165,5 +165,5 @@ options!( struct WithDebugLowerHex { true } fn debug_upper_hex(&self) -> bool { false } - struct WithDebugUpperHex { false } + struct WithDebugUpperHex { true } ); diff --git a/src/write.rs b/src/write.rs index b5a0631..ac27f9c 100644 --- a/src/write.rs +++ b/src/write.rs @@ -1,4 +1,4 @@ -use crate::{Result, Write}; +use crate::{Error, Result, Write}; impl Write for &mut W { fn write_str(&mut self, str: &str) -> Result { @@ -10,12 +10,21 @@ impl Write for &mut W { } } +/// Write is implemented for `&mut [u8]` by copying into the slice, overwriting +/// its data. +/// +/// Note that writing updates the slice to point to the yet unwritten part. +/// The slice will be empty when it has been completely overwritten. impl Write for &'_ mut [u8] { fn write_str(&mut self, str: &str) -> Result { let data = str.as_bytes(); - let amt = core::cmp::min(str.len(), self.len()); - let (a, b) = core::mem::replace(self, &mut []).split_at_mut(amt); - a.copy_from_slice(&data[..amt]); + + if data.len() > self.len() { + return Err(Error); + } + + let (a, b) = core::mem::replace(self, &mut []).split_at_mut(data.len()); + a.copy_from_slice(data); *self = b; Ok(()) } @@ -72,25 +81,23 @@ mod std_impls { net, process, }; - use crate::Result; - - trait IoWriteForwad: IoWrite { - fn write_str(&mut self, str: &str) -> Result { - ::write_all(self, str.as_bytes()).map_err(|_| crate::Error) - } - - fn write_char(&mut self, char: char) -> Result { - let mut buf = [0; 4]; - - ::write_all(self, char.encode_utf8(&mut buf).as_bytes()) - .map_err(|_| crate::Error) - } - } + use crate::{Result, Write}; macro_rules! impl_io_forward { ($($name:ty),* $(,)?) => { $( - impl IoWriteForwad for $name {} + impl Write for $name { + fn write_str(&mut self, str: &str) -> Result { + ::write_all(self, str.as_bytes()).map_err(|_| crate::Error) + } + + fn write_char(&mut self, char: char) -> Result { + let mut buf = [0; 4]; + + ::write_all(self, char.encode_utf8(&mut buf).as_bytes()) + .map_err(|_| crate::Error) + } + } )* }; }