From 61814d9a6d61b4aa61e15e2f2c142b79d3acd89b Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sun, 2 Oct 2022 12:24:07 +0200 Subject: [PATCH] Take references to input --- mono-fmt-macro/src/to_tokens.rs | 13 ++++++++----- src/args.rs | 4 ++-- src/lib.rs | 5 +---- src/rust_core_impl/num.rs | 2 ++ tests/base.rs | 14 ++++++++++++++ 5 files changed, 27 insertions(+), 11 deletions(-) diff --git a/mono-fmt-macro/src/to_tokens.rs b/mono-fmt-macro/src/to_tokens.rs index cbe5e7d..649402a 100644 --- a/mono-fmt-macro/src/to_tokens.rs +++ b/mono-fmt-macro/src/to_tokens.rs @@ -76,7 +76,7 @@ impl ToTokens for Scoped<'_, Format<'_>> { tokens.extend(quote! { #[allow(unused_parens)] - match (#(#args),*) { + match (#(&#args),*) { (#(#idents),*) => ( #(#parts),* ) @@ -111,16 +111,19 @@ impl ToTokens for Scoped<'_, FormatArg<'_>> { let current_position = self.current_position.get(); self.current_position.set(current_position + 1); - pos_arg_ident(current_position) + pos_arg_ident(current_position).to_token_stream() } - Some(FormatArgRef::Positional(idx)) => pos_arg_ident(idx), + Some(FormatArgRef::Positional(idx)) => pos_arg_ident(idx).to_token_stream(), Some(FormatArgRef::Named(name)) => self .input .named_args .iter() .find(|(arg, _)| arg == name) - .map(|(name, _)| named_arg_ident(name)) - .unwrap_or_else(|| Ident::new(name, self.input.format_str.span())), + .map(|(name, _)| named_arg_ident(name).to_token_stream()) + .unwrap_or_else(|| { + let ident = Ident::new(name, self.input.format_str.span()); + quote! { &#ident } + }), }; let opt_ty = opt_ty_tokens(self.scope(&self.inner.format_spec.formatter_args)); diff --git a/src/args.rs b/src/args.rs index c285cbc..5720461 100644 --- a/src/args.rs +++ b/src/args.rs @@ -50,13 +50,13 @@ impl Arguments for Str { macro_rules! traits { ($($(#[$no_reference_blanket_impl:ident])? struct $arg_name:ident: trait $trait:ident);* $(;)?) => { $( - pub struct $arg_name(pub T, pub O); + pub struct $arg_name<'a, T: ?Sized, O>(pub &'a T, pub O); pub trait $trait { fn fmt(&self, f: &mut Formatter) -> Result; } - impl Arguments for $arg_name { + impl Arguments for $arg_name<'_, T, O> { fn fmt(&self, f: &mut Formatter) -> Result { let mut f = f.wrap_with(&self.1); diff --git a/src/lib.rs b/src/lib.rs index 7ab9949..bef05b0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -121,14 +121,11 @@ pub mod uwu { fn test_expansion() { let evil = Cell::new(0); format!( - "{0}{0}{1}{owo}", + "{0}{0}", { evil.set(evil.get() + 1); 0 }, - 5, - owo = "owo", ); - // assert_eq!(evil.get(), 1); } } diff --git a/src/rust_core_impl/num.rs b/src/rust_core_impl/num.rs index e9e5ae4..6101dcb 100644 --- a/src/rust_core_impl/num.rs +++ b/src/rust_core_impl/num.rs @@ -1,5 +1,7 @@ //! Integer and floating-point number formatting +#![allow(clippy::pedantic)] + use core::{ mem::MaybeUninit, ops::{Div, Rem, Sub}, diff --git a/tests/base.rs b/tests/base.rs index c034687..8b8a472 100644 --- a/tests/base.rs +++ b/tests/base.rs @@ -35,4 +35,18 @@ fn dont_move() { let string = "uwu".to_string(); let _ = format!("{string}"); assert_eq!("uwu", string); + + let string = "owo".to_string(); + let _ = format!("{0}{0}", string); + assert_eq!("owo", string); } + + +#[test] +fn ptr_correct_addr() { + static STATIC: u8 = 0; + let addr = std::format!("{:p}", (&STATIC) as *const u8); + let fmt = format!("{:p}", &STATIC); + + assert_eq!(addr, fmt); +} \ No newline at end of file