mirror of
https://github.com/Noratrieb/mono-fmt.git
synced 2026-01-17 00:35:05 +01:00
fix hex
This commit is contained in:
parent
6fc05446fb
commit
8a57db3409
3 changed files with 35 additions and 21 deletions
|
|
@ -18,7 +18,7 @@ macro_rules! format_args {
|
||||||
|
|
||||||
pub use crate::{
|
pub use crate::{
|
||||||
args::{pub_exports::*, Arguments},
|
args::{pub_exports::*, Arguments},
|
||||||
formatter::Formatter,
|
formatter::{DebugList, DebugMap, DebugSet, DebugStruct, DebugTuple, Formatter},
|
||||||
opts::FmtOpts,
|
opts::FmtOpts,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -114,3 +114,10 @@ mod tests {
|
||||||
assert_eq!(result, "a: {6}");
|
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]");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -165,5 +165,5 @@ options!(
|
||||||
struct WithDebugLowerHex { true }
|
struct WithDebugLowerHex { true }
|
||||||
|
|
||||||
fn debug_upper_hex(&self) -> bool { false }
|
fn debug_upper_hex(&self) -> bool { false }
|
||||||
struct WithDebugUpperHex { false }
|
struct WithDebugUpperHex { true }
|
||||||
);
|
);
|
||||||
|
|
|
||||||
29
src/write.rs
29
src/write.rs
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::{Result, Write};
|
use crate::{Error, Result, Write};
|
||||||
|
|
||||||
impl<W: Write> Write for &mut W {
|
impl<W: Write> Write for &mut W {
|
||||||
fn write_str(&mut self, str: &str) -> Result {
|
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] {
|
impl Write for &'_ mut [u8] {
|
||||||
fn write_str(&mut self, str: &str) -> Result {
|
fn write_str(&mut self, str: &str) -> Result {
|
||||||
let data = str.as_bytes();
|
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);
|
if data.len() > self.len() {
|
||||||
a.copy_from_slice(&data[..amt]);
|
return Err(Error);
|
||||||
|
}
|
||||||
|
|
||||||
|
let (a, b) = core::mem::replace(self, &mut []).split_at_mut(data.len());
|
||||||
|
a.copy_from_slice(data);
|
||||||
*self = b;
|
*self = b;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
@ -72,9 +81,12 @@ mod std_impls {
|
||||||
net, process,
|
net, process,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::Result;
|
use crate::{Result, Write};
|
||||||
|
|
||||||
trait IoWriteForwad: IoWrite {
|
macro_rules! impl_io_forward {
|
||||||
|
($($name:ty),* $(,)?) => {
|
||||||
|
$(
|
||||||
|
impl Write for $name {
|
||||||
fn write_str(&mut self, str: &str) -> Result {
|
fn write_str(&mut self, str: &str) -> Result {
|
||||||
<Self as IoWrite>::write_all(self, str.as_bytes()).map_err(|_| crate::Error)
|
<Self as IoWrite>::write_all(self, str.as_bytes()).map_err(|_| crate::Error)
|
||||||
}
|
}
|
||||||
|
|
@ -86,11 +98,6 @@ mod std_impls {
|
||||||
.map_err(|_| crate::Error)
|
.map_err(|_| crate::Error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! impl_io_forward {
|
|
||||||
($($name:ty),* $(,)?) => {
|
|
||||||
$(
|
|
||||||
impl IoWriteForwad for $name {}
|
|
||||||
)*
|
)*
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue