Skip to content

Commit 2946cfe

Browse files
Bumped release to 0.2.13. assertion macros now use core::panic (#14)
* Bumped release to 0.2.13. assertion macros now use `core::panic` `core::panic` changed to allow passing a non-literal `&'static str` argument after writing the const_format assertion macros. Updated changelog * Fixed bug caused by use of respan macro * Delete .travis.yml
1 parent 9d0fc38 commit 2946cfe

File tree

9 files changed

+62
-142
lines changed

9 files changed

+62
-142
lines changed

.travis.yml

-62
This file was deleted.

Changelog.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ This is the changelog,summarising changes in each version(some minor changes may
22

33
# 0.2
44

5+
### 0.2.13
6+
7+
Fixed the assertion macros not to use `std::panic`, using `core::panic` instead, since `core::panic` changed to allow passing a non-literal `&'static str` argument.
8+
59
### 0.2.11
610

711
Fixed the documentation in case that the https://github.com/rust-lang/rust/pull/80243

README.md

+2-6
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,8 @@ This implicitly uses the `syn` crate, so clean compiles take a bit longer than w
261261

262262
- "assert": implies the "fmt" feature,
263263
enables the assertion macros.<br>
264-
This is a separate cargo feature because:
265-
- It uses nightly Rust features that are less stable than the "fmt" feature does.<br>
266-
- It requires the `std` crate, because `core::panic` requires a string literal argument.
264+
This is a separate cargo feature because
265+
it uses nightly Rust features that are less stable than the "fmt" feature does.
267266

268267
- "constant_time_as_str": implies the "fmt" feature.
269268
An optimization that requires a few additional nightly features,
@@ -283,9 +282,6 @@ This requires a nightly Rust compiler.
283282

284283
`const_format` is `#![no_std]`, it can be used anywhere Rust can be used.
285284

286-
Caveat: The opt-in "assert" feature uses the `std::panic` macro to panic,
287-
as of 2020-09-06 `core::panic` requires the argument to be a literal.
288-
289285
# Minimum Supported Rust Version
290286

291287
`const_format` requires Rust 1.46.0, because it uses looping an branching in const contexts.

const_format/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "const_format"
3-
version = "0.2.12"
3+
version = "0.2.13"
44
authors = ["rodrimati1992 <[email protected]>"]
55
edition = "2018"
66
license = "Zlib"

const_format/src/lib.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,8 @@
249249
//!
250250
//! - "assert": implies the "fmt" feature,
251251
//! enables the assertion macros.<br>
252-
//! This is a separate cargo feature because:
253-
//! - It uses nightly Rust features that are less stable than the "fmt" feature does.<br>
254-
//! - It requires the `std` crate, because `core::panic` requires a string literal argument.
252+
//! This is a separate cargo feature because
253+
//! it uses nightly Rust features that are less stable than the "fmt" feature does.<br>
255254
//!
256255
//! - "constant_time_as_str": implies the "fmt" feature.
257256
//! An optimization that requires a few additional nightly features,
@@ -339,10 +338,6 @@
339338

340339
include! {"const_debug_derive.rs"}
341340

342-
// Only used for panicking. Once panicking works without std, I'll remove this.
343-
#[cfg(feature = "assert")]
344-
extern crate std;
345-
346341
#[macro_use]
347342
mod macros;
348343

const_format/src/macros/assertions.rs

-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ macro_rules! with_shared_docs {(
1313
///
1414
/// This macro requires the "assert" feature to be exported,
1515
/// because it uses some nightly Rust features.<br>
16-
/// It uses `std::panic` for panicking due to an unforseen limitation
17-
/// in `core::panic`, which doesn't allow passing non-literal strings at compile-time.
1816
///
1917
$(#[$before_syntax])*
2018
/// # Syntax

const_format/src/macros/fmt_macros.rs

+38-48
Original file line numberDiff line numberDiff line change
@@ -322,64 +322,54 @@ macro_rules! __concatc_expr {
322322

323323
#[doc(hidden)]
324324
#[macro_export]
325-
macro_rules! __concatc_inner{
326-
($debug_fmt_fn:ident, $cond:expr, $($span:tt)*)=>{{
327-
$crate::pmr::respan_to!{($($span)*) {
328-
const fn len_nhpmwyd3nj() -> usize {
329-
if $cond {
330-
let mut strlen = __cf_osRcTFl4A::pmr::ComputeStrLength::new();
331-
let fmt = strlen.make_formatter(__cf_osRcTFl4A::FormattingFlags::NEW);
332-
match $debug_fmt_fn(fmt) {
333-
__cf_osRcTFl4A::pmr::Ok(()) => strlen.len(),
334-
__cf_osRcTFl4A::pmr::Err(_) => 0,
335-
}
336-
} else {
337-
0
325+
macro_rules! __concatc_inner {
326+
($debug_fmt_fn:ident, $cond:expr, $($span:tt)*) => {{
327+
const fn len_nhpmwyd3nj() -> usize {
328+
if $cond {
329+
let mut strlen = __cf_osRcTFl4A::pmr::ComputeStrLength::new();
330+
let fmt = strlen.make_formatter(__cf_osRcTFl4A::FormattingFlags::NEW);
331+
match $debug_fmt_fn(fmt) {
332+
__cf_osRcTFl4A::pmr::Ok(()) => strlen.len(),
333+
__cf_osRcTFl4A::pmr::Err(_) => 0,
338334
}
335+
} else {
336+
0
339337
}
338+
}
340339

341-
const LEN_NHPMWYD3NJA: usize = len_nhpmwyd3nj();
340+
const LEN_NHPMWYD3NJA: usize = len_nhpmwyd3nj();
342341

343-
const fn str_writer_nhpmwyd3nja(
344-
)-> __cf_osRcTFl4A::msg::ErrorTupleAndStrWriter<[u8; LEN_NHPMWYD3NJA]> {
345-
let mut writer = __cf_osRcTFl4A::pmr::StrWriter::new([0; LEN_NHPMWYD3NJA]);
346-
let error = if $cond {
347-
$debug_fmt_fn(
348-
__cf_osRcTFl4A::pmr::Formatter::from_sw(
349-
&mut writer,
350-
__cf_osRcTFl4A::FormattingFlags::NEW,
351-
)
352-
)
353-
} else {
354-
__cf_osRcTFl4A::pmr::Ok(())
355-
};
342+
const fn str_writer_nhpmwyd3nja(
343+
) -> __cf_osRcTFl4A::msg::ErrorTupleAndStrWriter<[u8; LEN_NHPMWYD3NJA]> {
344+
let mut writer = __cf_osRcTFl4A::pmr::StrWriter::new([0; LEN_NHPMWYD3NJA]);
345+
let error = if $cond {
346+
$debug_fmt_fn(__cf_osRcTFl4A::pmr::Formatter::from_sw(
347+
&mut writer,
348+
__cf_osRcTFl4A::FormattingFlags::NEW,
349+
))
350+
} else {
351+
__cf_osRcTFl4A::pmr::Ok(())
352+
};
356353

357-
__cf_osRcTFl4A::msg::ErrorTupleAndStrWriter{
358-
error: __cf_osRcTFl4A::msg::ErrorTuple::new(error, &writer),
359-
writer,
360-
}
354+
__cf_osRcTFl4A::msg::ErrorTupleAndStrWriter {
355+
error: __cf_osRcTFl4A::msg::ErrorTuple::new(error, &writer),
356+
writer,
361357
}
358+
}
362359

363-
const STR_WRITER_NHPMWYD3NJA:
364-
&__cf_osRcTFl4A::msg::ErrorTupleAndStrWriter<[u8; LEN_NHPMWYD3NJA]>=
365-
&str_writer_nhpmwyd3nja();
360+
const STR_WRITER_NHPMWYD3NJA: &__cf_osRcTFl4A::msg::ErrorTupleAndStrWriter<
361+
[u8; LEN_NHPMWYD3NJA],
362+
> = &str_writer_nhpmwyd3nja();
366363

367-
const _: __cf_osRcTFl4A::msg::Ok =
368-
<
369-
<
370-
__cf_osRcTFl4A::msg::ErrorPicker<
371-
[(); STR_WRITER_NHPMWYD3NJA.error.error_variant],
372-
[(); STR_WRITER_NHPMWYD3NJA.error.capacity]
373-
>
374-
as __cf_osRcTFl4A::msg::ErrorAsType
375-
>::Type
376-
>::NEW;
364+
const _: __cf_osRcTFl4A::msg::Ok = <<__cf_osRcTFl4A::msg::ErrorPicker<
365+
[(); STR_WRITER_NHPMWYD3NJA.error.error_variant],
366+
[(); STR_WRITER_NHPMWYD3NJA.error.capacity],
367+
> as __cf_osRcTFl4A::msg::ErrorAsType>::Type>::NEW;
377368

378-
const STR_NHPMWYD3NJA: &str =
379-
__cf_osRcTFl4A::strwriter_as_str!(&STR_WRITER_NHPMWYD3NJA.writer);
369+
const STR_NHPMWYD3NJA: &str =
370+
__cf_osRcTFl4A::strwriter_as_str!(&STR_WRITER_NHPMWYD3NJA.writer);
380371

381-
STR_NHPMWYD3NJA
382-
}}
372+
STR_NHPMWYD3NJA
383373
}};
384374
}
385375

const_format/src/panicking.rs

+11-16
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
pub trait Message {
2-
const COND: bool;
3-
const MSG: &'static str;
4-
const PANIC: usize = <Self as PanicIf>::PANIC;
5-
}
6-
7-
pub trait PanicIf: Message {
8-
const PANIC: usize;
9-
}
10-
111
macro_rules! panic_ {
122
($($span:tt)*)=> {crate::pmr::respan_to!{($($span)*)
3+
pub trait Message {
4+
const COND: bool;
5+
const MSG: &'static str;
6+
const PANIC: usize = <Self as PanicIf>::PANIC;
7+
}
8+
9+
pub trait PanicIf: Message {
10+
const PANIC: usize;
11+
}
12+
1313
impl<T> crate::panicking::PanicIf for T
1414
where
1515
T: ?Sized + crate::panicking::Message,
1616
{
1717
const PANIC: usize = {
18-
use ::std::panic as do_not_show_this_code_rustc;
18+
use ::core::panic as do_not_show_this_code_rustc;
1919

2020
let secret = T::MSG;
2121
if T::COND {
@@ -48,10 +48,5 @@ macro_rules! assert_with_str {
4848
}
4949

5050
const _: usize = <__PanicWithStr as $crate::panicking::Message>::PANIC;
51-
52-
// Errors about infinite loops show up above the panic.
53-
if $cond {
54-
loop {}
55-
}
5651
}};
5752
}

print_errors/src/using_assertc_macros.rs

+4
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@ assertc_eq!((), (), "{}");
1616
assertc_eq!((), (), "{}", foo = "", 100u8);
1717

1818
assertc_eq!(0u8, 1u8, "{}", 0);
19+
20+
assertc_eq!(0u8, 1u8, "{}", 0u8);
21+
22+
assertc!(2 + 2 == 5, "{}", 0u8);

0 commit comments

Comments
 (0)