Skip to content

Commit e790bb7

Browse files
committed
core: replace ascii::char::to_{u8,char} with From impls
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
1 parent cd6d8f2 commit e790bb7

File tree

3 files changed

+24
-22
lines changed

3 files changed

+24
-22
lines changed

library/core/src/ascii/ascii_char.rs

+17-15
Original file line numberDiff line numberDiff line change
@@ -515,20 +515,6 @@ impl AsciiChar {
515515
}
516516
}
517517

518-
/// Gets this ASCII character as a byte.
519-
#[unstable(feature = "ascii_char", issue = "110998")]
520-
#[inline]
521-
pub const fn to_u8(self) -> u8 {
522-
self as u8
523-
}
524-
525-
/// Gets this ASCII character as a `char` Unicode Scalar Value.
526-
#[unstable(feature = "ascii_char", issue = "110998")]
527-
#[inline]
528-
pub const fn to_char(self) -> char {
529-
self as u8 as char
530-
}
531-
532518
/// Views this ASCII character as a one-code-unit UTF-8 `str`.
533519
#[unstable(feature = "ascii_char", issue = "110998")]
534520
#[inline]
@@ -537,6 +523,22 @@ impl AsciiChar {
537523
}
538524
}
539525

526+
macro_rules! into_int_impl {
527+
($($ty:ty)*) => {
528+
$(
529+
#[unstable(feature = "ascii_char", issue = "110998")]
530+
impl From<AsciiChar> for $ty {
531+
#[inline]
532+
fn from(chr: AsciiChar) -> $ty {
533+
chr as u8 as $ty
534+
}
535+
}
536+
)*
537+
}
538+
}
539+
540+
into_int_impl!(u8 u16 u32 u64 u128 char);
541+
540542
impl [AsciiChar] {
541543
/// Views this slice of ASCII characters as a UTF-8 `str`.
542544
#[unstable(feature = "ascii_char", issue = "110998")]
@@ -580,7 +582,7 @@ impl fmt::Debug for AsciiChar {
580582
AsciiChar::ReverseSolidus => backslash(AsciiChar::ReverseSolidus),
581583
AsciiChar::Apostrophe => backslash(AsciiChar::Apostrophe),
582584
_ => {
583-
let byte = self.to_u8();
585+
let byte = u8::from(*self);
584586
if !byte.is_ascii_control() {
585587
([*self, AsciiChar::Null, AsciiChar::Null, AsciiChar::Null], 1)
586588
} else {

library/core/src/escape.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ impl<const N: usize> EscapeIterInner<N> {
9999
}
100100

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

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

109109
pub fn advance_by(&mut self, n: usize) -> Result<(), NonZeroUsize> {

library/core/src/iter/range.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -490,18 +490,18 @@ impl Step for char {
490490
impl Step for AsciiChar {
491491
#[inline]
492492
fn steps_between(&start: &AsciiChar, &end: &AsciiChar) -> Option<usize> {
493-
Step::steps_between(&start.to_u8(), &end.to_u8())
493+
Step::steps_between(&u8::from(start), &u8::from(end))
494494
}
495495

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

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

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

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

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

0 commit comments

Comments
 (0)