Skip to content

Commit 305eed8

Browse files
Merge pull request #6 from rodrimati1992/assertions
0.2.6 release
2 parents eb312b1 + b4de355 commit 305eed8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+299
-216
lines changed

Changelog.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@ This is the changelog,summarising changes in each version(some minor changes may
22

33
# 0.2
44

5+
### 0.2.6
6+
7+
Made the macros in `const_format` usable when the crate is renamed.
8+
9+
Added a `#[cdeb(crate = "foo")]` helper attribute to
10+
pass the path to `const_format` to `ConstDebug`, useful when reexporting the derive macro.
11+
12+
Documented that `writec!(buff, "{foo}")` (where `foo` is a local variable) works,
13+
and is equivelent to `writec!(buff, "{}", foo)`.
14+
515
### 0.2.5
616

717
Added the "assert" cargo feature,

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,18 @@ assert_eq!(const_format::concatcp!(1 + 1, 2 + 1), "23");
237237

238238
None right now.
239239

240+
# Renaming crate
241+
242+
All function-like macros from `const_format` can be used when the crate is renamed.
243+
244+
The [`ConstDebug`] derive macro has the `#[cdeb(crate = "foo::bar")]` attribute to
245+
tell it where to find the `const_format` crate.
246+
247+
Example of renaming the `const_format` crate in the Cargo.toml file:
248+
```toml
249+
cfmt = {version = "0.*", package = "const_format"}
250+
```
251+
240252
# Cargo features
241253

242254
- "fmt": Enables the [`std::fmt`]-like API,

const_format/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "const_format"
3-
version = "0.2.5"
3+
version = "0.2.6"
44
authors = ["rodrimati1992 <rodrimatt1985@gmail.com>"]
55
edition = "2018"
66
license = "Zlib"
@@ -34,7 +34,7 @@ only_new_tests = ["testing"]
3434
all = ["fmt", "derive", "constant_time_as_str", "assert"]
3535

3636
[dependencies.const_format_proc_macros]
37-
version = "=0.2.5"
37+
version = "=0.2.6"
3838
path = "../const_format_proc_macros"
3939

4040
[dev-dependencies]

const_format/src/const_debug_derive.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@
5151
/// When this attribute is used it disables the default implementation
5252
/// that uses the type parameters generically.
5353
///
54+
/// ### `#[cdeb(crate = "foo::bar")]`
55+
///
56+
/// The path to the `const_format` crate, useful if you want to reexport the ConstDebug macro,
57+
/// or rename the const_format crate in the Cargo.toml .
58+
///
59+
/// Example of renaming the `const_format` crate in the Cargo.toml file:
60+
/// ```toml
61+
/// cfmt = {version = "0.*", package = "const_format"}
62+
/// ```
63+
///
5464
/// Example:
5565
///
5666
/// ```rust
@@ -298,5 +308,36 @@
298308
/// [`impls attribute`]: #cdebimpls
299309
///
300310
///
311+
///
312+
///
313+
/// ### Renamed import
314+
///
315+
/// This example demonstrates that you can use all the macros when the `const_format`
316+
/// crate is renamed.
317+
///
318+
/// ```rust
319+
/// #![feature(const_mut_refs)]
320+
/// # extern crate self as const_format;
321+
/// # extern crate const_format as cfmt;
322+
/// # fn main() {
323+
/// use cfmt::{
324+
/// for_examples::Unit,
325+
/// ConstDebug, formatc,
326+
/// };
327+
///
328+
/// #[derive(ConstDebug)]
329+
/// #[cdeb(crate = "cfmt")]
330+
/// struct Foo {
331+
/// bar: &'static str,
332+
/// baz: Unit
333+
/// }
334+
///
335+
/// const TEXT: &str = formatc!("{:?}", Foo{ bar: "hello", baz: Unit });
336+
///
337+
/// assert_eq!(TEXT, r#"Foo { bar: "hello", baz: Unit }"#);
338+
///
339+
/// # }
340+
/// ```
341+
///
301342
#[cfg(feature = "derive")]
302343
pub use const_format_proc_macros::ConstDebug;

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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@
205205
//! So `#[doc = "foobar"]` cannot be replaced with `#[doc = concatcp!("foo", "bar") ]`.
206206
//!
207207
//! <span id="integer-args"></span>
208+
//!
208209
//! ### Integer arguments
209210
//!
210211
//! Integer arguments must have a type inferrable from context.
@@ -224,6 +225,18 @@
224225
//! assert_eq!(const_format::concatcp!(1 + 1, 2 + 1), "23");
225226
//! ```
226227
//!
228+
//! # Renaming crate
229+
//!
230+
//! All function-like macros from `const_format` can be used when the crate is renamed.
231+
//!
232+
//! The [`ConstDebug`] derive macro has the `#[cdeb(crate = "foo::bar")]` attribute to
233+
//! tell it where to find the `const_format` crate.
234+
//!
235+
//! Example of renaming the `const_format` crate in the Cargo.toml file:
236+
//! ```toml
237+
//! cfmt = {version = "0.*", package = "const_format"}
238+
//! ```
239+
//!
227240
//! # Cargo features
228241
//!
229242
//! - "fmt": Enables the [`std::fmt`]-like API,

const_format/src/macros/assertions.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,12 @@ with_shared_docs! {
141141
macro_rules! assertc {
142142
($cond:expr $(, $fmt_literal:expr $(,$fmt_arg:expr)*)? $(,)? ) => (
143143
const _: () = {
144+
use $crate::__cf_osRcTFl4A;
145+
144146
const PANIC_IF_TRUE_NHPMWYD3NJA: bool = !($cond);
145147

146-
const MSG_NHPMWYD3NJA: &str = $crate::assertc!(
147-
@fmt_string
148-
($crate),
148+
const MSG_NHPMWYD3NJA: &str = $crate::pmr::__formatc_if_impl!(
149+
(PANIC_IF_TRUE_NHPMWYD3NJA),
149150
(concat!(
150151
"{SEP_NHPMWYD3NJA}\
151152
module_path: {MODULE_NHPMWYD3NJA}\n\
@@ -169,14 +170,6 @@ with_shared_docs! {
169170
$crate::assert_with_str!(PANIC_IF_TRUE_NHPMWYD3NJA, MSG_NHPMWYD3NJA);
170171
};
171172
);
172-
(@fmt_string ($path:path), ($fmt_literal:expr) $(,$($eveything:tt)*)? ) => (
173-
$crate::pmr::__formatc_if_impl!(
174-
(($path))
175-
(PANIC_IF_TRUE_NHPMWYD3NJA),
176-
($fmt_literal),
177-
$($($eveything)*)?
178-
)
179-
);
180173
}
181174
}
182175

@@ -574,16 +567,16 @@ macro_rules! __assertc_equality_inner {
574567
),
575568
$($($fmt_arg,)*)?
576569
fmt_NHPMWYD3NJA = |__cf_fmt| {
577-
use $crate::try_ as __cf_try;
578-
use $crate::coerce_to_fmt as __cf_coerce_to_fmt;
570+
use __cf_osRcTFl4A::try_ as __cf_try;
571+
use __cf_osRcTFl4A::coerce_to_fmt as __cf_coerce_to_fmt;
579572

580573
#[allow(irrefutable_let_patterns)]
581574
if let __cf_respan_to!(($left) [ref __cf_left, ref __cf_right]) =
582575
[$left, $right]
583576
{__cf_respan_to!{
584577
($left)
585578
let __cf_fmt = &mut __cf_fmt
586-
.make_formatter($crate::FormattingFlags::__A_REG);
579+
.make_formatter(__cf_osRcTFl4A::FormattingFlags::__A_REG);
587580

588581
__cf_try!(__cf_fmt.write_str(" left: `"));
589582
__cf_try!(__cf_coerce_to_fmt!(__cf_left).const_debug_fmt(__cf_fmt));

const_format/src/macros/fmt_macros.rs

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,11 @@
5454
macro_rules! concatcp {
5555
()=>{""};
5656
($($arg: expr),* $(,)?)=>({
57-
$crate::concatcp!(@inner (($crate)), $( ( $arg ), )* )
58-
});
59-
(@inner (($path:path)) $($everything:tt)* ) => (
57+
use $crate::__cf_osRcTFl4A;
6058
$crate::pmr::__concatcp_impl!{
61-
(($path))
62-
$($everything)*
59+
$( ( $arg ), )*
6360
}
64-
);
61+
});
6562
}
6663

6764
#[doc(hidden)]
@@ -219,20 +216,14 @@ macro_rules! __concatcp_inner {
219216
///
220217
#[macro_export]
221218
macro_rules! formatcp {
222-
($format_string:expr $( $(, $expr:expr )+ )? $(,)? ) => (
223-
$crate::formatcp!(
224-
@inner
225-
(($crate))
219+
($format_string:expr $( $(, $expr:expr )+ )? $(,)? ) => ({
220+
use $crate::__cf_osRcTFl4A;
221+
222+
$crate::pmr::__formatcp_impl!(
226223
($format_string)
227224
$(, $(($expr),)+)?
228225
)
229-
);
230-
(@inner (($path:path)) $($everything:tt)* ) => (
231-
$crate::pmr::__formatcp_impl!(
232-
(($path))
233-
$($everything)*
234-
)
235-
);
226+
});
236227
}
237228

238229
////////////////////////////////////////////////////////////////////////////////
@@ -287,9 +278,11 @@ macro_rules! formatcp {
287278
#[macro_export]
288279
macro_rules! concatc {
289280
()=>{""};
290-
($($anything:tt)*)=>(
281+
($($anything:tt)*)=>({
282+
use $crate::__cf_osRcTFl4A;
283+
291284
$crate::__concatc_expr!(($($anything)*) ($($anything)*))
292-
)
285+
})
293286
}
294287

295288
#[doc(hidden)]
@@ -322,11 +315,7 @@ macro_rules! __concatc_expr {
322315
#[macro_export]
323316
macro_rules! __concatc_inner{
324317
($debug_fmt_fn:ident, $cond:expr, $($span:tt)*)=>{{
325-
use $crate::__cf_osRcTFl4A;
326-
327318
$crate::pmr::respan_to!{($($span)*) {
328-
329-
330319
const fn len_nhpmwyd3nj() -> usize {
331320
if $cond {
332321
let mut strlen = __cf_osRcTFl4A::pmr::ComputeStrLength::new();
@@ -455,18 +444,12 @@ macro_rules! __concatc_inner{
455444
#[macro_export]
456445
#[cfg(feature = "fmt")]
457446
macro_rules! formatc {
458-
($format_string:expr $( $(, $expr:expr )+ )? $(,)? ) => (
459-
$crate::formatc!(
460-
@inner
461-
(($crate))
447+
($format_string:expr $( $(, $expr:expr )+ )? $(,)? ) => ({
448+
use $crate::__cf_osRcTFl4A;
449+
450+
$crate::pmr::__formatc_impl!{
462451
($format_string)
463452
$(, $(($expr),)+)?
464-
)
465-
);
466-
(@inner (($path:path)) $($everything:tt)* ) => ({
467-
$crate::pmr::__formatc_impl!{
468-
(($path))
469-
$($everything)*
470453
}
471454
});
472455
}
@@ -491,7 +474,7 @@ macro_rules! formatc {
491474
/// ```
492475
///
493476
/// The syntax is otherwise the same as described in
494-
/// [the const_format::fmt module](./fmt/index.html#fmtsyntax).
477+
/// [the `const_format::fmt` module](./fmt/index.html#fmtsyntax).
495478
///
496479
/// # Writers
497480
///
@@ -624,6 +607,31 @@ macro_rules! formatc {
624607
/// # Ok::<(), const_format::Error>(())
625608
/// ```
626609
///
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+
///
627635
/// [`Formatter`]: ./fmt/struct.Formatter.html
628636
/// [`WriteMarker`]: ./marker_traits/trait.WriteMarker.html
629637
///
@@ -633,19 +641,13 @@ macro_rules! formatc {
633641
#[macro_export]
634642
#[cfg(feature = "fmt")]
635643
macro_rules! writec {
636-
( $writer:expr, $format_string:expr $( $(, $expr:expr )+ )? $(,)? ) => (
637-
$crate::writec!(
638-
@inner
639-
(($crate))
644+
( $writer:expr, $format_string:expr $( $(, $expr:expr )+ )? $(,)? ) => ({
645+
use $crate::__cf_osRcTFl4A;
646+
647+
$crate::pmr::__writec_impl!{
640648
($writer)
641649
($format_string)
642650
$(, $(($expr),)+)?
643-
)
644-
);
645-
(@inner (($path:path)) $($everything:tt)* ) => ({
646-
$crate::pmr::__writec_impl!{
647-
(($path))
648-
$($everything)*
649651
}
650652
});
651653
}

const_format/tests/fmt_tests/display_formatting.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use const_format::{
1+
use cfmt_a::{
22
fmt::{ComputeStrLength, Error, Formatter, FormattingFlags, StrWriter},
33
try_,
44
wrapper_types::PWrapper,
@@ -145,8 +145,8 @@ macro_rules! unwrap_opt {
145145

146146
#[test]
147147
fn display_fmt_other_types() {
148-
const fn inner(fmt: &mut Formatter<'_>) -> const_format::Result {
149-
const_format::writec!(
148+
const fn inner(fmt: &mut Formatter<'_>) -> cfmt_a::Result {
149+
cfmt_a::writec!(
150150
fmt,
151151
concat!("{},{};", "{},{};{},{};{},{};{},{};",),
152152
false,

const_format/tests/fmt_tests/formatted_writing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use const_format::{
1+
use cfmt_a::{
22
__for_range,
33
fmt::{ComputeStrLength, Error, Formatter, FormattingFlags, StrWriter},
44
try_,

0 commit comments

Comments
 (0)