Skip to content

Commit 1fdf953

Browse files
committed
Documented that writec!(buff, "{foo}") works for local variables.
Added test for `writec!(buff, "{foo}")`
1 parent 5aba8b1 commit 1fdf953

File tree

7 files changed

+77
-10
lines changed

7 files changed

+77
-10
lines changed

const_format/src/fmt.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@
5353
//! - Named, from constant (eg: `formatc!("{FOO}")`):
5454
//! Uses the `FOO` constant from the enclosing scope.
5555
//!
56+
//! - Named, from locals (eg: `formatc!("{foo}")`):
57+
//! Uses the `foo` local variable from the enclosing scope,
58+
//! only usable with the [`writec`] macro.
59+
//!
5660
//! ### Formatters
5761
//!
5862
//! The format arguments can be formatted in these ways:
@@ -282,7 +286,7 @@
282286
//! ```
283287
//!
284288
//!
285-
//!
289+
//! [`writec`]: ../macro.writec.html
286290
//! [`Formatter`]: ./struct.Formatter.html
287291
//! [`FormatMarker`]: ../marker_traits/trait.FormatMarker.html
288292
//! [`ConstDebug`]: ../derive.ConstDebug.html

const_format/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,5 +428,3 @@ pub mod pmr {
428428

429429
#[cfg(all(test, not(feature = "testing")))]
430430
compile_error! { "tests must be run with the \"testing\" feature" }
431-
432-
const _: &str = concatcp!(0u8);

const_format/src/macros/fmt_macros.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ macro_rules! formatc {
474474
/// ```
475475
///
476476
/// The syntax is otherwise the same as described in
477-
/// [the const_format::fmt module](./fmt/index.html#fmtsyntax).
477+
/// [the `const_format::fmt` module](./fmt/index.html#fmtsyntax).
478478
///
479479
/// # Writers
480480
///
@@ -607,6 +607,31 @@ macro_rules! formatc {
607607
/// # Ok::<(), const_format::Error>(())
608608
/// ```
609609
///
610+
/// ### Locals in the format string
611+
///
612+
/// This example demonstrates how you can format local variables,
613+
/// by using their identifiers in the format string.
614+
///
615+
/// ```rust
616+
/// #![feature(const_mut_refs)]
617+
///
618+
/// use const_format::{Formatter, FormattingFlags, StrWriter, try_, writec};
619+
///
620+
/// const fn writeit(mut fmt: Formatter<'_>, foo: u32, bar: &str) -> const_format::Result {
621+
/// try_!(writec!(fmt, "{foo},{foo:?},{foo:#x},{foo:#b};"));
622+
/// try_!(writec!(fmt, "{bar},{bar:?}"));
623+
/// Ok(())
624+
/// }
625+
///
626+
/// let writer: &mut StrWriter = &mut StrWriter::new([0; 128]);
627+
///
628+
/// writeit(writer.make_formatter(FormattingFlags::NEW), 100, "hello")?;
629+
///
630+
/// assert_eq!(writer.as_str(), r#"100,100,0x64,0b1100100;hello,"hello""#);
631+
///
632+
/// # Ok::<(), const_format::Error>(())
633+
/// ```
634+
///
610635
/// [`Formatter`]: ./fmt/struct.Formatter.html
611636
/// [`WriteMarker`]: ./marker_traits/trait.WriteMarker.html
612637
///

const_format/tests/misc_tests/writec_macro.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,30 @@ fn named_parameters() {
100100
assert_eq!(writer.as_str(), "8,13,21,34,1000,D,55..89");
101101
}
102102

103+
#[test]
104+
fn write_from_locals() {
105+
const fn inner(f: &mut Formatter<'_>) -> Result<(), Error> {
106+
let foo = 13u8;
107+
let bar = "58";
108+
109+
innerb(f, foo, bar)
110+
}
111+
const fn innerb(f: &mut Formatter<'_>, foo: u8, bar: &str) -> Result<(), Error> {
112+
writec!(
113+
f,
114+
"{foo},{bar},{foo:?},{bar:?},{foo:x},{bar:x},{foo:b},{bar:b}"
115+
)
116+
}
117+
118+
let writer: &mut StrWriter = &mut StrWriter::new([0; 96]);
119+
inner(&mut writer.make_formatter(FormattingFlags::NEW)).unwrap();
120+
assert_eq!(writer.as_str(), r#"13,58,13,"58",D,"58",1101,"58""#);
121+
122+
writer.clear();
123+
inner(&mut writer.make_formatter(FormattingFlags::NEW)).unwrap();
124+
assert_eq!(writer.as_str(), r#"13,58,13,"58",D,"58",1101,"58""#);
125+
}
126+
103127
#[test]
104128
#[cfg(feature = "fmt")]
105129
fn access_formatter() {

const_format/tests/misc_tests_modules.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,23 @@ pub const _ASSERT_NOT_CF: [(); 13] = [(); const_format::NOT_CF];
99

1010
mod misc_tests {
1111
#[cfg(feature = "assert")]
12+
#[cfg(not(feature = "only_new_tests"))]
1213
mod asserts;
1314

1415
#[cfg(feature = "fmt")]
1516
#[cfg(not(feature = "only_new_tests"))]
1617
mod call_debug_fmt_macro;
1718

1819
#[cfg(feature = "fmt")]
20+
#[cfg(not(feature = "only_new_tests"))]
1921
mod concatc_macro_tests;
2022

2123
#[cfg(feature = "derive")]
24+
#[cfg(not(feature = "only_new_tests"))]
2225
mod derive_tests;
2326

2427
#[cfg(feature = "assert")]
28+
#[cfg(not(feature = "only_new_tests"))]
2529
mod equality_tests;
2630

2731
#[cfg(not(feature = "only_new_tests"))]
@@ -31,14 +35,15 @@ mod misc_tests {
3135
#[cfg(not(feature = "only_new_tests"))]
3236
mod impl_fmt_macro_tests;
3337

34-
// #[cfg(not(feature = "only_new_tests"))]
38+
#[cfg(feature = "assert")]
39+
#[cfg(not(feature = "only_new_tests"))]
3540
mod shared_cp_macro_tests;
3641

3742
#[cfg(feature = "fmt")]
3843
#[cfg(not(feature = "only_new_tests"))]
3944
mod type_kind_coercion_macro_tests;
4045

4146
#[cfg(feature = "fmt")]
42-
#[cfg(not(feature = "only_new_tests"))]
47+
//#[cfg(not(feature = "only_new_tests"))]
4348
mod writec_macro;
4449
}

const_format_proc_macros/src/format_args.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use crate::{
22
format_str_parsing::FormatStr, formatting::FormattingFlags, parse_utils::StrRawness,
3-
shared_arg_parsing::ExprArg, spanned::Spans,
3+
parse_utils::TokenStream2Ext, shared_arg_parsing::ExprArg, spanned::Spans,
44
};
55

66
use proc_macro2::{Ident, Span, TokenStream as TokenStream2};
77

8-
use quote::quote_spanned;
8+
use quote::{quote_spanned, TokenStreamExt};
99

1010
////////////////////////////////////////////////
1111

@@ -101,10 +101,15 @@ impl ExpandInto {
101101
let local_variable = &fmted.local_variable;
102102
let span = local_variable.span();
103103

104-
quote_spanned!(span=>
104+
let mut tokens = quote::quote!(
105105
__cf_osRcTFl4A::coerce_to_fmt!(&#local_variable)
106-
.#fmt_method(&mut #formatter.make_formatter(#flags))
106+
.#fmt_method
107107
)
108+
.set_span_recursive(span);
109+
110+
tokens.append_all(quote::quote!( (&mut #formatter.make_formatter(#flags)) ));
111+
112+
tokens
108113
}
109114
ExpandInto::WithFormatter(ExpandWithFormatter {
110115
format,

print_errors/src/using_writec_macro.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,11 @@ fn using_writec(writer: &mut StrWriter) -> cfmt::Result {
1111
// trying to write an uninferred integer type
1212
writec!(writer, "{}", 0)?;
1313

14+
let a = 0;
15+
writec!(writer, "{}", a)?;
16+
writec!(writer, "{b}", b = a)?;
17+
writec!(writer, "{a}")?;
18+
writec!(writer, "{a:?}")?;
19+
1420
Ok(())
1521
}

0 commit comments

Comments
 (0)