Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added impls for IntErrorKind/ParseIntError, 0.2.11 release #15

Merged
merged 3 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,23 @@ jobs:
strategy:
max-parallel: 2
matrix:
rust: [stable, beta, nightly, 1.64.0, 1.57.0]
rust: [stable, beta, nightly, 1.82.0, 1.64.0, 1.57.0]

steps:
- uses: actions/checkout@v2
- name: ci-1.64
if: ${{ matrix.rust != '1.57.0' }}
run: |
rustup override set ${{ matrix.rust }}
cargo update
- name: ci-1-82-version-env_vars
if: ${{ matrix.rust == '1.82.0' }}
run: echo "rustv=rust_1_82" >> $GITHUB_ENV

cd "${{github.workspace}}/"
- uses: actions/checkout@v2
- name: ci-1-64-version-env_vars
if: ${{ matrix.rust == '1.64.0' }}
run: echo "rustv=rust_1_64" >> $GITHUB_ENV

cargo test --no-default-features --features "test rust_1_64"
cargo test --no-default-features --features "test rust_1_64 non_basic"
- uses: actions/checkout@v2
- name: ci-stable-or-later-env_vars
if: ${{ matrix.rust == 'stable' || matrix.rust == 'beta' || matrix.rust == 'nightly' }}
run: echo "rustv=rust_latest_stable" >> $GITHUB_ENV

- name: ci-all-versions
run: |
Expand All @@ -39,7 +42,7 @@ jobs:
cargo build --no-default-features
cargo build
cargo test --features "test"
cargo test --no-default-features --features "test "
cargo test --no-default-features --features "test non_basic"
cargo test --no-default-features --features "test non_basic derive"
cargo test --no-default-features --features "test ${{env.rustv}}"
cargo test --no-default-features --features "test ${{env.rustv}} non_basic"
cargo test --no-default-features --features "test ${{env.rustv}} non_basic derive"

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "const_panic"
version = "0.2.10"
version = "0.2.11"
authors = ["rodrimati1992 <[email protected]>"]
edition = "2021"
license = "Zlib"
Expand Down Expand Up @@ -40,6 +40,8 @@ features = ["small_rng"]
default = ["non_basic"]

rust_1_64 = []
rust_1_82 = ["rust_1_64"]
rust_latest_stable = ["rust_1_82"]
non_basic = ["typewit"]
docsrs = []
derive = ["const_panic_proc_macros", "non_basic"]
Expand All @@ -48,4 +50,4 @@ derive = ["const_panic_proc_macros", "non_basic"]
test = []

[package.metadata.docs.rs]
features = ["derive", "rust_1_64", "docsrs"]
features = ["derive", "rust_latest_stable", "docsrs"]
6 changes: 6 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ This changelog is a summary of the changes made in each release.

# 0.2

### 0.2.11

Added formatting for `core::num::{IntErrorKind, ParseIntError}`

Added `"rust_1_82"` and `"rust_latest_stable"` features

### 0.2.10

