This commit is contained in:
nora 2022-09-25 13:58:29 +02:00
parent 6fc05446fb
commit 8a57db3409
3 changed files with 35 additions and 21 deletions

View file

@ -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]");
}
}

View file

@ -165,5 +165,5 @@ options!(
struct WithDebugLowerHex { true }
fn debug_upper_hex(&self) -> bool { false }
struct WithDebugUpperHex { false }
struct WithDebugUpperHex { true }
);

View file

@ -1,4 +1,4 @@
use crate::{Result, Write};
use crate::{Error, Result, Write};
impl<W: Write> Write for &mut W {
fn write_str(&mut self, str: &str) -> Result {
@ -10,12 +10,21 @@ impl<W: Write> 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 {
<Self as IoWrite>::write_all(self, str.as_bytes()).map_err(|_| crate::Error)
}
fn write_char(&mut self, char: char) -> Result {
let mut buf = [0; 4];
<Self as IoWrite>::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 {
<Self as IoWrite>::write_all(self, str.as_bytes()).map_err(|_| crate::Error)
}
fn write_char(&mut self, char: char) -> Result {
let mut buf = [0; 4];
<Self as IoWrite>::write_all(self, char.encode_utf8(&mut buf).as_bytes())
.map_err(|_| crate::Error)
}
}
)*
};
}