Skip to content

Commit

Permalink
Merge pull request #1 from rodrimati1992/0_1_patch
Browse files Browse the repository at this point in the history
0.1.1 patch
  • Loading branch information
rodrimati1992 authored Nov 2, 2021
2 parents c9679ea + 8bcd89d commit 98eecfe
Show file tree
Hide file tree
Showing 18 changed files with 930 additions and 38 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "const_panic"
version = "0.1.0"
version = "0.1.1"
authors = ["rodrimati1992 <[email protected]>"]
edition = "2021"
license = "Zlib"
Expand Down
93 changes: 93 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
This changelog is a summary of the changes made in each release.

# 0.1

### 0.1.1

Added `PanicFmt`-based formatting for these types(all of which require the `"non_basic"` feature):
- `Option`s of integer, bool, and `&str`
- `Option`s of arrays and slices (of integer, bool, and `&str`)
- `NonZero*` integers, and `Option`s of them
- `NonNull`, and `Option`s of them
- `*const T` and `*mut T`
- `std::cmp::Ordering`, and `Option`s of them
- `std::sync::atomic::Ordering`
- `std::ops::Range*` types, parameterized with `usize`.
- `()`
- `std::marker::PhantomData`
- `std::marker::PhantomPinned`
- `StdWrapper`

Added these macros:
- `unwrap_ok`
- `unwrap_err`
- `unwrap_some`

Fixed signature of to_panicvals for arrays and slices of PanicVals, by adding a `FmtArg` parameter.


### 0.1.0

Defined the `fmt::PanicFmt` trait.

Defined these types in the `fmt` module:
- `ComputePvCount`
- `FmtArg`
- `IsCustomType`
- `IsStdType`
- `IsPanicFMt`
- `Separator`
- `Delimiter`
- `FmtKind`
- `IsLast`
- `TypeDelim`
- `ShortString` (type alias for `ArrayString<16>`)

Defined these constants in the `fmt` module:
- `COMMA_SEP`
- `COMMA_TERM`
- `INDENTATION_STEP`

Re-exported these variants from `fmt::Delimiter` in `fmt`:
- `CloseBrace`
- `CloseBracket`
- `CloseParen`
- `Empty` renamed to `EmptyDelimiter`
- `OpenBrace`
- `OpenBracket`
- `OpenParen`

Reexported these items from `fmt` in the root module:
- `FmtArg`
- `IsCustomType`
- `PanicFmt`
- `ComputePvCount`
- `TypeDelim`


Defined these macros:
- `coerce_fmt`
- `concat_panic`: for panicking with formatted arguments.
- `flatten_panicvals`: for flattening the argument slices of `PanicVal`s into an array.
- `impl_panicfmt`: for user-defined structs and enums to implement `PanicFmt`
- `inline_macro`

Implemented `PanicFmt`-based formatting for:
- All the primitive integer types
- `str`
- `bool`
- Arrays and slices of `PanicVal` integers, `bool`, and `&str`.

Defined the `ArrayString` stack allocated string type.

Defined the `PanicVal` opaque enum used for formatting.

Defined the `StdWrapper` wrapper type for defining methods on `std` types

Defined the `concat_panic` function, for panicking with formatted arguments.

Defined the `"non_basic"` crate feature,
which enables all items for doing more than panicking with primitive types.



