From 577515481a4b9d70a20379b29bd0ea52735d3750 Mon Sep 17 00:00:00 2001 From: Noa Date: Thu, 16 Nov 2023 16:44:38 -0600 Subject: [PATCH 1/2] Fix bindings_with_variant_name error --- src/ascii_char.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ascii_char.rs b/src/ascii_char.rs index 39f2ceb..1858901 100644 --- a/src/ascii_char.rs +++ b/src/ascii_char.rs @@ -794,8 +794,8 @@ macro_rules! impl_into_partial_eq_ord { ($wider:ty, $to_wider:expr) => { impl From for $wider { #[inline] - fn from(a: AsciiChar) -> $wider { - $to_wider(a) + fn from(ch: AsciiChar) -> $wider { + $to_wider(ch) } } impl PartialEq<$wider> for AsciiChar { From 4e2e56bbb9d593c96de514e2318af4be44e4d283 Mon Sep 17 00:00:00 2001 From: Noa Date: Thu, 16 Nov 2023 16:54:30 -0600 Subject: [PATCH 2/2] Better error message for AsciiChar::new --- Cargo.toml | 1 + README.md | 2 +- src/ascii_char.rs | 41 ++++++++--------------------------------- 3 files changed, 10 insertions(+), 34 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8ec25de..3e5b6eb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ name = "ascii" readme = "README.md" repository = "https://github.com/tomprogrammer/rust-ascii" version = "1.1.0" +rust-version = "1.57.0" [dependencies] serde = { version = "1.0.25", optional = true } diff --git a/README.md b/README.md index fa960cc..d0a360d 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ ascii = { version = "1.1", default-features = false, features = ["alloc"] } ## Minimum supported Rust version -The minimum Rust version for 1.2.\* releases is 1.56.1. +The minimum Rust version for 1.2.\* releases is 1.57.0. Later 1.y.0 releases might require newer Rust versions, but the three most recent stable releases at the time of publishing will always be supported. For example this means that if the current stable Rust version is 1.70 when diff --git a/src/ascii_char.rs b/src/ascii_char.rs index 1858901..fccf79c 100644 --- a/src/ascii_char.rs +++ b/src/ascii_char.rs @@ -341,34 +341,10 @@ impl AsciiChar { /// current limitations of `const fn`. #[must_use] pub const fn new(ch: char) -> AsciiChar { - // It's restricted to this function, and without it - // we'd need to specify `AsciiChar::` or `Self::` 128 times. - #[allow(clippy::enum_glob_use)] - use AsciiChar::*; - - #[rustfmt::skip] - const ALL: [AsciiChar; 128] = [ - Null, SOH, AsciiChar::STX, ETX, EOT, ENQ, ACK, Bell, - BackSpace, Tab, LineFeed, VT, FF, CarriageReturn, SI, SO, - DLE, DC1, DC2, DC3, DC4, NAK, SYN, ETB, - CAN, EM, SUB, ESC, FS, GS, RS, US, - Space, Exclamation, Quotation, Hash, Dollar, Percent, Ampersand, Apostrophe, - ParenOpen, ParenClose, Asterisk, Plus, Comma, Minus, Dot, Slash, - _0, _1, _2, _3, _4, _5, _6, _7, - _8, _9, Colon, Semicolon, LessThan, Equal, GreaterThan, Question, - At, A, B, C, D, E, F, G, - H, I, J, K, L, M, N, O, - P, Q, R, S, T, U, V, W, - X, Y, Z, BracketOpen, BackSlash, BracketClose, Caret, UnderScore, - Grave, a, b, c, d, e, f, g, - h, i, j, k, l, m, n, o, - p, q, r, s, t, u, v, w, - x, y, z, CurlyBraceOpen, VerticalBar, CurlyBraceClose, Tilde, DEL, - ]; - - // We want to slice here and detect `const_err` from rustc if the slice is invalid - #[allow(clippy::indexing_slicing)] - ALL[ch as usize] + match Self::try_new(ch) { + Ok(ch) => ch, + Err(_) => panic!("{}", ERRORMSG_CHAR), + } } /// Create an `AsciiChar` from a `char`, in a `const fn` way. @@ -389,11 +365,10 @@ impl AsciiChar { /// Fails for non-ASCII characters. #[inline] pub const fn try_new(ch: char) -> Result { - unsafe { - match ch as u32 { - 0..=127 => Ok(mem::transmute(ch as u8)), - _ => Err(ToAsciiCharError(())), - } + if ch.is_ascii() { + Ok(unsafe { Self::from_ascii_unchecked(ch as u8) }) + } else { + Err(ToAsciiCharError(())) } }