Skip to content

Commit

Permalink
core: replace ascii::char::to_{u8,char} with From impls
Browse files Browse the repository at this point in the history
Introduce `From<core::ascii::char>` implementations for all unsigned
numeric types and `char`.  With those conversion presents, `to_u8` and
`to_char` methods on the type are no longer necessary.

This matches the interface of `char` type which doesn’t have `to_xx`
methods and instead offers From implementations.

Issue: #110998
  • Loading branch information
mina86 committed Jan 24, 2024
1 parent cd6d8f2 commit e790bb7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
32 changes: 17 additions & 15 deletions library/core/src/ascii/ascii_char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,20 +515,6 @@ impl AsciiChar {
}
}

/// Gets this ASCII character as a byte.
#[unstable(feature = "ascii_char", issue = "110998")]
#[inline]
pub const fn to_u8(self) -> u8 {
self as u8
}

/// Gets this ASCII character as a `char` Unicode Scalar Value.
#[unstable(feature = "ascii_char", issue = "110998")]
#[inline]
pub const fn to_char(self) -> char {
self as u8 as char
}

/// Views this ASCII character as a one-code-unit UTF-8 `str`.
#[unstable(feature = "ascii_char", issue = "110998")]
#[inline]
Expand All @@ -537,6 +523,22 @@ impl AsciiChar {
}
}

macro_rules! into_int_impl {
($($ty:ty)*) => {
$(
#[unstable(feature = "ascii_char", issue = "110998")]
impl From<AsciiChar> for $ty {
#[inline]
fn from(chr: AsciiChar) -> $ty {
chr as u8 as $ty
}
}
)*
}
}

into_int_impl!(u8 u16 u32 u64 u128 char);

impl [AsciiChar] {
/// Views this slice of ASCII characters as a UTF-8 `str`.
#[unstable(feature = "ascii_char", issue = "110998")]
Expand Down Expand Up @@ -580,7 +582,7 @@ impl fmt::Debug for AsciiChar {
AsciiChar::ReverseSolidus => backslash(AsciiChar::ReverseSolidus),
AsciiChar::Apostrophe => backslash(AsciiChar::Apostrophe),
_ => {
let byte = self.to_u8();
let byte = u8::from(*self);
if !byte.is_ascii_control() {
([*self, AsciiChar::Null, AsciiChar::Null, AsciiChar::Null], 1)
} else {
Expand Down
4 changes: 2 additions & 2 deletions library/core/src/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ impl<const N: usize> EscapeIterInner<N> {
}

pub fn next(&mut self) -> Option<u8> {
self.alive.next().map(|i| self.data[usize::from(i)].to_u8())
self.alive.next().map(|i| u8::from(self.data[usize::from(i)]))
}

pub fn next_back(&mut self) -> Option<u8> {
self.alive.next_back().map(|i| self.data[usize::from(i)].to_u8())
self.alive.next_back().map(|i| u8::from(self.data[usize::from(i)]))
}

pub fn advance_by(&mut self, n: usize) -> Result<(), NonZeroUsize> {
Expand Down
10 changes: 5 additions & 5 deletions library/core/src/iter/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,18 +490,18 @@ impl Step for char {
impl Step for AsciiChar {
#[inline]
fn steps_between(&start: &AsciiChar, &end: &AsciiChar) -> Option<usize> {
Step::steps_between(&start.to_u8(), &end.to_u8())
Step::steps_between(&u8::from(start), &u8::from(end))
}

#[inline]
fn forward_checked(start: AsciiChar, count: usize) -> Option<AsciiChar> {
let end = Step::forward_checked(start.to_u8(), count)?;
let end = Step::forward_checked(u8::from(start), count)?;
AsciiChar::from_u8(end)
}

#[inline]
fn backward_checked(start: AsciiChar, count: usize) -> Option<AsciiChar> {
let end = Step::backward_checked(start.to_u8(), count)?;
let end = Step::backward_checked(u8::from(start), count)?;

// SAFETY: Values below that of a valid ASCII character are also valid ASCII
Some(unsafe { AsciiChar::from_u8_unchecked(end) })
Expand All @@ -511,7 +511,7 @@ impl Step for AsciiChar {
unsafe fn forward_unchecked(start: AsciiChar, count: usize) -> AsciiChar {
// SAFETY: Caller asserts that result is a valid ASCII character,
// and therefore it is a valid u8.
let end = unsafe { Step::forward_unchecked(start.to_u8(), count) };
let end = unsafe { Step::forward_unchecked(u8::from(start), count) };

// SAFETY: Caller asserts that result is a valid ASCII character.
unsafe { AsciiChar::from_u8_unchecked(end) }
Expand All @@ -521,7 +521,7 @@ impl Step for AsciiChar {
unsafe fn backward_unchecked(start: AsciiChar, count: usize) -> AsciiChar {
// SAFETY: Caller asserts that result is a valid ASCII character,
// and therefore it is a valid u8.
let end = unsafe { Step::backward_unchecked(start.to_u8(), count) };
let end = unsafe { Step::backward_unchecked(u8::from(start), count) };

// SAFETY: Caller asserts that result is a valid ASCII character.
unsafe { AsciiChar::from_u8_unchecked(end) }
Expand Down

0 comments on commit e790bb7

Please sign in to comment.