22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,28 @@ error[E0080]: evaluation of constant value failed
neither foo nor bar can be zero!
foo: 10
bar: 0', src/lib.rs:8:15
```
When called at runtime
```should_panic
use const_panic::concat_panic;
assert_non_zero(10, 0);
#[track_caller]
const fn assert_non_zero(foo: u32, bar: u32) {
if foo == 0 || bar == 0 {
concat_panic!("\nneither foo nor bar can be zero!\nfoo: ", foo, "\nbar: ", bar)
}
}
```
it prints this:
```text
thread 'main' panicked at '
neither foo nor bar can be zero!
foo: 10
bar: 0', src/lib.rs:6:1
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
```

### Custom types
Expand Down
4 changes: 3 additions & 1 deletion src/concat_panic_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ pub const fn concat_panic(args: &[&[PanicVal<'_>]]) -> ! {
}
}

/// The maximum length of panic messages (in bytes),
/// after which the message is truncated.
// this should probably be smaller on platforms where this
// const fn is called at runtime, and the stack is finy.
const MAX_PANIC_MSG_LEN: usize = 32768;
pub const MAX_PANIC_MSG_LEN: usize = 32768;

macro_rules! write_panicval_to_buffer {
(
Expand Down
17 changes: 15 additions & 2 deletions src/doc_macros.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
macro_rules! formatting_docs {($($additional_fmt_overrides:expr)?) => {
concat!("
concat!(r##"
# Formatting
Literals are Display formatted by default, so that you can pass string literals
without worrying about what the current formatting settings are.
Expressions are formatted as determined by the `$fmtarg` argument.
Note that literals inside parentheses (eg: `(100)`) are considered expressions
by this macro.
### Formatting overrides
You can override how an argument is formatted by prefixing the argument expression with
Expand All @@ -15,8 +18,18 @@ any of the options below:
- `alt_debug:` or `{#?}:`: alternate-`Debug` formats the argument.
- `display:` or `{}:`: `Display` formats the argument.
- `alt_display:` or `{#}:`: alternate-`Display` formats the argument.
",
"##,
$($additional_fmt_overrides,)?
r##"
### String formatting
String expressions are debug-formatted like this:
- Prepending and appending the double quote character (`"`).
- Escaping the `'\t'`,`'\n'`,`'\r'`,`'\\'`, `'\''`, and`'\"'` characters.
- Escaping control characters with `\xYY`,
where `YY` is the hexadecimal value of the control character.
"##
)}}

macro_rules! limitation_docs {() => {
Expand Down
50 changes: 35 additions & 15 deletions src/fmt_impls/basic_fmt_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,38 @@ use crate::{
FmtArg, PanicFmt, StdWrapper,
};

macro_rules! primitive_static_panicfmt {
(
fn[$($impl:tt)*](&$self:ident: $ty:ty, $f:ident) {
$($content:tt)*
}
) => {
impl<$($impl)*> PanicFmt for $ty {
type This = Self;
type Kind = crate::fmt::IsStdType;
const PV_COUNT: usize = 1;
}

impl<$($impl)*> crate::StdWrapper<&$ty> {
#[doc = concat!(
"Converts this `", stringify!($ty), "` to a single-element `PanicVal` array."
)]
pub const fn to_panicvals($self, $f: FmtArg) -> [PanicVal<'static>; 1] {
[{
$($content)*
}]
}

#[doc = concat!(
"Converts this `", stringify!($ty), "` to a `PanicVal`."
)]
pub const fn to_panicval($self, $f: FmtArg) -> PanicVal<'static> {
$($content)*
}
}
}
}

