mirror of
https://github.com/Noratrieb/mono-fmt.git
synced 2026-01-14 07:15:06 +01:00
Cleanups
This commit is contained in:
parent
61814d9a6d
commit
822a85e4ce
8 changed files with 74 additions and 42 deletions
|
|
@ -75,8 +75,7 @@ impl ToTokens for Scoped<'_, Format<'_>> {
|
|||
let idents = pos_idents.chain(named_idents);
|
||||
|
||||
tokens.extend(quote! {
|
||||
#[allow(unused_parens)]
|
||||
match (#(&#args),*) {
|
||||
match { #[allow(unused_parens)] (#(&#args),*) } {
|
||||
(#(#idents),*) => (
|
||||
#(#parts),*
|
||||
)
|
||||
|
|
|
|||
11
src/args.rs
11
src/args.rs
|
|
@ -3,6 +3,12 @@ pub trait Arguments {
|
|||
fn fmt<W: Write, O: FmtOpts>(&self, f: &mut Formatter<W, O>) -> Result;
|
||||
}
|
||||
|
||||
impl<A: Arguments> Arguments for &A {
|
||||
fn fmt<W: Write, O: FmtOpts>(&self, f: &mut Formatter<W, O>) -> Result {
|
||||
<A as Arguments>::fmt(*self, f)
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! tuple_args {
|
||||
() => {};
|
||||
($first:ident $($rest:ident)*) => {
|
||||
|
|
@ -21,6 +27,7 @@ macro_rules! tuple_args {
|
|||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
tuple_args!($($rest)*);
|
||||
};
|
||||
}
|
||||
|
|
@ -31,8 +38,8 @@ tuple_args!(
|
|||
A11 A12 A13 A14 A15 A16 A17 A18 A19 A20
|
||||
A21 A22 A23 A24 A25 A26 A27 A28 A29 A30
|
||||
A31 A32 A33 A34 A35 A36 A37 A38 A39 A40
|
||||
// A41 A42 A43 A44 A45 A46 A47 A48 A49 A50
|
||||
// A51 A52 A53 A54 A55 A56 A57 A58 A59 A60
|
||||
A41 A42 A43 A44 A45 A46 A47 A48 A49 A50
|
||||
A51 A52 A53 A54 A55 A56 A57 A58 A59 A60
|
||||
// A61 A62 A63 A64 A65 A66 A67 A68 A69 A70
|
||||
// A71 A72 A73 A74 A75 A76 A77 A78 A79 A80
|
||||
// A81 A82 A83 A84 A85 A86 A87 A88 A89 A90
|
||||
|
|
|
|||
|
|
@ -29,6 +29,14 @@ impl<W: Write, O: FmtOpts> Formatter<W, O> {
|
|||
self.buf.write_str(str)
|
||||
}
|
||||
|
||||
pub fn debug_struct(&mut self, name: &str) -> DebugStruct<'_, W, O> {
|
||||
debug_struct_new(self, name)
|
||||
}
|
||||
|
||||
pub fn debug_tuple(&mut self, name: &str) -> DebugTuple<'_, W, O> {
|
||||
debug_tuple_new(self, name)
|
||||
}
|
||||
|
||||
pub fn debug_list(&mut self) -> DebugList<'_, W, O> {
|
||||
debug_list_new(self)
|
||||
}
|
||||
|
|
@ -153,7 +161,7 @@ impl<'a, W: Write, O: FmtOpts> DebugStruct<'a, W, O> {
|
|||
self.fmt.write_str(" {\n")?;
|
||||
}
|
||||
let mut slot = None;
|
||||
let mut state = Default::default();
|
||||
let mut state = PadAdapterState::default();
|
||||
let mut writer = PadAdapter::wrap(self.fmt, &mut slot, &mut state);
|
||||
writer.write_str(name)?;
|
||||
writer.write_str(": ")?;
|
||||
|
|
@ -177,7 +185,7 @@ impl<'a, W: Write, O: FmtOpts> DebugStruct<'a, W, O> {
|
|||
if self.has_fields {
|
||||
if self.is_pretty() {
|
||||
let mut slot = None;
|
||||
let mut state = Default::default();
|
||||
let mut state = PadAdapterState::default();
|
||||
let mut writer = PadAdapter::wrap(self.fmt, &mut slot, &mut state);
|
||||
writer.write_str("..\n")?;
|
||||
self.fmt.write_str("}")
|
||||
|
|
@ -239,7 +247,7 @@ impl<'a, W: Write, O: FmtOpts> DebugTuple<'a, W, O> {
|
|||
self.fmt.write_str("(\n")?;
|
||||
}
|
||||
let mut slot = None;
|
||||
let mut state = Default::default();
|
||||
let mut state = PadAdapterState::default();
|
||||
let mut writer = PadAdapter::wrap(self.fmt, &mut slot, &mut state);
|
||||
value.fmt(&mut writer)?;
|
||||
writer.write_str(",\n")
|
||||
|
|
@ -285,13 +293,13 @@ impl<'a, W: Write, O: FmtOpts> DebugInner<'a, W, O> {
|
|||
self.fmt.write_str("\n")?;
|
||||
}
|
||||
let mut slot = None;
|
||||
let mut state = Default::default();
|
||||
let mut state = PadAdapterState::default();
|
||||
let mut writer = PadAdapter::wrap(self.fmt, &mut slot, &mut state);
|
||||
entry.fmt(&mut writer)?;
|
||||
writer.write_str(",\n")
|
||||
} else {
|
||||
if self.has_fields {
|
||||
self.fmt.write_str(", ")?
|
||||
self.fmt.write_str(", ")?;
|
||||
}
|
||||
entry.fmt(self.fmt)
|
||||
}
|
||||
|
|
@ -411,7 +419,7 @@ pub(super) fn debug_map_new<W: Write, O: FmtOpts>(
|
|||
result,
|
||||
has_fields: false,
|
||||
has_key: false,
|
||||
state: Default::default(),
|
||||
state: PadAdapterState::default(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -433,13 +441,13 @@ impl<'a, W: Write, O: FmtOpts> DebugMap<'a, W, O> {
|
|||
self.fmt.write_str("\n")?;
|
||||
}
|
||||
let mut slot = None;
|
||||
self.state = Default::default();
|
||||
self.state = PadAdapterState::default();
|
||||
let mut writer = PadAdapter::wrap(self.fmt, &mut slot, &mut self.state);
|
||||
key.fmt(&mut writer)?;
|
||||
writer.write_str(": ")?;
|
||||
} else {
|
||||
if self.has_fields {
|
||||
self.fmt.write_str(", ")?
|
||||
self.fmt.write_str(", ")?;
|
||||
}
|
||||
key.fmt(self.fmt)?;
|
||||
self.fmt.write_str(": ")?;
|
||||
|
|
|
|||
23
src/lib.rs
23
src/lib.rs
|
|
@ -1,5 +1,12 @@
|
|||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
#![allow(dead_code)]
|
||||
#![warn(clippy::pedantic)]
|
||||
#![allow(
|
||||
clippy::missing_panics_doc,
|
||||
clippy::missing_errors_doc,
|
||||
clippy::wildcard_imports,
|
||||
clippy::module_name_repetitions,
|
||||
clippy::single_match_else,
|
||||
)]
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
|
|
@ -115,17 +122,17 @@ mod tests {
|
|||
}
|
||||
}
|
||||
|
||||
/// this should really not end up in the final code lmao
|
||||
#[doc(hidden)]
|
||||
pub mod uwu {
|
||||
#![allow(dead_code)]
|
||||
use std::cell::Cell;
|
||||
|
||||
fn test_expansion() {
|
||||
let evil = Cell::new(0);
|
||||
format!(
|
||||
"{0}{0}",
|
||||
{
|
||||
evil.set(evil.get() + 1);
|
||||
0
|
||||
},
|
||||
);
|
||||
format!("{0}{0}", {
|
||||
evil.set(evil.get() + 1);
|
||||
0
|
||||
},);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ macro_rules! options {
|
|||
|
||||
/// # Example
|
||||
/// `Self` is `WithAlternate<WithFill<(), ' '>>`
|
||||
/// `Other` is WithMinus<()>
|
||||
/// `Other` is `WithMinus<()>`
|
||||
///
|
||||
/// This returns `WithAlternate<WithFille<WithMinus<()>, ' '>>`
|
||||
///
|
||||
|
|
|
|||
|
|
@ -20,29 +20,33 @@ impl<T: Debug> Debug for [T] {
|
|||
|
||||
// pointers
|
||||
mod pointers {
|
||||
use core::ptr;
|
||||
|
||||
use super::impl_prelude::*;
|
||||
|
||||
impl<T: ?Sized> Pointer for *const T {
|
||||
fn fmt<W: Write, O: FmtOpts>(&self, f: &mut Formatter<W, O>) -> Result {
|
||||
pointer_fmt_inner((*self as *const ()) as usize, f)
|
||||
pointer_fmt_inner((*self).cast::<()>() as usize, f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized> Pointer for *mut T {
|
||||
fn fmt<W: Write, O: FmtOpts>(&self, f: &mut Formatter<W, O>) -> Result {
|
||||
pointer_fmt_inner((*self as *mut ()) as usize, f)
|
||||
pointer_fmt_inner((*self).cast::<()>() as usize, f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized> Pointer for &T {
|
||||
fn fmt<W: Write, O: FmtOpts>(&self, f: &mut Formatter<W, O>) -> Result {
|
||||
Pointer::fmt(&(*self as *const T), f)
|
||||
let ptr: *const T = ptr::addr_of!(**self);
|
||||
pointer_fmt_inner(ptr.cast::<()>() as usize, f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized> Pointer for &mut T {
|
||||
fn fmt<W: Write, O: FmtOpts>(&self, f: &mut Formatter<W, O>) -> Result {
|
||||
Pointer::fmt(&(&**self as *const T), f)
|
||||
let ptr: *const T = ptr::addr_of!(**self);
|
||||
pointer_fmt_inner(ptr.cast::<()>() as usize, f)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
//! Copied modified stuff from core
|
||||
|
||||
#![allow(dead_code)]
|
||||
|
||||
mod aggregated;
|
||||
mod num;
|
||||
|
||||
|
|
@ -135,6 +137,23 @@ impl PostPadding {
|
|||
|
||||
impl<W: Write, O: FmtOpts> Formatter<W, O> {
|
||||
fn pad_integral(&mut self, is_nonnegative: bool, prefix: &str, buf: &str) -> Result {
|
||||
// Writes the sign if it exists, and then the prefix if it was requested
|
||||
#[inline(never)]
|
||||
fn write_prefix<W: Write, O>(
|
||||
f: &mut Formatter<W, O>,
|
||||
sign: Option<char>,
|
||||
prefix: Option<&str>,
|
||||
) -> Result {
|
||||
if let Some(c) = sign {
|
||||
f.buf.write_char(c)?;
|
||||
}
|
||||
if let Some(prefix) = prefix {
|
||||
f.buf.write_str(prefix)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
let mut width = buf.len();
|
||||
|
||||
let mut sign = None;
|
||||
|
|
@ -153,23 +172,6 @@ impl<W: Write, O: FmtOpts> Formatter<W, O> {
|
|||
None
|
||||
};
|
||||
|
||||
// Writes the sign if it exists, and then the prefix if it was requested
|
||||
#[inline(never)]
|
||||
fn write_prefix<W: Write, O>(
|
||||
f: &mut Formatter<W, O>,
|
||||
sign: Option<char>,
|
||||
prefix: Option<&str>,
|
||||
) -> Result {
|
||||
if let Some(c) = sign {
|
||||
f.buf.write_char(c)?;
|
||||
}
|
||||
if let Some(prefix) = prefix {
|
||||
f.buf.write_str(prefix)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
// The `width` field is more of a `min-width` parameter at this point.
|
||||
match self.width() {
|
||||
// If there's no minimum length requirements then we can just
|
||||
|
|
|
|||
|
|
@ -49,4 +49,9 @@ fn ptr_correct_addr() {
|
|||
let fmt = format!("{:p}", &STATIC);
|
||||
|
||||
assert_eq!(addr, fmt);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn temporaries() {
|
||||
let _ = format_args!("{}", { "owo".to_string() });
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue