Skip to content

Commit 43ba1a3

Browse files
committed
Constify methods that transmute
* AsciiChar::from_ascii_unchecked() * AsciiChar::as_printable_char()
1 parent 3701661 commit 43ba1a3

File tree

4 files changed

+8
-7
lines changed

4 files changed

+8
-7
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
strategy:
1414
fail-fast: false
1515
matrix:
16-
rust: [1.46.0, 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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ ascii = { version = "1.1", default-features = false, features = ["alloc"] }
3535

3636
## Minimum supported Rust version
3737

38-
The minimum Rust version for 1.2.\* releases is 1.46.0.
38+
The minimum Rust version for 1.2.\* releases is 1.56.1.
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

Lines changed: 5 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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.2.\* releases is 1.46.0.
18+
//! The minimum Rust version for 1.2.\* releases is 1.56.1.
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)