Skip to content

Commit 1ba5396

Browse files
committed
Constify methods that transmute
* AsciiChar::from_ascii_unchecked() * AsciiChar::as_printable_char()
1 parent a834fc3 commit 1ba5396

File tree

4 files changed

+12
-11
lines changed

4 files changed

+12
-11
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.44.1, stable, beta, nightly]
16+
rust: [1.56.1, stable, beta, nightly]
1717
steps:
1818
- uses: actions/checkout@v2
1919
- uses: hecrj/setup-rust-action@v1

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ 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.44.0.
38+
The minimum Rust version for 1.1.\* releases is 1.56.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.
41-
For example this means that if the current stable Rust version is 1.50 when
41+
For example this means that if the current stable Rust version is 1.70 when
4242
ascii 1.2.0 is released, then ascii 1.2.\* will not require a newer
43-
Rust version than 1.48.
43+
Rust version than 1.68.
4444

4545
## History
4646

src/ascii_char.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -375,9 +375,9 @@ impl AsciiChar {
375375
/// and `Some(AsciiChar::from_ascii_unchecked(128))` might be `None`.
376376
#[inline]
377377
#[must_use]
378-
pub unsafe fn from_ascii_unchecked(ch: u8) -> Self {
378+
pub const unsafe fn from_ascii_unchecked(ch: u8) -> Self {
379379
// SAFETY: Caller guarantees `ch` is within bounds of ascii.
380-
unsafe { ch.to_ascii_char_unchecked() }
380+
unsafe { mem::transmute(ch) }
381381
}
382382

383383
/// Converts an ASCII character into a `u8`.
@@ -659,14 +659,15 @@ impl AsciiChar {
659659
/// assert_eq!(AsciiChar::new('p').as_printable_char(), 'p');
660660
/// ```
661661
#[must_use]
662-
pub fn as_printable_char(self) -> char {
662+
pub const fn as_printable_char(self) -> char {
663+
#![allow(clippy::transmute_int_to_char)] // from_utf32_unchecked() is not const fn yet.
663664
match self as u8 {
664665
// Non printable characters
665666
// SAFETY: From codepoint 0x2400 ('␀') to 0x241f (`␟`), there are characters representing
666667
// the unprintable characters from 0x0 to 0x1f, ordered correctly.
667668
// As `b` is guaranteed to be within 0x0 to 0x1f, the conversion represents a
668669
// valid character.
669-
b @ 0x0..=0x1f => unsafe { char::from_u32_unchecked(u32::from('␀') + u32::from(b)) },
670+
b @ 0x0..=0x1f => unsafe { mem::transmute('␀' as u32 + b as u32) },
670671

671672
// 0x7f (delete) has it's own character at codepoint 0x2420, not 0x247f, so it is special
672673
// cased to return it's character

src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
//!
1616
//! # Minimum supported Rust version
1717
//!
18-
//! The minimum Rust version for 1.1.\* releases is 1.44.0.
18+
//! The minimum Rust version for 1.1.\* releases is 1.56.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.
21-
//! For example this means that if the current stable Rust version is 1.50 when
21+
//! For example this means that if the current stable Rust version is 1.70 when
2222
//! ascii 1.2.0 is released, then ascii 1.2.* will not require a newer
23-
//! Rust version than 1.48.
23+
//! Rust version than 1.68.
2424
//!
2525
//! # History
2626
//!

0 commit comments

Comments
 (0)