Skip to content

Commit fb8b234

Browse files
committed
Give a more meaningful panic message in AsciiChar::new()
Including the offending character in the panic message is not yet possible as of 1.61, so instead use #{track_caller] to make the panic text point to where a panic happened.
1 parent 6efbde6 commit fb8b234

File tree

4 files changed

+13
-33
lines changed

4 files changed

+13
-33
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
strategy:
1414
fail-fast: false
1515
matrix:
16-
rust: [1.56.1, stable, beta, nightly]
16+
rust: [1.57.0, stable, beta, nightly]
1717
steps:
1818
- uses: actions/checkout@v2
1919
- uses: hecrj/setup-rust-action@v1

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ ascii = { version = "1.0", default-features = false, features = ["alloc"] }
3535

3636
## Minimum supported Rust version
3737

38-
The minimum Rust version for 1.1.\* releases is 1.56.0.
38+
The minimum Rust version for 1.1.\* releases is 1.57.0.
3939
Later 1.y.0 releases might require newer Rust versions, but the three most
4040
recent stable releases at the time of publishing will always be supported.
4141
For example this means that if the current stable Rust version is 1.70 when

src/ascii_char.rs

+10-30
Original file line numberDiff line numberDiff line change
@@ -326,38 +326,18 @@ impl AsciiChar {
326326
///
327327
/// This function will panic if passed a non-ASCII character.
328328
///
329-
/// The panic message might not be the most descriptive due to the
330-
/// current limitations of `const fn`.
329+
/// The panic message might not be the most descriptive due to
330+
/// current limitations of what is available in `const fn`.
331+
#[inline]
331332
#[must_use]
333+
#[track_caller]
332334
pub const fn new(ch: char) -> AsciiChar {
333-
// It's restricted to this function, and without it
334-
// we'd need to specify `AsciiChar::` or `Self::` 128 times.
335-
#[allow(clippy::enum_glob_use)]
336-
use AsciiChar::*;
337-
338-
#[rustfmt::skip]
339-
const ALL: [AsciiChar; 128] = [
340-
Null, SOH, SOX, ETX, EOT, ENQ, ACK, Bell,
341-
BackSpace, Tab, LineFeed, VT, FF, CarriageReturn, SI, SO,
342-
DLE, DC1, DC2, DC3, DC4, NAK, SYN, ETB,
343-
CAN, EM, SUB, ESC, FS, GS, RS, US,
344-
Space, Exclamation, Quotation, Hash, Dollar, Percent, Ampersand, Apostrophe,
345-
ParenOpen, ParenClose, Asterisk, Plus, Comma, Minus, Dot, Slash,
346-
_0, _1, _2, _3, _4, _5, _6, _7,
347-
_8, _9, Colon, Semicolon, LessThan, Equal, GreaterThan, Question,
348-
At, A, B, C, D, E, F, G,
349-
H, I, J, K, L, M, N, O,
350-
P, Q, R, S, T, U, V, W,
351-
X, Y, Z, BracketOpen, BackSlash, BracketClose, Caret, UnderScore,
352-
Grave, a, b, c, d, e, f, g,
353-
h, i, j, k, l, m, n, o,
354-
p, q, r, s, t, u, v, w,
355-
x, y, z, CurlyBraceOpen, VerticalBar, CurlyBraceClose, Tilde, DEL,
356-
];
357-
358-
// We want to slice here and detect `const_err` from rustc if the slice is invalid
359-
#[allow(clippy::indexing_slicing)]
360-
ALL[ch as usize]
335+
unsafe {
336+
match ch as u32 {
337+
0..=127 => mem::transmute(ch as u8),
338+
_ => panic!("not an ASCII character"),
339+
}
340+
}
361341
}
362342

363343
/// Create an `AsciiChar` from a `char`, in a `const fn` way.

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
//!
1616
//! # Minimum supported Rust version
1717
//!
18-
//! The minimum Rust version for 1.1.\* releases is 1.56.0.
18+
//! The minimum Rust version for 1.1.\* releases is 1.57.0.
1919
//! Later 1.y.0 releases might require newer Rust versions, but the three most
2020
//! recent stable releases at the time of publishing will always be supported.
2121
//! For example this means that if the current stable Rust version is 1.70 when

0 commit comments

Comments
 (0)