Take references to input

This commit is contained in:
nora 2022-10-02 12:24:07 +02:00
parent d4ac7596da
commit 61814d9a6d
No known key found for this signature in database
5 changed files with 27 additions and 11 deletions

View file

@ -76,7 +76,7 @@ impl ToTokens for Scoped<'_, Format<'_>> {
tokens.extend(quote! { tokens.extend(quote! {
#[allow(unused_parens)] #[allow(unused_parens)]
match (#(#args),*) { match (#(&#args),*) {
(#(#idents),*) => ( (#(#idents),*) => (
#(#parts),* #(#parts),*
) )
@ -111,16 +111,19 @@ impl ToTokens for Scoped<'_, FormatArg<'_>> {
let current_position = self.current_position.get(); let current_position = self.current_position.get();
self.current_position.set(current_position + 1); 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 Some(FormatArgRef::Named(name)) => self
.input .input
.named_args .named_args
.iter() .iter()
.find(|(arg, _)| arg == name) .find(|(arg, _)| arg == name)
.map(|(name, _)| named_arg_ident(name)) .map(|(name, _)| named_arg_ident(name).to_token_stream())
.unwrap_or_else(|| Ident::new(name, self.input.format_str.span())), .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)); let opt_ty = opt_ty_tokens(self.scope(&self.inner.format_spec.formatter_args));

View file

@ -50,13 +50,13 @@ impl Arguments for Str {
macro_rules! traits { macro_rules! traits {
($($(#[$no_reference_blanket_impl:ident])? struct $arg_name:ident: trait $trait:ident);* $(;)?) => { ($($(#[$no_reference_blanket_impl:ident])? struct $arg_name:ident: trait $trait:ident);* $(;)?) => {
$( $(
pub struct $arg_name<T, O>(pub T, pub O); pub struct $arg_name<'a, T: ?Sized, O>(pub &'a T, pub O);
pub trait $trait { pub trait $trait {
fn fmt<W: Write, O: FmtOpts>(&self, f: &mut Formatter<W, O>) -> Result; fn fmt<W: Write, O: FmtOpts>(&self, f: &mut Formatter<W, O>) -> Result;
} }
impl<T: $trait, O: FmtOpts> Arguments for $arg_name<T, O> { impl<T: $trait + ?Sized, O: FmtOpts> Arguments for $arg_name<'_, T, O> {
fn fmt<W: Write, OldOpts: FmtOpts>(&self, f: &mut Formatter<W, OldOpts>) -> Result { fn fmt<W: Write, OldOpts: FmtOpts>(&self, f: &mut Formatter<W, OldOpts>) -> Result {
let mut f = f.wrap_with(&self.1); let mut f = f.wrap_with(&self.1);

View file

@ -121,14 +121,11 @@ pub mod uwu {
fn test_expansion() { fn test_expansion() {
let evil = Cell::new(0); let evil = Cell::new(0);
format!( format!(
"{0}{0}{1}{owo}", "{0}{0}",
{ {
evil.set(evil.get() + 1); evil.set(evil.get() + 1);
0 0
}, },
5,
owo = "owo",
); );
// assert_eq!(evil.get(), 1);
} }
} }

View file

@ -1,5 +1,7 @@
//! Integer and floating-point number formatting //! Integer and floating-point number formatting
#![allow(clippy::pedantic)]
use core::{ use core::{
mem::MaybeUninit, mem::MaybeUninit,
ops::{Div, Rem, Sub}, ops::{Div, Rem, Sub},

View file

@ -35,4 +35,18 @@ fn dont_move() {
let string = "uwu".to_string(); let string = "uwu".to_string();
let _ = format!("{string}"); let _ = format!("{string}");
assert_eq!("uwu", 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);
}