Skip to content

Commit

Permalink
Remove MemDecoder::read_raw_bytes_inherent.
Browse files Browse the repository at this point in the history
It's unnecessary. Note that `MemDecoder::read_raw_bytes` how has a `&'a
[u8]` return type, the same as what `read_raw_bytes_inherent` had.
  • Loading branch information
nnethercote committed Apr 27, 2023
1 parent c14882f commit b51deba
Showing 1 changed file with 11 additions and 19 deletions.
30 changes: 11 additions & 19 deletions compiler/rustc_serialize/src/opaque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,22 +573,6 @@ impl<'a> MemDecoder<'a> {
self.read_raw_bytes(N).try_into().unwrap()
}

// The trait method doesn't have a lifetime parameter, and we need a version of this
// that definitely returns a slice based on the underlying storage as opposed to
// the Decoder itself in order to implement read_str efficiently.
#[inline]
fn read_raw_bytes_inherent(&mut self, bytes: usize) -> &'a [u8] {
if bytes > self.remaining() {
Self::decoder_exhausted();
}
// SAFETY: We just checked if this range is in-bounds above.
unsafe {
let slice = std::slice::from_raw_parts(self.current, bytes);
self.current = self.current.add(bytes);
slice
}
}

/// While we could manually expose manipulation of the decoder position,
/// all current users of that method would need to reset the position later,
/// incurring the bounds check of set_position twice.
Expand Down Expand Up @@ -706,14 +690,22 @@ impl<'a> Decoder for MemDecoder<'a> {
#[inline]
fn read_str(&mut self) -> &str {
let len = self.read_usize();
let bytes = self.read_raw_bytes_inherent(len + 1);
let bytes = self.read_raw_bytes(len + 1);
assert!(bytes[len] == STR_SENTINEL);
unsafe { std::str::from_utf8_unchecked(&bytes[..len]) }
}

#[inline]
fn read_raw_bytes(&mut self, bytes: usize) -> &[u8] {
self.read_raw_bytes_inherent(bytes)
fn read_raw_bytes(&mut self, bytes: usize) -> &'a [u8] {
if bytes > self.remaining() {
Self::decoder_exhausted();
}
// SAFETY: We just checked if this range is in-bounds above.
unsafe {
let slice = std::slice::from_raw_parts(self.current, bytes);
self.current = self.current.add(bytes);
slice
}
}

#[inline]
Expand Down

0 comments on commit b51deba

Please sign in to comment.