Skip to content

Commit 7a16d25

Browse files
committed
Add some provided methods to Encoder/Decoder.
The methods for `i8`, `bool`, `char`, `str` are the same for all impls, because they layered on top of other methods.
1 parent fa133f5 commit 7a16d25

File tree

2 files changed

+56
-84
lines changed

2 files changed

+56
-84
lines changed

compiler/rustc_serialize/src/opaque.rs

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,6 @@ macro_rules! write_leb128 {
5151
}};
5252
}
5353

54-
/// A byte that [cannot occur in UTF8 sequences][utf8]. Used to mark the end of a string.
55-
/// This way we can skip validation and still be relatively sure that deserialization
56-
/// did not desynchronize.
57-
///
58-
/// [utf8]: https://en.wikipedia.org/w/index.php?title=UTF-8&oldid=1058865525#Codepage_layout
59-
const STR_SENTINEL: u8 = 0xC1;
60-
6154
impl Encoder for MemEncoder {
6255
#[inline]
6356
fn emit_usize(&mut self, v: usize) {
@@ -114,28 +107,6 @@ impl Encoder for MemEncoder {
114107
self.data.extend_from_slice(&v.to_le_bytes());
115108
}
116109

117-
#[inline]
118-
fn emit_i8(&mut self, v: i8) {
119-
self.emit_u8(v as u8);
120-
}
121-
122-
#[inline]
123-
fn emit_bool(&mut self, v: bool) {
124-
self.emit_u8(if v { 1 } else { 0 });
125-
}
126-
127-
#[inline]
128-
fn emit_char(&mut self, v: char) {
129-
self.emit_u32(v as u32);
130-
}
131-
132-
#[inline]
133-
fn emit_str(&mut self, v: &str) {
134-
self.emit_usize(v.len());
135-
self.emit_raw_bytes(v.as_bytes());
136-
self.emit_u8(STR_SENTINEL);
137-
}
138-
139110
#[inline]
140111
fn emit_raw_bytes(&mut self, s: &[u8]) {
141112
self.data.extend_from_slice(s);
@@ -480,28 +451,6 @@ impl Encoder for FileEncoder {
480451
self.write_all(&v.to_le_bytes());
481452
}
482453

483-
#[inline]
484-
fn emit_i8(&mut self, v: i8) {
485-
self.emit_u8(v as u8);
486-
}
487-
488-
#[inline]
489-
fn emit_bool(&mut self, v: bool) {
490-
self.emit_u8(if v { 1 } else { 0 });
491-
}
492-
493-
#[inline]
494-
fn emit_char(&mut self, v: char) {
495-
self.emit_u32(v as u32);
496-
}
497-
498-
#[inline]
499-
fn emit_str(&mut self, v: &str) {
500-
self.emit_usize(v.len());
501-
self.emit_raw_bytes(v.as_bytes());
502-
self.emit_u8(STR_SENTINEL);
503-
}
504-
505454
#[inline]
506455
fn emit_raw_bytes(&mut self, s: &[u8]) {
507456
self.write_all(s);
@@ -665,36 +614,11 @@ impl<'a> Decoder for MemDecoder<'a> {
665614
i16::from_le_bytes(self.read_array())
666615
}
667616

668-
#[inline]
669-
fn read_i8(&mut self) -> i8 {
670-
self.read_byte() as i8
671-
}
672-
673617
#[inline]
674618
fn read_isize(&mut self) -> isize {
675619
read_leb128!(self, read_isize_leb128)
676620
}
677621

678-
#[inline]
679-
fn read_bool(&mut self) -> bool {
680-
let value = self.read_u8();
681-
value != 0
682-
}
683-
684-
#[inline]
685-
fn read_char(&mut self) -> char {
686-
let bits = self.read_u32();
687-
std::char::from_u32(bits).unwrap()
688-
}
689-
690-
#[inline]
691-
fn read_str(&mut self) -> &str {
692-
let len = self.read_usize();
693-
let bytes = self.read_raw_bytes(len + 1);
694-
assert!(bytes[len] == STR_SENTINEL);
695-
unsafe { std::str::from_utf8_unchecked(&bytes[..len]) }
696-
}
697-
698622
#[inline]
699623
fn read_raw_bytes(&mut self, bytes: usize) -> &'a [u8] {
700624
if bytes > self.remaining() {

compiler/rustc_serialize/src/serialize.rs

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ use std::path;
1212
use std::rc::Rc;
1313
use std::sync::Arc;
1414

15+
/// A byte that [cannot occur in UTF8 sequences][utf8]. Used to mark the end of a string.
16+
/// This way we can skip validation and still be relatively sure that deserialization
17+
/// did not desynchronize.
18+
///
19+
/// [utf8]: https://en.wikipedia.org/w/index.php?title=UTF-8&oldid=1058865525#Codepage_layout
20+
const STR_SENTINEL: u8 = 0xC1;
21+
1522
/// A note about error handling.
1623
///
1724
/// Encoders may be fallible, but in practice failure is rare and there are so
@@ -40,10 +47,29 @@ pub trait Encoder {
4047
fn emit_i64(&mut self, v: i64);
4148
fn emit_i32(&mut self, v: i32);
4249
fn emit_i16(&mut self, v: i16);
43-
fn emit_i8(&mut self, v: i8);
44-
fn emit_bool(&mut self, v: bool);
45-
fn emit_char(&mut self, v: char);
46-
fn emit_str(&mut self, v: &str);
50+
51+
#[inline]
52+
fn emit_i8(&mut self, v: i8) {
53+
self.emit_u8(v as u8);
54+
}
55+
56+
#[inline]
57+
fn emit_bool(&mut self, v: bool) {
58+
self.emit_u8(if v { 1 } else { 0 });
59+
}
60+
61+
#[inline]
62+
fn emit_char(&mut self, v: char) {
63+
self.emit_u32(v as u32);
64+
}
65+
66+
#[inline]
67+
fn emit_str(&mut self, v: &str) {
68+
self.emit_usize(v.len());
69+
self.emit_raw_bytes(v.as_bytes());
70+
self.emit_u8(STR_SENTINEL);
71+
}
72+
4773
fn emit_raw_bytes(&mut self, s: &[u8]);
4874

4975
fn emit_enum_variant<F>(&mut self, v_id: usize, f: F)
@@ -79,10 +105,32 @@ pub trait Decoder {
79105
fn read_i64(&mut self) -> i64;
80106
fn read_i32(&mut self) -> i32;
81107
fn read_i16(&mut self) -> i16;
82-
fn read_i8(&mut self) -> i8;
83-
fn read_bool(&mut self) -> bool;
84-
fn read_char(&mut self) -> char;
85-
fn read_str(&mut self) -> &str;
108+
109+
#[inline]
110+
fn read_i8(&mut self) -> i8 {
111+
self.read_u8() as i8
112+
}
113+
114+
#[inline]
115+
fn read_bool(&mut self) -> bool {
116+
let value = self.read_u8();
117+
value != 0
118+
}
119+
120+
#[inline]
121+
fn read_char(&mut self) -> char {
122+
let bits = self.read_u32();
123+
std::char::from_u32(bits).unwrap()
124+
}
125+
126+
#[inline]
127+
fn read_str(&mut self) -> &str {
128+
let len = self.read_usize();
129+
let bytes = self.read_raw_bytes(len + 1);
130+
assert!(bytes[len] == STR_SENTINEL);
131+
unsafe { std::str::from_utf8_unchecked(&bytes[..len]) }
132+
}
133+
86134
fn read_raw_bytes(&mut self, len: usize) -> &[u8];
87135

88136
// Although there is an `emit_enum_variant` method in `Encoder`, the code

0 commit comments

Comments
 (0)