Skip to content

Commit

Permalink
Provide raw &str access from Decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark-Simulacrum committed Feb 22, 2022
1 parent 68369a0 commit da3b2ca
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
5 changes: 2 additions & 3 deletions compiler/rustc_serialize/src/opaque.rs
@@ -1,6 +1,5 @@
use crate::leb128::{self, max_leb128_len};
use crate::serialize::{self, Encoder as _};
use std::borrow::Cow;
use std::convert::TryInto;
use std::fs::File;
use std::io::{self, Write};
Expand Down Expand Up @@ -663,15 +662,15 @@ impl<'a> serialize::Decoder for Decoder<'a> {
}

#[inline]
fn read_str(&mut self) -> Cow<'_, str> {
fn read_str(&mut self) -> &str {
let len = self.read_usize();
let sentinel = self.data[self.position + len];
assert!(sentinel == STR_SENTINEL);
let s = unsafe {
std::str::from_utf8_unchecked(&self.data[self.position..self.position + len])
};
self.position += len + 1;
Cow::Borrowed(s)
s
}

#[inline]
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_serialize/src/serialize.rs
Expand Up @@ -198,7 +198,7 @@ pub trait Decoder {
fn read_f64(&mut self) -> f64;
fn read_f32(&mut self) -> f32;
fn read_char(&mut self) -> char;
fn read_str(&mut self) -> Cow<'_, str>;
fn read_str(&mut self) -> &str;
fn read_raw_bytes_into(&mut self, s: &mut [u8]);
}

Expand Down Expand Up @@ -313,7 +313,7 @@ impl<S: Encoder> Encodable<S> for String {

impl<D: Decoder> Decodable<D> for String {
fn decode(d: &mut D) -> String {
d.read_str().into_owned()
d.read_str().to_owned()
}
}

Expand Down

0 comments on commit da3b2ca

Please sign in to comment.