[Fixed internal use of the panic macro to not append nul bytes to the output](<https://github.com/rodrimati1992/const_panic/pull/12>).
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,15 @@ Enables support for formatting structs, enums, and arrays.
Without this feature, you can effectively only format primitive types
(custom types can manually implement formatting with more difficulty).

- `"rust_latest_stable"`(disabled by default):
Enables all the `"rust_1_*"` features.

- `"rust_1_64"`(disabled by default):
Enables formatting of additional items that require Rust 1.64.0 to do so.

- `"rust_1_82"`(disabled by default):
Enables formatting of additional items that require Rust 1.82.0 to do so.

- `"derive"`(disabled by default):
Enables the [`PanicFmt` derive] macro.

Expand Down
2 changes: 1 addition & 1 deletion src/fmt_impls/nonzero_impls.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{FmtArg, PanicFmt, PanicVal};
use crate::{FmtArg, PanicVal};

use core::num::{
NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU128,
Expand Down
2 changes: 1 addition & 1 deletion src/fmt_impls/other_impls.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{FmtArg, PanicFmt, PanicVal};
use crate::{FmtArg, PanicVal};

use core::{
marker::{PhantomData, PhantomPinned},
Expand Down
74 changes: 74 additions & 0 deletions src/fmt_impls/rust_1_82_fmt_impls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use crate::{
fmt::{FmtArg, FmtKind, PanicFmt},
PanicVal, StdWrapper,
};

use core::num::{IntErrorKind, ParseIntError};

#[cfg_attr(feature = "docsrs", doc(cfg(feature = "rust_1_82")))]
impl PanicFmt for ParseIntError {
type This = Self;
type Kind = crate::fmt::IsStdType;
const PV_COUNT: usize = 1;
}

#[cfg_attr(feature = "docsrs", doc(cfg(feature = "rust_1_82")))]
impl StdWrapper<&ParseIntError> {
/// Formats a `ParseIntError` (supports both Debug and Display formatting).
pub const fn to_panicvals(
self,
fmtarg: FmtArg,
) -> [PanicVal<'static>; ParseIntError::PV_COUNT] {
[self.to_panicval(fmtarg)]
}

/// Formats a `ParseIntError` (supports both Debug and Display formatting).
pub const fn to_panicval(self, fmtarg: FmtArg) -> PanicVal<'static> {
let this = self.0;
match fmtarg.fmt_kind {
FmtKind::Display => PanicVal::write_str(match this.kind() {
IntErrorKind::Empty => "cannot parse integer from empty string",
IntErrorKind::InvalidDigit => "invalid digit found in string",
IntErrorKind::PosOverflow => "number too large to fit in target type",
IntErrorKind::NegOverflow => "number too small to fit in target type",
IntErrorKind::Zero => "number would be zero for non-zero type",
_ => "<ParseIntError>",
}),
FmtKind::Debug => PanicVal::write_str(match this.kind() {
IntErrorKind::Empty => "ParseIntError { kind: Empty }",
IntErrorKind::InvalidDigit => "ParseIntError { kind: InvalidDigit }",
IntErrorKind::PosOverflow => "ParseIntError { kind: PosOverflow }",
IntErrorKind::NegOverflow => "ParseIntError { kind: NegOverflow }",
IntErrorKind::Zero => "ParseIntError { kind: Zero }",
_ => "<IntErrorKind>",
}),
}
}
}

#[cfg_attr(feature = "docsrs", doc(cfg(feature = "rust_1_82")))]
impl PanicFmt for IntErrorKind {
type This = Self;
type Kind = crate::fmt::IsStdType;
const PV_COUNT: usize = 1;
}

#[cfg_attr(feature = "docsrs", doc(cfg(feature = "rust_1_82")))]
impl StdWrapper<&IntErrorKind> {
/// Formats a `IntErrorKind` (supports only Debug formatting).
pub const fn to_panicvals(self, fmtarg: FmtArg) -> [PanicVal<'static>; IntErrorKind::PV_COUNT] {
[self.to_panicval(fmtarg)]
}

/// Formats a `IntErrorKind` (supports only Debug formatting).
pub const fn to_panicval(self, _: FmtArg) -> PanicVal<'static> {
PanicVal::write_str(match *self.0 {
IntErrorKind::Empty => "Empty",
IntErrorKind::InvalidDigit => "InvalidDigit",
IntErrorKind::PosOverflow => "PosOverflow",
IntErrorKind::NegOverflow => "NegOverflow",
IntErrorKind::Zero => "Zero",
_ => "<IntErrorKind>",
})
}
}
14 changes: 10 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,15 @@
//! Without this feature, you can effectively only format primitive types
//! (custom types can manually implement formatting with more difficulty).
//!
//! - `"rust_latest_stable"`(disabled by default):
//! Enables all the `"rust_1_*"` features.
//!
//! - `"rust_1_64"`(disabled by default):
//! Enables formatting of additional items that require Rust 1.64.0 to do so.
//!
//! - `"rust_1_82"`(disabled by default):
//! Enables formatting of additional items that require Rust 1.82.0 to do so.
//!
//! - `"derive"`(disabled by default):
//! Enables the [`PanicFmt` derive] macro.
//!
Expand Down Expand Up @@ -248,6 +254,9 @@ mod fmt_impls {
#[cfg(feature = "rust_1_64")]
mod rust_1_64_fmt_impls;

#[cfg(feature = "rust_1_82")]
mod rust_1_82_fmt_impls;

#[macro_use]
#[cfg(feature = "non_basic")]
mod option_fmt_impls;
Expand Down Expand Up @@ -294,10 +303,7 @@ pub mod __ {
#[cfg(feature = "non_basic")]
#[doc(hidden)]
mod reexported_non_basic {
pub use core::{
option::Option::{self, None, Some},
primitive::str,
};
pub use core::{option::Option, primitive::str};

pub use typewit::MakeTypeWitness;

Expand Down
21 changes: 10 additions & 11 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use crate::debug_str_fmt::ForEscaping;

#[cfg(feature = "rust_1_64")]
#[cfg(test)]
mod utils_1_64_tests;

#[cfg(feature = "non_basic")]
Expand Down Expand Up @@ -232,25 +233,24 @@ pub(crate) const fn tail_byte_array<const TO: usize>(

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


/// Const equivalent of `&buffer[..upto]` with saturating indexing.
///
/// "saturating indexing" means that if `upto > buffer.len()`,
///
/// "saturating indexing" means that if `upto > buffer.len()`,
/// then this returns all of `buffer` instead of panicking.
///
/// # Example
///
///
/// # Example
///
/// ```rust
/// use const_panic::utils::bytes_up_to;
///
///
/// const BYTES: &[u8] = &[3, 5, 8, 13, 21, 34, 55, 89];
///
///
/// const SLICE: &[u8] = bytes_up_to(BYTES, 4);
/// assert_eq!(SLICE, &[3, 5, 8, 13][..]);
///
///
/// const WHOLE: &[u8] = bytes_up_to(BYTES, usize::MAX);
/// assert_eq!(WHOLE, &[3, 5, 8, 13, 21, 34, 55, 89][..]);
///
///
/// ```
pub const fn bytes_up_to(buffer: &[u8], upto: usize) -> &[u8] {
if upto > buffer.len() {
Expand Down Expand Up @@ -283,4 +283,3 @@ pub const fn bytes_up_to(buffer: &[u8], upto: usize) -> &[u8] {
unsafe { core::slice::from_raw_parts(buffer.as_ptr(), upto) }
}
}

5 changes: 4 additions & 1 deletion tests/main_test_modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ mod main_tests {
#[cfg(feature = "rust_1_64")]
mod rust_1_64_types_tests;

#[cfg(feature = "rust_1_82")]
mod rust_1_82_types_tests;

#[cfg(feature = "non_basic")]
mod impl_panicfmt_tests;

Expand All @@ -44,6 +47,6 @@ mod main_tests {
mod pvcount_tests;

mod string_tests;

mod utils_tests;
}
2 changes: 2 additions & 0 deletions tests/main_tests/impl_panicfmt_tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(non_local_definitions)]

use const_panic::FmtArg;

use const_panic::test_utils::MyPhantomData;
Expand Down
1 change: 1 addition & 0 deletions tests/main_tests/rust_1_64_types_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ macro_rules! test_val {
}

#[test]
#[allow(invalid_from_utf8)]
fn test_utf8_error() {
let has_no_error_len = std::str::from_utf8(&[0xC2]).unwrap_err();
assert_eq!(has_no_error_len.error_len(), None);
Expand Down
64 changes: 64 additions & 0 deletions tests/main_tests/rust_1_82_types_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use core::num::{IntErrorKind, ParseIntError};

use const_panic::{FmtArg, StdWrapper};

macro_rules! test_val {
($value:expr) => ({
let val = $value;
let display = format!("{}", val);
let debug = format!("{:?}", val);
assert_eq!(
trunc_fmt!(1024; StdWrapper(&val).to_panicvals(FmtArg::DEBUG)),
&*debug,
);
assert_eq!(
trunc_fmt!(1024; StdWrapper(&val).to_panicvals(FmtArg::DISPLAY)),
&*display,
);
})
}

#[test]
fn test_parse_int_error() {
{
let err: ParseIntError = u32::from_str_radix("", 10).unwrap_err();
assert_eq!(*err.kind(), IntErrorKind::Empty);
test_val! {err}
}
{
let err: ParseIntError = u32::from_str_radix("A", 10).unwrap_err();
assert_eq!(*err.kind(), IntErrorKind::InvalidDigit);
test_val! {err}
}
{
let err: ParseIntError = u8::from_str_radix("256", 10).unwrap_err();
assert_eq!(*err.kind(), IntErrorKind::PosOverflow);
test_val! {err}
}
{
let err: ParseIntError = i8::from_str_radix("-256", 10).unwrap_err();
assert_eq!(*err.kind(), IntErrorKind::NegOverflow);
test_val! {err}
}
{
let err: ParseIntError = "0".parse::<core::num::NonZeroI8>().unwrap_err();
assert_eq!(*err.kind(), IntErrorKind::Zero);
test_val! {err}
}
}

#[test]
fn test_int_error_kind() {
for kind in [
IntErrorKind::Empty,
IntErrorKind::InvalidDigit,
IntErrorKind::PosOverflow,
IntErrorKind::NegOverflow,
IntErrorKind::Zero,
] {
assert_eq!(
trunc_fmt!(1024; StdWrapper(&kind).to_panicvals(FmtArg::DEBUG)),
&*format!("{kind:?}"),
);
}
}
13 changes: 4 additions & 9 deletions tests/main_tests/utils_tests.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use const_panic::utils::bytes_up_to;


macro_rules! case {
($bytes:expr, $upto:expr) => ({
($bytes:expr, $upto:expr) => {{
const SLICE: &[u8] = bytes_up_to($bytes, $upto);
assert_eq!(slice, );
})
assert_eq!(slice,);
}};
}


#[test]
fn test_bytes_up_to_isconst() {
const SLICE: &[u8] = bytes_up_to(&[10, 20], 1);
Expand All @@ -25,10 +23,7 @@ fn test_bytes_up_to() {
for bytes_len in iter.clone() {
let bytes = BYTES.get(..bytes_len).unwrap_or(BYTES);
for upto in iter.clone() {
assert_eq!(
bytes_up_to(bytes, upto),
bytes.get(..upto).unwrap_or(bytes),
)
assert_eq!(bytes_up_to(bytes, upto), bytes.get(..upto).unwrap_or(bytes),)
}
}
}
Loading