macro_rules! impl_panicfmt_panicval_array {
(
PV_COUNT = $pv_len:expr;
Expand All @@ -18,7 +50,7 @@ macro_rules! impl_panicfmt_panicval_array {

impl<'s, $($impl)*> StdWrapper<&'s $ty> {
///
pub const fn to_panicvals($self: Self) -> $ret {
pub const fn to_panicvals($self: Self, _: FmtArg) -> $ret {
$($content)*
}
}
Expand Down Expand Up @@ -77,20 +109,8 @@ macro_rules! impl_panicfmt_int {
}
}

impl PanicFmt for $ty {
type This = Self;
type Kind = crate::fmt::IsStdType;

const PV_COUNT: usize = 1;
}

impl<'s> StdWrapper<&'s $ty> {
/// Converts this integer to a single-element `PanicVal` array.
pub const fn to_panicvals(self: Self, f: FmtArg) -> [PanicVal<'static>; 1] {
[PanicVal::$panic_arg_ctor(*self.0, f)]
}
/// Converts this integer to a `PanicVal`.
pub const fn to_panicval(self: Self, f: FmtArg) -> PanicVal<'static> {
primitive_static_panicfmt! {
fn[](&self: $ty, f) {
PanicVal::$panic_arg_ctor(*self.0, f)
}
}
Expand Down
87 changes: 87 additions & 0 deletions src/fmt_impls/fmt_range.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use crate::{FmtArg, PanicFmt, PanicVal, StdWrapper};

use core::ops::{Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive};

macro_rules! impl_range_panicfmt_one {
(
fn(&$self:ident: $ty:ty, $f:ident) -> $pv_count:literal {
$($content:tt)*
}
) => {
impl PanicFmt for $ty {
type This = Self;
type Kind = crate::fmt::IsStdType;
const PV_COUNT: usize = $pv_count;
}

impl crate::StdWrapper<&$ty> {
#[doc = concat!(
"Converts this `", stringify!($ty), "` to a single-element `PanicVal` array."
)]
pub const fn to_panicvals($self, $f: FmtArg) -> [PanicVal<'static>; $pv_count] {
$($content)*
}
}
}
}

macro_rules! impl_range_panicfmt {
($elem_ty:ty) => {
impl_range_panicfmt_one! {
fn(&self: Range<$elem_ty>, f) -> 3 {
[
StdWrapper(&self.0.start).to_panicval(f),
PanicVal::write_str(".."),
StdWrapper(&self.0.end).to_panicval(f),
]
}
}

impl_range_panicfmt_one! {
fn(&self: RangeFrom<$elem_ty>, f) -> 2 {
[
StdWrapper(&self.0.start).to_panicval(f),
PanicVal::write_str(".."),
]
}
}

impl_range_panicfmt_one! {
fn(&self: RangeTo<$elem_ty>, f) -> 2 {
[
PanicVal::write_str(".."),
StdWrapper(&self.0.end).to_panicval(f),
]
}
}

impl_range_panicfmt_one! {
fn(&self: RangeToInclusive<$elem_ty>, f) -> 2 {
[
PanicVal::write_str("..="),
StdWrapper(&self.0.end).to_panicval(f),
]
}
}

impl_range_panicfmt_one! {
fn(&self: RangeInclusive<$elem_ty>, f) -> 3 {
[
StdWrapper(self.0.start()).to_panicval(f),
PanicVal::write_str("..="),
StdWrapper(self.0.end()).to_panicval(f),
]
}
}
};
}

impl_range_panicfmt! {usize}

////////////////////////////////////////////////////////////////////////////////

impl_range_panicfmt_one! {
fn(&self: RangeFull, _f) -> 1 {
[PanicVal::write_str("..")]
}
}
37 changes: 37 additions & 0 deletions src/fmt_impls/nonzero_impls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use crate::{FmtArg, PanicFmt, PanicVal};

use core::num::{
NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU128,
NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize,
};

macro_rules! nonzero_impls {
($(($int_ctor:ident, $ty:ty))*) => (
$(
primitive_static_panicfmt!{
fn[](&self: $ty, fmtarg) {
PanicVal::$int_ctor(self.0.get(), fmtarg)
}
}
)*

impl_for_option!{
$((for[], 'static, $ty, $ty))*
}
)
}

nonzero_impls! {
(from_u8, NonZeroU8)
(from_i8, NonZeroI8)
(from_u16, NonZeroU16)
(from_i16, NonZeroI16)
(from_u32, NonZeroU32)
(from_i32, NonZeroI32)
(from_u64, NonZeroU64)
(from_i64, NonZeroI64)
(from_u128, NonZeroU128)
(from_i128, NonZeroI128)
(from_usize, NonZeroUsize)
(from_isize, NonZeroIsize)
}
Loading

0 comments on commit 98eecfe

Please sign in to comment.