Skip to content

Commit

Permalink
EscapeDefault: change range field to Range<u8>, reducing struct s…
Browse files Browse the repository at this point in the history
…ize 24 -> 6 bytes
  • Loading branch information
klensy committed Sep 18, 2021
1 parent 2b5ddf3 commit cccd6e0
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions library/core/src/ascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::str::from_utf8_unchecked;
#[stable(feature = "rust1", since = "1.0.0")]
#[derive(Clone)]
pub struct EscapeDefault {
range: Range<usize>,
range: Range<u8>,
data: [u8; 4],
}

Expand Down Expand Up @@ -114,7 +114,7 @@ pub fn escape_default(c: u8) -> EscapeDefault {
impl Iterator for EscapeDefault {
type Item = u8;
fn next(&mut self) -> Option<u8> {
self.range.next().map(|i| self.data[i])
self.range.next().map(|i| self.data[i as usize])
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.range.size_hint()
Expand All @@ -126,7 +126,7 @@ impl Iterator for EscapeDefault {
#[stable(feature = "rust1", since = "1.0.0")]
impl DoubleEndedIterator for EscapeDefault {
fn next_back(&mut self) -> Option<u8> {
self.range.next_back().map(|i| self.data[i])
self.range.next_back().map(|i| self.data[i as usize])
}
}
#[stable(feature = "rust1", since = "1.0.0")]
Expand All @@ -138,7 +138,9 @@ impl FusedIterator for EscapeDefault {}
impl fmt::Display for EscapeDefault {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// SAFETY: ok because `escape_default` created only valid utf-8 data
f.write_str(unsafe { from_utf8_unchecked(&self.data[self.range.clone()]) })
f.write_str(unsafe {
from_utf8_unchecked(&self.data[(self.range.start as usize)..(self.range.end as usize)])
})
}
}

Expand Down

0 comments on commit cccd6e0

Please sign in to comment.