mv compiler to compiler/
This commit is contained in:
parent
db534b3ac2
commit
9e5f7d5631
1686 changed files with 941 additions and 1051 deletions
337
compiler/rustc_serialize/src/collection_impls.rs
Normal file
337
compiler/rustc_serialize/src/collection_impls.rs
Normal file
|
@ -0,0 +1,337 @@
|
|||
//! Implementations of serialization for structures found in liballoc
|
||||
|
||||
use std::hash::{BuildHasher, Hash};
|
||||
|
||||
use crate::{Decodable, Decoder, Encodable, Encoder};
|
||||
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, LinkedList, VecDeque};
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
|
||||
use smallvec::{Array, SmallVec};
|
||||
|
||||
impl<S: Encoder, A: Array<Item: Encodable<S>>> Encodable<S> for SmallVec<A> {
|
||||
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
||||
s.emit_seq(self.len(), |s| {
|
||||
for (i, e) in self.iter().enumerate() {
|
||||
s.emit_seq_elt(i, |s| e.encode(s))?;
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Decoder, A: Array<Item: Decodable<D>>> Decodable<D> for SmallVec<A> {
|
||||
fn decode(d: &mut D) -> Result<SmallVec<A>, D::Error> {
|
||||
d.read_seq(|d, len| {
|
||||
let mut vec = SmallVec::with_capacity(len);
|
||||
// FIXME(#48994) - could just be collected into a Result<SmallVec, D::Error>
|
||||
for i in 0..len {
|
||||
vec.push(d.read_seq_elt(i, |d| Decodable::decode(d))?);
|
||||
}
|
||||
Ok(vec)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: Encoder, T: Encodable<S>> Encodable<S> for LinkedList<T> {
|
||||
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
||||
s.emit_seq(self.len(), |s| {
|
||||
for (i, e) in self.iter().enumerate() {
|
||||
s.emit_seq_elt(i, |s| e.encode(s))?;
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Decoder, T: Decodable<D>> Decodable<D> for LinkedList<T> {
|
||||
fn decode(d: &mut D) -> Result<LinkedList<T>, D::Error> {
|
||||
d.read_seq(|d, len| {
|
||||
let mut list = LinkedList::new();
|
||||
for i in 0..len {
|
||||
list.push_back(d.read_seq_elt(i, |d| Decodable::decode(d))?);
|
||||
}
|
||||
Ok(list)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: Encoder, T: Encodable<S>> Encodable<S> for VecDeque<T> {
|
||||
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
||||
s.emit_seq(self.len(), |s| {
|
||||
for (i, e) in self.iter().enumerate() {
|
||||
s.emit_seq_elt(i, |s| e.encode(s))?;
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Decoder, T: Decodable<D>> Decodable<D> for VecDeque<T> {
|
||||
fn decode(d: &mut D) -> Result<VecDeque<T>, D::Error> {
|
||||
d.read_seq(|d, len| {
|
||||
let mut deque: VecDeque<T> = VecDeque::with_capacity(len);
|
||||
for i in 0..len {
|
||||
deque.push_back(d.read_seq_elt(i, |d| Decodable::decode(d))?);
|
||||
}
|
||||
Ok(deque)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: Encoder, K, V> Encodable<S> for BTreeMap<K, V>
|
||||
where
|
||||
K: Encodable<S> + PartialEq + Ord,
|
||||
V: Encodable<S>,
|
||||
{
|
||||
fn encode(&self, e: &mut S) -> Result<(), S::Error> {
|
||||
e.emit_map(self.len(), |e| {
|
||||
for (i, (key, val)) in self.iter().enumerate() {
|
||||
e.emit_map_elt_key(i, |e| key.encode(e))?;
|
||||
e.emit_map_elt_val(i, |e| val.encode(e))?;
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Decoder, K, V> Decodable<D> for BTreeMap<K, V>
|
||||
where
|
||||
K: Decodable<D> + PartialEq + Ord,
|
||||
V: Decodable<D>,
|
||||
{
|
||||
fn decode(d: &mut D) -> Result<BTreeMap<K, V>, D::Error> {
|
||||
d.read_map(|d, len| {
|
||||
let mut map = BTreeMap::new();
|
||||
for i in 0..len {
|
||||
let key = d.read_map_elt_key(i, |d| Decodable::decode(d))?;
|
||||
let val = d.read_map_elt_val(i, |d| Decodable::decode(d))?;
|
||||
map.insert(key, val);
|
||||
}
|
||||
Ok(map)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: Encoder, T> Encodable<S> for BTreeSet<T>
|
||||
where
|
||||
T: Encodable<S> + PartialEq + Ord,
|
||||
{
|
||||
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
||||
s.emit_seq(self.len(), |s| {
|
||||
for (i, e) in self.iter().enumerate() {
|
||||
s.emit_seq_elt(i, |s| e.encode(s))?;
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Decoder, T> Decodable<D> for BTreeSet<T>
|
||||
where
|
||||
T: Decodable<D> + PartialEq + Ord,
|
||||
{
|
||||
fn decode(d: &mut D) -> Result<BTreeSet<T>, D::Error> {
|
||||
d.read_seq(|d, len| {
|
||||
let mut set = BTreeSet::new();
|
||||
for i in 0..len {
|
||||
set.insert(d.read_seq_elt(i, |d| Decodable::decode(d))?);
|
||||
}
|
||||
Ok(set)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Encoder, K, V, S> Encodable<E> for HashMap<K, V, S>
|
||||
where
|
||||
K: Encodable<E> + Eq,
|
||||
V: Encodable<E>,
|
||||
S: BuildHasher,
|
||||
{
|
||||
fn encode(&self, e: &mut E) -> Result<(), E::Error> {
|
||||
e.emit_map(self.len(), |e| {
|
||||
for (i, (key, val)) in self.iter().enumerate() {
|
||||
e.emit_map_elt_key(i, |e| key.encode(e))?;
|
||||
e.emit_map_elt_val(i, |e| val.encode(e))?;
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Decoder, K, V, S> Decodable<D> for HashMap<K, V, S>
|
||||
where
|
||||
K: Decodable<D> + Hash + Eq,
|
||||
V: Decodable<D>,
|
||||
S: BuildHasher + Default,
|
||||
{
|
||||
fn decode(d: &mut D) -> Result<HashMap<K, V, S>, D::Error> {
|
||||
d.read_map(|d, len| {
|
||||
let state = Default::default();
|
||||
let mut map = HashMap::with_capacity_and_hasher(len, state);
|
||||
for i in 0..len {
|
||||
let key = d.read_map_elt_key(i, |d| Decodable::decode(d))?;
|
||||
let val = d.read_map_elt_val(i, |d| Decodable::decode(d))?;
|
||||
map.insert(key, val);
|
||||
}
|
||||
Ok(map)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Encoder, T, S> Encodable<E> for HashSet<T, S>
|
||||
where
|
||||
T: Encodable<E> + Eq,
|
||||
S: BuildHasher,
|
||||
{
|
||||
fn encode(&self, s: &mut E) -> Result<(), E::Error> {
|
||||
s.emit_seq(self.len(), |s| {
|
||||
for (i, e) in self.iter().enumerate() {
|
||||
s.emit_seq_elt(i, |s| e.encode(s))?;
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Encoder, T, S> Encodable<E> for &HashSet<T, S>
|
||||
where
|
||||
T: Encodable<E> + Eq,
|
||||
S: BuildHasher,
|
||||
{
|
||||
fn encode(&self, s: &mut E) -> Result<(), E::Error> {
|
||||
(**self).encode(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Decoder, T, S> Decodable<D> for HashSet<T, S>
|
||||
where
|
||||
T: Decodable<D> + Hash + Eq,
|
||||
S: BuildHasher + Default,
|
||||
{
|
||||
fn decode(d: &mut D) -> Result<HashSet<T, S>, D::Error> {
|
||||
d.read_seq(|d, len| {
|
||||
let state = Default::default();
|
||||
let mut set = HashSet::with_capacity_and_hasher(len, state);
|
||||
for i in 0..len {
|
||||
set.insert(d.read_seq_elt(i, |d| Decodable::decode(d))?);
|
||||
}
|
||||
Ok(set)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Encoder, K, V, S> Encodable<E> for indexmap::IndexMap<K, V, S>
|
||||
where
|
||||
K: Encodable<E> + Hash + Eq,
|
||||
V: Encodable<E>,
|
||||
S: BuildHasher,
|
||||
{
|
||||
fn encode(&self, e: &mut E) -> Result<(), E::Error> {
|
||||
e.emit_map(self.len(), |e| {
|
||||
for (i, (key, val)) in self.iter().enumerate() {
|
||||
e.emit_map_elt_key(i, |e| key.encode(e))?;
|
||||
e.emit_map_elt_val(i, |e| val.encode(e))?;
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Decoder, K, V, S> Decodable<D> for indexmap::IndexMap<K, V, S>
|
||||
where
|
||||
K: Decodable<D> + Hash + Eq,
|
||||
V: Decodable<D>,
|
||||
S: BuildHasher + Default,
|
||||
{
|
||||
fn decode(d: &mut D) -> Result<indexmap::IndexMap<K, V, S>, D::Error> {
|
||||
d.read_map(|d, len| {
|
||||
let state = Default::default();
|
||||
let mut map = indexmap::IndexMap::with_capacity_and_hasher(len, state);
|
||||
for i in 0..len {
|
||||
let key = d.read_map_elt_key(i, |d| Decodable::decode(d))?;
|
||||
let val = d.read_map_elt_val(i, |d| Decodable::decode(d))?;
|
||||
map.insert(key, val);
|
||||
}
|
||||
Ok(map)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Encoder, T, S> Encodable<E> for indexmap::IndexSet<T, S>
|
||||
where
|
||||
T: Encodable<E> + Hash + Eq,
|
||||
S: BuildHasher,
|
||||
{
|
||||
fn encode(&self, s: &mut E) -> Result<(), E::Error> {
|
||||
s.emit_seq(self.len(), |s| {
|
||||
for (i, e) in self.iter().enumerate() {
|
||||
s.emit_seq_elt(i, |s| e.encode(s))?;
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Decoder, T, S> Decodable<D> for indexmap::IndexSet<T, S>
|
||||
where
|
||||
T: Decodable<D> + Hash + Eq,
|
||||
S: BuildHasher + Default,
|
||||
{
|
||||
fn decode(d: &mut D) -> Result<indexmap::IndexSet<T, S>, D::Error> {
|
||||
d.read_seq(|d, len| {
|
||||
let state = Default::default();
|
||||
let mut set = indexmap::IndexSet::with_capacity_and_hasher(len, state);
|
||||
for i in 0..len {
|
||||
set.insert(d.read_seq_elt(i, |d| Decodable::decode(d))?);
|
||||
}
|
||||
Ok(set)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Encoder, T: Encodable<E>> Encodable<E> for Rc<[T]> {
|
||||
fn encode(&self, s: &mut E) -> Result<(), E::Error> {
|
||||
s.emit_seq(self.len(), |s| {
|
||||
for (index, e) in self.iter().enumerate() {
|
||||
s.emit_seq_elt(index, |s| e.encode(s))?;
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Decoder, T: Decodable<D>> Decodable<D> for Rc<[T]> {
|
||||
fn decode(d: &mut D) -> Result<Rc<[T]>, D::Error> {
|
||||
d.read_seq(|d, len| {
|
||||
let mut vec = Vec::with_capacity(len);
|
||||
for index in 0..len {
|
||||
vec.push(d.read_seq_elt(index, |d| Decodable::decode(d))?);
|
||||
}
|
||||
Ok(vec.into())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Encoder, T: Encodable<E>> Encodable<E> for Arc<[T]> {
|
||||
fn encode(&self, s: &mut E) -> Result<(), E::Error> {
|
||||
s.emit_seq(self.len(), |s| {
|
||||
for (index, e) in self.iter().enumerate() {
|
||||
s.emit_seq_elt(index, |s| e.encode(s))?;
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Decoder, T: Decodable<D>> Decodable<D> for Arc<[T]> {
|
||||
fn decode(d: &mut D) -> Result<Arc<[T]>, D::Error> {
|
||||
d.read_seq(|d, len| {
|
||||
let mut vec = Vec::with_capacity(len);
|
||||
for index in 0..len {
|
||||
vec.push(d.read_seq_elt(index, |d| Decodable::decode(d))?);
|
||||
}
|
||||
Ok(vec.into())
|
||||
})
|
||||
}
|
||||
}
|
2778
compiler/rustc_serialize/src/json.rs
Normal file
2778
compiler/rustc_serialize/src/json.rs
Normal file
File diff suppressed because it is too large
Load diff
147
compiler/rustc_serialize/src/json/tests.rs
Normal file
147
compiler/rustc_serialize/src/json/tests.rs
Normal file
|
@ -0,0 +1,147 @@
|
|||
// Benchmarks and tests that require private items
|
||||
|
||||
extern crate test;
|
||||
use super::{from_str, Parser, Stack, StackElement};
|
||||
use std::string;
|
||||
use test::Bencher;
|
||||
|
||||
#[test]
|
||||
fn test_stack() {
|
||||
let mut stack = Stack::new();
|
||||
|
||||
assert!(stack.is_empty());
|
||||
assert!(stack.is_empty());
|
||||
assert!(!stack.last_is_index());
|
||||
|
||||
stack.push_index(0);
|
||||
stack.bump_index();
|
||||
|
||||
assert!(stack.len() == 1);
|
||||
assert!(stack.is_equal_to(&[StackElement::Index(1)]));
|
||||
assert!(stack.starts_with(&[StackElement::Index(1)]));
|
||||
assert!(stack.ends_with(&[StackElement::Index(1)]));
|
||||
assert!(stack.last_is_index());
|
||||
assert!(stack.get(0) == StackElement::Index(1));
|
||||
|
||||
stack.push_key("foo".to_string());
|
||||
|
||||
assert!(stack.len() == 2);
|
||||
assert!(stack.is_equal_to(&[StackElement::Index(1), StackElement::Key("foo")]));
|
||||
assert!(stack.starts_with(&[StackElement::Index(1), StackElement::Key("foo")]));
|
||||
assert!(stack.starts_with(&[StackElement::Index(1)]));
|
||||
assert!(stack.ends_with(&[StackElement::Index(1), StackElement::Key("foo")]));
|
||||
assert!(stack.ends_with(&[StackElement::Key("foo")]));
|
||||
assert!(!stack.last_is_index());
|
||||
assert!(stack.get(0) == StackElement::Index(1));
|
||||
assert!(stack.get(1) == StackElement::Key("foo"));
|
||||
|
||||
stack.push_key("bar".to_string());
|
||||
|
||||
assert!(stack.len() == 3);
|
||||
assert!(stack.is_equal_to(&[
|
||||
StackElement::Index(1),
|
||||
StackElement::Key("foo"),
|
||||
StackElement::Key("bar")
|
||||
]));
|
||||
assert!(stack.starts_with(&[StackElement::Index(1)]));
|
||||
assert!(stack.starts_with(&[StackElement::Index(1), StackElement::Key("foo")]));
|
||||
assert!(stack.starts_with(&[
|
||||
StackElement::Index(1),
|
||||
StackElement::Key("foo"),
|
||||
StackElement::Key("bar")
|
||||
]));
|
||||
assert!(stack.ends_with(&[StackElement::Key("bar")]));
|
||||
assert!(stack.ends_with(&[StackElement::Key("foo"), StackElement::Key("bar")]));
|
||||
assert!(stack.ends_with(&[
|
||||
StackElement::Index(1),
|
||||
StackElement::Key("foo"),
|
||||
StackElement::Key("bar")
|
||||
]));
|
||||
assert!(!stack.last_is_index());
|
||||
assert!(stack.get(0) == StackElement::Index(1));
|
||||
assert!(stack.get(1) == StackElement::Key("foo"));
|
||||
assert!(stack.get(2) == StackElement::Key("bar"));
|
||||
|
||||
stack.pop();
|
||||
|
||||
assert!(stack.len() == 2);
|
||||
assert!(stack.is_equal_to(&[StackElement::Index(1), StackElement::Key("foo")]));
|
||||
assert!(stack.starts_with(&[StackElement::Index(1), StackElement::Key("foo")]));
|
||||
assert!(stack.starts_with(&[StackElement::Index(1)]));
|
||||
assert!(stack.ends_with(&[StackElement::Index(1), StackElement::Key("foo")]));
|
||||
assert!(stack.ends_with(&[StackElement::Key("foo")]));
|
||||
assert!(!stack.last_is_index());
|
||||
assert!(stack.get(0) == StackElement::Index(1));
|
||||
assert!(stack.get(1) == StackElement::Key("foo"));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_streaming_small(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let mut parser = Parser::new(
|
||||
r#"{
|
||||
"a": 1.0,
|
||||
"b": [
|
||||
true,
|
||||
"foo\nbar",
|
||||
{ "c": {"d": null} }
|
||||
]
|
||||
}"#
|
||||
.chars(),
|
||||
);
|
||||
loop {
|
||||
match parser.next() {
|
||||
None => return,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
#[bench]
|
||||
fn bench_small(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let _ = from_str(
|
||||
r#"{
|
||||
"a": 1.0,
|
||||
"b": [
|
||||
true,
|
||||
"foo\nbar",
|
||||
{ "c": {"d": null} }
|
||||
]
|
||||
}"#,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
fn big_json() -> string::String {
|
||||
let mut src = "[\n".to_string();
|
||||
for _ in 0..500 {
|
||||
src.push_str(
|
||||
r#"{ "a": true, "b": null, "c":3.1415, "d": "Hello world", "e": \
|
||||
[1,2,3]},"#,
|
||||
);
|
||||
}
|
||||
src.push_str("{}]");
|
||||
return src;
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_streaming_large(b: &mut Bencher) {
|
||||
let src = big_json();
|
||||
b.iter(|| {
|
||||
let mut parser = Parser::new(src.chars());
|
||||
loop {
|
||||
match parser.next() {
|
||||
None => return,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
#[bench]
|
||||
fn bench_large(b: &mut Bencher) {
|
||||
let src = big_json();
|
||||
b.iter(|| {
|
||||
let _ = from_str(&src);
|
||||
});
|
||||
}
|
110
compiler/rustc_serialize/src/leb128.rs
Normal file
110
compiler/rustc_serialize/src/leb128.rs
Normal file
|
@ -0,0 +1,110 @@
|
|||
macro_rules! impl_write_unsigned_leb128 {
|
||||
($fn_name:ident, $int_ty:ident) => {
|
||||
#[inline]
|
||||
pub fn $fn_name(out: &mut Vec<u8>, mut value: $int_ty) {
|
||||
loop {
|
||||
if value < 0x80 {
|
||||
out.push(value as u8);
|
||||
break;
|
||||
} else {
|
||||
out.push(((value & 0x7f) | 0x80) as u8);
|
||||
value >>= 7;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl_write_unsigned_leb128!(write_u16_leb128, u16);
|
||||
impl_write_unsigned_leb128!(write_u32_leb128, u32);
|
||||
impl_write_unsigned_leb128!(write_u64_leb128, u64);
|
||||
impl_write_unsigned_leb128!(write_u128_leb128, u128);
|
||||
impl_write_unsigned_leb128!(write_usize_leb128, usize);
|
||||
|
||||
macro_rules! impl_read_unsigned_leb128 {
|
||||
($fn_name:ident, $int_ty:ident) => {
|
||||
#[inline]
|
||||
pub fn $fn_name(slice: &[u8]) -> ($int_ty, usize) {
|
||||
let mut result = 0;
|
||||
let mut shift = 0;
|
||||
let mut position = 0;
|
||||
loop {
|
||||
let byte = slice[position];
|
||||
position += 1;
|
||||
if (byte & 0x80) == 0 {
|
||||
result |= (byte as $int_ty) << shift;
|
||||
return (result, position);
|
||||
} else {
|
||||
result |= ((byte & 0x7F) as $int_ty) << shift;
|
||||
}
|
||||
shift += 7;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl_read_unsigned_leb128!(read_u16_leb128, u16);
|
||||
impl_read_unsigned_leb128!(read_u32_leb128, u32);
|
||||
impl_read_unsigned_leb128!(read_u64_leb128, u64);
|
||||
impl_read_unsigned_leb128!(read_u128_leb128, u128);
|
||||
impl_read_unsigned_leb128!(read_usize_leb128, usize);
|
||||
|
||||
#[inline]
|
||||
/// encodes an integer using signed leb128 encoding and stores
|
||||
/// the result using a callback function.
|
||||
///
|
||||
/// The callback `write` is called once for each position
|
||||
/// that is to be written to with the byte to be encoded
|
||||
/// at that position.
|
||||
pub fn write_signed_leb128_to<W>(mut value: i128, mut write: W)
|
||||
where
|
||||
W: FnMut(u8),
|
||||
{
|
||||
loop {
|
||||
let mut byte = (value as u8) & 0x7f;
|
||||
value >>= 7;
|
||||
let more =
|
||||
!(((value == 0) && ((byte & 0x40) == 0)) || ((value == -1) && ((byte & 0x40) != 0)));
|
||||
|
||||
if more {
|
||||
byte |= 0x80; // Mark this byte to show that more bytes will follow.
|
||||
}
|
||||
|
||||
write(byte);
|
||||
|
||||
if !more {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn write_signed_leb128(out: &mut Vec<u8>, value: i128) {
|
||||
write_signed_leb128_to(value, |v| out.push(v))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn read_signed_leb128(data: &[u8], start_position: usize) -> (i128, usize) {
|
||||
let mut result = 0;
|
||||
let mut shift = 0;
|
||||
let mut position = start_position;
|
||||
let mut byte;
|
||||
|
||||
loop {
|
||||
byte = data[position];
|
||||
position += 1;
|
||||
result |= i128::from(byte & 0x7F) << shift;
|
||||
shift += 7;
|
||||
|
||||
if (byte & 0x80) == 0 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (shift < 64) && ((byte & 0x40) != 0) {
|
||||
// sign extend
|
||||
result |= -(1 << shift);
|
||||
}
|
||||
|
||||
(result, position - start_position)
|
||||
}
|
27
compiler/rustc_serialize/src/lib.rs
Normal file
27
compiler/rustc_serialize/src/lib.rs
Normal file
|
@ -0,0 +1,27 @@
|
|||
//! Support code for encoding and decoding types.
|
||||
|
||||
/*
|
||||
Core encoding and decoding interfaces.
|
||||
*/
|
||||
|
||||
#![doc(
|
||||
html_root_url = "https://doc.rust-lang.org/nightly/",
|
||||
html_playground_url = "https://play.rust-lang.org/",
|
||||
test(attr(allow(unused_variables), deny(warnings)))
|
||||
)]
|
||||
#![feature(box_syntax)]
|
||||
#![feature(never_type)]
|
||||
#![feature(nll)]
|
||||
#![feature(associated_type_bounds)]
|
||||
#![cfg_attr(test, feature(test))]
|
||||
#![allow(rustc::internal)]
|
||||
|
||||
pub use self::serialize::{Decodable, Decoder, Encodable, Encoder};
|
||||
|
||||
mod collection_impls;
|
||||
mod serialize;
|
||||
|
||||
pub mod json;
|
||||
|
||||
pub mod leb128;
|
||||
pub mod opaque;
|
318
compiler/rustc_serialize/src/opaque.rs
Normal file
318
compiler/rustc_serialize/src/opaque.rs
Normal file
|
@ -0,0 +1,318 @@
|
|||
use crate::leb128::{self, read_signed_leb128, write_signed_leb128};
|
||||
use crate::serialize;
|
||||
use std::borrow::Cow;
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Encoder
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
pub type EncodeResult = Result<(), !>;
|
||||
|
||||
pub struct Encoder {
|
||||
pub data: Vec<u8>,
|
||||
}
|
||||
|
||||
impl Encoder {
|
||||
pub fn new(data: Vec<u8>) -> Encoder {
|
||||
Encoder { data }
|
||||
}
|
||||
|
||||
pub fn into_inner(self) -> Vec<u8> {
|
||||
self.data
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn emit_raw_bytes(&mut self, s: &[u8]) {
|
||||
self.data.extend_from_slice(s);
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! write_uleb128 {
|
||||
($enc:expr, $value:expr, $fun:ident) => {{
|
||||
leb128::$fun(&mut $enc.data, $value);
|
||||
Ok(())
|
||||
}};
|
||||
}
|
||||
|
||||
macro_rules! write_sleb128 {
|
||||
($enc:expr, $value:expr) => {{
|
||||
write_signed_leb128(&mut $enc.data, $value as i128);
|
||||
Ok(())
|
||||
}};
|
||||
}
|
||||
|
||||
impl serialize::Encoder for Encoder {
|
||||
type Error = !;
|
||||
|
||||
#[inline]
|
||||
fn emit_unit(&mut self) -> EncodeResult {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn emit_usize(&mut self, v: usize) -> EncodeResult {
|
||||
write_uleb128!(self, v, write_usize_leb128)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn emit_u128(&mut self, v: u128) -> EncodeResult {
|
||||
write_uleb128!(self, v, write_u128_leb128)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn emit_u64(&mut self, v: u64) -> EncodeResult {
|
||||
write_uleb128!(self, v, write_u64_leb128)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn emit_u32(&mut self, v: u32) -> EncodeResult {
|
||||
write_uleb128!(self, v, write_u32_leb128)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn emit_u16(&mut self, v: u16) -> EncodeResult {
|
||||
write_uleb128!(self, v, write_u16_leb128)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn emit_u8(&mut self, v: u8) -> EncodeResult {
|
||||
self.data.push(v);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn emit_isize(&mut self, v: isize) -> EncodeResult {
|
||||
write_sleb128!(self, v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn emit_i128(&mut self, v: i128) -> EncodeResult {
|
||||
write_sleb128!(self, v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn emit_i64(&mut self, v: i64) -> EncodeResult {
|
||||
write_sleb128!(self, v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn emit_i32(&mut self, v: i32) -> EncodeResult {
|
||||
write_sleb128!(self, v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn emit_i16(&mut self, v: i16) -> EncodeResult {
|
||||
write_sleb128!(self, v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn emit_i8(&mut self, v: i8) -> EncodeResult {
|
||||
let as_u8: u8 = unsafe { ::std::mem::transmute(v) };
|
||||
self.emit_u8(as_u8)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn emit_bool(&mut self, v: bool) -> EncodeResult {
|
||||
self.emit_u8(if v { 1 } else { 0 })
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn emit_f64(&mut self, v: f64) -> EncodeResult {
|
||||
let as_u64: u64 = v.to_bits();
|
||||
self.emit_u64(as_u64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn emit_f32(&mut self, v: f32) -> EncodeResult {
|
||||
let as_u32: u32 = v.to_bits();
|
||||
self.emit_u32(as_u32)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn emit_char(&mut self, v: char) -> EncodeResult {
|
||||
self.emit_u32(v as u32)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn emit_str(&mut self, v: &str) -> EncodeResult {
|
||||
self.emit_usize(v.len())?;
|
||||
self.emit_raw_bytes(v.as_bytes());
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Encoder {
|
||||
#[inline]
|
||||
pub fn position(&self) -> usize {
|
||||
self.data.len()
|
||||
}
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Decoder
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
pub struct Decoder<'a> {
|
||||
pub data: &'a [u8],
|
||||
position: usize,
|
||||
}
|
||||
|
||||
impl<'a> Decoder<'a> {
|
||||
#[inline]
|
||||
pub fn new(data: &'a [u8], position: usize) -> Decoder<'a> {
|
||||
Decoder { data, position }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn position(&self) -> usize {
|
||||
self.position
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn set_position(&mut self, pos: usize) {
|
||||
self.position = pos
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn advance(&mut self, bytes: usize) {
|
||||
self.position += bytes;
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn read_raw_bytes(&mut self, s: &mut [u8]) -> Result<(), String> {
|
||||
let start = self.position;
|
||||
let end = start + s.len();
|
||||
|
||||
s.copy_from_slice(&self.data[start..end]);
|
||||
|
||||
self.position = end;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! read_uleb128 {
|
||||
($dec:expr, $fun:ident) => {{
|
||||
let (value, bytes_read) = leb128::$fun(&$dec.data[$dec.position..]);
|
||||
$dec.position += bytes_read;
|
||||
Ok(value)
|
||||
}};
|
||||
}
|
||||
|
||||
macro_rules! read_sleb128 {
|
||||
($dec:expr, $t:ty) => {{
|
||||
let (value, bytes_read) = read_signed_leb128($dec.data, $dec.position);
|
||||
$dec.position += bytes_read;
|
||||
Ok(value as $t)
|
||||
}};
|
||||
}
|
||||
|
||||
impl<'a> serialize::Decoder for Decoder<'a> {
|
||||
type Error = String;
|
||||
|
||||
#[inline]
|
||||
fn read_nil(&mut self) -> Result<(), Self::Error> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn read_u128(&mut self) -> Result<u128, Self::Error> {
|
||||
read_uleb128!(self, read_u128_leb128)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn read_u64(&mut self) -> Result<u64, Self::Error> {
|
||||
read_uleb128!(self, read_u64_leb128)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn read_u32(&mut self) -> Result<u32, Self::Error> {
|
||||
read_uleb128!(self, read_u32_leb128)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn read_u16(&mut self) -> Result<u16, Self::Error> {
|
||||
read_uleb128!(self, read_u16_leb128)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn read_u8(&mut self) -> Result<u8, Self::Error> {
|
||||
let value = self.data[self.position];
|
||||
self.position += 1;
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn read_usize(&mut self) -> Result<usize, Self::Error> {
|
||||
read_uleb128!(self, read_usize_leb128)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn read_i128(&mut self) -> Result<i128, Self::Error> {
|
||||
read_sleb128!(self, i128)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn read_i64(&mut self) -> Result<i64, Self::Error> {
|
||||
read_sleb128!(self, i64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn read_i32(&mut self) -> Result<i32, Self::Error> {
|
||||
read_sleb128!(self, i32)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn read_i16(&mut self) -> Result<i16, Self::Error> {
|
||||
read_sleb128!(self, i16)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn read_i8(&mut self) -> Result<i8, Self::Error> {
|
||||
let as_u8 = self.data[self.position];
|
||||
self.position += 1;
|
||||
unsafe { Ok(::std::mem::transmute(as_u8)) }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn read_isize(&mut self) -> Result<isize, Self::Error> {
|
||||
read_sleb128!(self, isize)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn read_bool(&mut self) -> Result<bool, Self::Error> {
|
||||
let value = self.read_u8()?;
|
||||
Ok(value != 0)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn read_f64(&mut self) -> Result<f64, Self::Error> {
|
||||
let bits = self.read_u64()?;
|
||||
Ok(f64::from_bits(bits))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn read_f32(&mut self) -> Result<f32, Self::Error> {
|
||||
let bits = self.read_u32()?;
|
||||
Ok(f32::from_bits(bits))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn read_char(&mut self) -> Result<char, Self::Error> {
|
||||
let bits = self.read_u32()?;
|
||||
Ok(::std::char::from_u32(bits).unwrap())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn read_str(&mut self) -> Result<Cow<'_, str>, Self::Error> {
|
||||
let len = self.read_usize()?;
|
||||
let s = ::std::str::from_utf8(&self.data[self.position..self.position + len]).unwrap();
|
||||
self.position += len;
|
||||
Ok(Cow::Borrowed(s))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn error(&mut self, err: &str) -> Self::Error {
|
||||
err.to_string()
|
||||
}
|
||||
}
|
782
compiler/rustc_serialize/src/serialize.rs
Normal file
782
compiler/rustc_serialize/src/serialize.rs
Normal file
|
@ -0,0 +1,782 @@
|
|||
//! Support code for encoding and decoding types.
|
||||
|
||||
/*
|
||||
Core encoding and decoding interfaces.
|
||||
*/
|
||||
|
||||
use std::borrow::Cow;
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::marker::PhantomData;
|
||||
use std::path;
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub trait Encoder {
|
||||
type Error;
|
||||
|
||||
// Primitive types:
|
||||
fn emit_unit(&mut self) -> Result<(), Self::Error>;
|
||||
fn emit_usize(&mut self, v: usize) -> Result<(), Self::Error>;
|
||||
fn emit_u128(&mut self, v: u128) -> Result<(), Self::Error>;
|
||||
fn emit_u64(&mut self, v: u64) -> Result<(), Self::Error>;
|
||||
fn emit_u32(&mut self, v: u32) -> Result<(), Self::Error>;
|
||||
fn emit_u16(&mut self, v: u16) -> Result<(), Self::Error>;
|
||||
fn emit_u8(&mut self, v: u8) -> Result<(), Self::Error>;
|
||||
fn emit_isize(&mut self, v: isize) -> Result<(), Self::Error>;
|
||||
fn emit_i128(&mut self, v: i128) -> Result<(), Self::Error>;
|
||||
fn emit_i64(&mut self, v: i64) -> Result<(), Self::Error>;
|
||||
fn emit_i32(&mut self, v: i32) -> Result<(), Self::Error>;
|
||||
fn emit_i16(&mut self, v: i16) -> Result<(), Self::Error>;
|
||||
fn emit_i8(&mut self, v: i8) -> Result<(), Self::Error>;
|
||||
fn emit_bool(&mut self, v: bool) -> Result<(), Self::Error>;
|
||||
fn emit_f64(&mut self, v: f64) -> Result<(), Self::Error>;
|
||||
fn emit_f32(&mut self, v: f32) -> Result<(), Self::Error>;
|
||||
fn emit_char(&mut self, v: char) -> Result<(), Self::Error>;
|
||||
fn emit_str(&mut self, v: &str) -> Result<(), Self::Error>;
|
||||
|
||||
// Compound types:
|
||||
#[inline]
|
||||
fn emit_enum<F>(&mut self, _name: &str, f: F) -> Result<(), Self::Error>
|
||||
where
|
||||
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
|
||||
{
|
||||
f(self)
|
||||
}
|
||||
|
||||
fn emit_enum_variant<F>(
|
||||
&mut self,
|
||||
_v_name: &str,
|
||||
v_id: usize,
|
||||
_len: usize,
|
||||
f: F,
|
||||
) -> Result<(), Self::Error>
|
||||
where
|
||||
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
|
||||
{
|
||||
self.emit_usize(v_id)?;
|
||||
f(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn emit_enum_variant_arg<F>(&mut self, _a_idx: usize, f: F) -> Result<(), Self::Error>
|
||||
where
|
||||
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
|
||||
{
|
||||
f(self)
|
||||
}
|
||||
|
||||
fn emit_enum_struct_variant<F>(
|
||||
&mut self,
|
||||
v_name: &str,
|
||||
v_id: usize,
|
||||
len: usize,
|
||||
f: F,
|
||||
) -> Result<(), Self::Error>
|
||||
where
|
||||
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
|
||||
{
|
||||
self.emit_enum_variant(v_name, v_id, len, f)
|
||||
}
|
||||
|
||||
fn emit_enum_struct_variant_field<F>(
|
||||
&mut self,
|
||||
_f_name: &str,
|
||||
f_idx: usize,
|
||||
f: F,
|
||||
) -> Result<(), Self::Error>
|
||||
where
|
||||
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
|
||||
{
|
||||
self.emit_enum_variant_arg(f_idx, f)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn emit_struct<F>(&mut self, _name: &str, _len: usize, f: F) -> Result<(), Self::Error>
|
||||
where
|
||||
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
|
||||
{
|
||||
f(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn emit_struct_field<F>(
|
||||
&mut self,
|
||||
_f_name: &str,
|
||||
_f_idx: usize,
|
||||
f: F,
|
||||
) -> Result<(), Self::Error>
|
||||
where
|
||||
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
|
||||
{
|
||||
f(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn emit_tuple<F>(&mut self, _len: usize, f: F) -> Result<(), Self::Error>
|
||||
where
|
||||
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
|
||||
{
|
||||
f(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn emit_tuple_arg<F>(&mut self, _idx: usize, f: F) -> Result<(), Self::Error>
|
||||
where
|
||||
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
|
||||
{
|
||||
f(self)
|
||||
}
|
||||
|
||||
fn emit_tuple_struct<F>(&mut self, _name: &str, len: usize, f: F) -> Result<(), Self::Error>
|
||||
where
|
||||
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
|
||||
{
|
||||
self.emit_tuple(len, f)
|
||||
}
|
||||
|
||||
fn emit_tuple_struct_arg<F>(&mut self, f_idx: usize, f: F) -> Result<(), Self::Error>
|
||||
where
|
||||
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
|
||||
{
|
||||
self.emit_tuple_arg(f_idx, f)
|
||||
}
|
||||
|
||||
// Specialized types:
|
||||
fn emit_option<F>(&mut self, f: F) -> Result<(), Self::Error>
|
||||
where
|
||||
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
|
||||
{
|
||||
self.emit_enum("Option", f)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn emit_option_none(&mut self) -> Result<(), Self::Error> {
|
||||
self.emit_enum_variant("None", 0, 0, |_| Ok(()))
|
||||
}
|
||||
|
||||
fn emit_option_some<F>(&mut self, f: F) -> Result<(), Self::Error>
|
||||
where
|
||||
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
|
||||
{
|
||||
self.emit_enum_variant("Some", 1, 1, f)
|
||||
}
|
||||
|
||||
fn emit_seq<F>(&mut self, len: usize, f: F) -> Result<(), Self::Error>
|
||||
where
|
||||
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
|
||||
{
|
||||
self.emit_usize(len)?;
|
||||
f(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn emit_seq_elt<F>(&mut self, _idx: usize, f: F) -> Result<(), Self::Error>
|
||||
where
|
||||
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
|
||||
{
|
||||
f(self)
|
||||
}
|
||||
|
||||
fn emit_map<F>(&mut self, len: usize, f: F) -> Result<(), Self::Error>
|
||||
where
|
||||
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
|
||||
{
|
||||
self.emit_usize(len)?;
|
||||
f(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn emit_map_elt_key<F>(&mut self, _idx: usize, f: F) -> Result<(), Self::Error>
|
||||
where
|
||||
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
|
||||
{
|
||||
f(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn emit_map_elt_val<F>(&mut self, _idx: usize, f: F) -> Result<(), Self::Error>
|
||||
where
|
||||
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
|
||||
{
|
||||
f(self)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Decoder {
|
||||
type Error;
|
||||
|
||||
// Primitive types:
|
||||
fn read_nil(&mut self) -> Result<(), Self::Error>;
|
||||
fn read_usize(&mut self) -> Result<usize, Self::Error>;
|
||||
fn read_u128(&mut self) -> Result<u128, Self::Error>;
|
||||
fn read_u64(&mut self) -> Result<u64, Self::Error>;
|
||||
fn read_u32(&mut self) -> Result<u32, Self::Error>;
|
||||
fn read_u16(&mut self) -> Result<u16, Self::Error>;
|
||||
fn read_u8(&mut self) -> Result<u8, Self::Error>;
|
||||
fn read_isize(&mut self) -> Result<isize, Self::Error>;
|
||||
fn read_i128(&mut self) -> Result<i128, Self::Error>;
|
||||
fn read_i64(&mut self) -> Result<i64, Self::Error>;
|
||||
fn read_i32(&mut self) -> Result<i32, Self::Error>;
|
||||
fn read_i16(&mut self) -> Result<i16, Self::Error>;
|
||||
fn read_i8(&mut self) -> Result<i8, Self::Error>;
|
||||
fn read_bool(&mut self) -> Result<bool, Self::Error>;
|
||||
fn read_f64(&mut self) -> Result<f64, Self::Error>;
|
||||
fn read_f32(&mut self) -> Result<f32, Self::Error>;
|
||||
fn read_char(&mut self) -> Result<char, Self::Error>;
|
||||
fn read_str(&mut self) -> Result<Cow<'_, str>, Self::Error>;
|
||||
|
||||
// Compound types:
|
||||
#[inline]
|
||||
fn read_enum<T, F>(&mut self, _name: &str, f: F) -> Result<T, Self::Error>
|
||||
where
|
||||
F: FnOnce(&mut Self) -> Result<T, Self::Error>,
|
||||
{
|
||||
f(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn read_enum_variant<T, F>(&mut self, _names: &[&str], mut f: F) -> Result<T, Self::Error>
|
||||
where
|
||||
F: FnMut(&mut Self, usize) -> Result<T, Self::Error>,
|
||||
{
|
||||
let disr = self.read_usize()?;
|
||||
f(self, disr)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn read_enum_variant_arg<T, F>(&mut self, _a_idx: usize, f: F) -> Result<T, Self::Error>
|
||||
where
|
||||
F: FnOnce(&mut Self) -> Result<T, Self::Error>,
|
||||
{
|
||||
f(self)
|
||||
}
|
||||
|
||||
fn read_enum_struct_variant<T, F>(&mut self, names: &[&str], f: F) -> Result<T, Self::Error>
|
||||
where
|
||||
F: FnMut(&mut Self, usize) -> Result<T, Self::Error>,
|
||||
{
|
||||
self.read_enum_variant(names, f)
|
||||
}
|
||||
|
||||
fn read_enum_struct_variant_field<T, F>(
|
||||
&mut self,
|
||||
_f_name: &str,
|
||||
f_idx: usize,
|
||||
f: F,
|
||||
) -> Result<T, Self::Error>
|
||||
where
|
||||
F: FnOnce(&mut Self) -> Result<T, Self::Error>,
|
||||
{
|
||||
self.read_enum_variant_arg(f_idx, f)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn read_struct<T, F>(&mut self, _s_name: &str, _len: usize, f: F) -> Result<T, Self::Error>
|
||||
where
|
||||
F: FnOnce(&mut Self) -> Result<T, Self::Error>,
|
||||
{
|
||||
f(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn read_struct_field<T, F>(
|
||||
&mut self,
|
||||
_f_name: &str,
|
||||
_f_idx: usize,
|
||||
f: F,
|
||||
) -> Result<T, Self::Error>
|
||||
where
|
||||
F: FnOnce(&mut Self) -> Result<T, Self::Error>,
|
||||
{
|
||||
f(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn read_tuple<T, F>(&mut self, _len: usize, f: F) -> Result<T, Self::Error>
|
||||
where
|
||||
F: FnOnce(&mut Self) -> Result<T, Self::Error>,
|
||||
{
|
||||
f(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn read_tuple_arg<T, F>(&mut self, _a_idx: usize, f: F) -> Result<T, Self::Error>
|
||||
where
|
||||
F: FnOnce(&mut Self) -> Result<T, Self::Error>,
|
||||
{
|
||||
f(self)
|
||||
}
|
||||
|
||||
fn read_tuple_struct<T, F>(&mut self, _s_name: &str, len: usize, f: F) -> Result<T, Self::Error>
|
||||
where
|
||||
F: FnOnce(&mut Self) -> Result<T, Self::Error>,
|
||||
{
|
||||
self.read_tuple(len, f)
|
||||
}
|
||||
|
||||
fn read_tuple_struct_arg<T, F>(&mut self, a_idx: usize, f: F) -> Result<T, Self::Error>
|
||||
where
|
||||
F: FnOnce(&mut Self) -> Result<T, Self::Error>,
|
||||
{
|
||||
self.read_tuple_arg(a_idx, f)
|
||||
}
|
||||
|
||||
// Specialized types:
|
||||
fn read_option<T, F>(&mut self, mut f: F) -> Result<T, Self::Error>
|
||||
where
|
||||
F: FnMut(&mut Self, bool) -> Result<T, Self::Error>,
|
||||
{
|
||||
self.read_enum("Option", move |this| {
|
||||
this.read_enum_variant(&["None", "Some"], move |this, idx| match idx {
|
||||
0 => f(this, false),
|
||||
1 => f(this, true),
|
||||
_ => Err(this.error("read_option: expected 0 for None or 1 for Some")),
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
fn read_seq<T, F>(&mut self, f: F) -> Result<T, Self::Error>
|
||||
where
|
||||
F: FnOnce(&mut Self, usize) -> Result<T, Self::Error>,
|
||||
{
|
||||
let len = self.read_usize()?;
|
||||
f(self, len)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn read_seq_elt<T, F>(&mut self, _idx: usize, f: F) -> Result<T, Self::Error>
|
||||
where
|
||||
F: FnOnce(&mut Self) -> Result<T, Self::Error>,
|
||||
{
|
||||
f(self)
|
||||
}
|
||||
|
||||
fn read_map<T, F>(&mut self, f: F) -> Result<T, Self::Error>
|
||||
where
|
||||
F: FnOnce(&mut Self, usize) -> Result<T, Self::Error>,
|
||||
{
|
||||
let len = self.read_usize()?;
|
||||
f(self, len)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn read_map_elt_key<T, F>(&mut self, _idx: usize, f: F) -> Result<T, Self::Error>
|
||||
where
|
||||
F: FnOnce(&mut Self) -> Result<T, Self::Error>,
|
||||
{
|
||||
f(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn read_map_elt_val<T, F>(&mut self, _idx: usize, f: F) -> Result<T, Self::Error>
|
||||
where
|
||||
F: FnOnce(&mut Self) -> Result<T, Self::Error>,
|
||||
{
|
||||
f(self)
|
||||
}
|
||||
|
||||
// Failure
|
||||
fn error(&mut self, err: &str) -> Self::Error;
|
||||
}
|
||||
|
||||
/// Trait for types that can be serialized
|
||||
///
|
||||
/// This can be implemented using the `Encodable`, `TyEncodable` and
|
||||
/// `MetadataEncodable` macros.
|
||||
///
|
||||
/// * `Encodable` should be used in crates that don't depend on
|
||||
/// `rustc_middle`.
|
||||
/// * `MetadataEncodable` is used in `rustc_metadata` for types that contain
|
||||
/// `rustc_metadata::rmeta::Lazy`.
|
||||
/// * `TyEncodable` should be used for types that are only serialized in crate
|
||||
/// metadata or the incremental cache. This is most types in `rustc_middle`.
|
||||
pub trait Encodable<S: Encoder> {
|
||||
fn encode(&self, s: &mut S) -> Result<(), S::Error>;
|
||||
}
|
||||
|
||||
/// Trait for types that can be deserialized
|
||||
///
|
||||
/// This can be implemented using the `Decodable`, `TyDecodable` and
|
||||
/// `MetadataDecodable` macros.
|
||||
///
|
||||
/// * `Decodable` should be used in crates that don't depend on
|
||||
/// `rustc_middle`.
|
||||
/// * `MetadataDecodable` is used in `rustc_metadata` for types that contain
|
||||
/// `rustc_metadata::rmeta::Lazy`.
|
||||
/// * `TyDecodable` should be used for types that are only serialized in crate
|
||||
/// metadata or the incremental cache. This is most types in `rustc_middle`.
|
||||
pub trait Decodable<D: Decoder>: Sized {
|
||||
fn decode(d: &mut D) -> Result<Self, D::Error>;
|
||||
}
|
||||
|
||||
macro_rules! direct_serialize_impls {
|
||||
($($ty:ident $emit_method:ident $read_method:ident),*) => {
|
||||
$(
|
||||
impl<S: Encoder> Encodable<S> for $ty {
|
||||
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
||||
s.$emit_method(*self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Decoder> Decodable<D> for $ty {
|
||||
fn decode(d: &mut D) -> Result<$ty, D::Error> {
|
||||
d.$read_method()
|
||||
}
|
||||
}
|
||||
)*
|
||||
}
|
||||
}
|
||||
|
||||
direct_serialize_impls! {
|
||||
usize emit_usize read_usize,
|
||||
u8 emit_u8 read_u8,
|
||||
u16 emit_u16 read_u16,
|
||||
u32 emit_u32 read_u32,
|
||||
u64 emit_u64 read_u64,
|
||||
u128 emit_u128 read_u128,
|
||||
isize emit_isize read_isize,
|
||||
i8 emit_i8 read_i8,
|
||||
i16 emit_i16 read_i16,
|
||||
i32 emit_i32 read_i32,
|
||||
i64 emit_i64 read_i64,
|
||||
i128 emit_i128 read_i128,
|
||||
f32 emit_f32 read_f32,
|
||||
f64 emit_f64 read_f64,
|
||||
bool emit_bool read_bool,
|
||||
char emit_char read_char
|
||||
}
|
||||
|
||||
impl<S: Encoder> Encodable<S> for ::std::num::NonZeroU32 {
|
||||
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
||||
s.emit_u32(self.get())
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Decoder> Decodable<D> for ::std::num::NonZeroU32 {
|
||||
fn decode(d: &mut D) -> Result<Self, D::Error> {
|
||||
d.read_u32().map(|d| ::std::num::NonZeroU32::new(d).unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: Encoder> Encodable<S> for str {
|
||||
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
||||
s.emit_str(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: Encoder> Encodable<S> for &str {
|
||||
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
||||
s.emit_str(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: Encoder> Encodable<S> for String {
|
||||
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
||||
s.emit_str(&self[..])
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Decoder> Decodable<D> for String {
|
||||
fn decode(d: &mut D) -> Result<String, D::Error> {
|
||||
Ok(d.read_str()?.into_owned())
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: Encoder> Encodable<S> for () {
|
||||
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
||||
s.emit_unit()
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Decoder> Decodable<D> for () {
|
||||
fn decode(d: &mut D) -> Result<(), D::Error> {
|
||||
d.read_nil()
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: Encoder, T> Encodable<S> for PhantomData<T> {
|
||||
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
||||
s.emit_unit()
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Decoder, T> Decodable<D> for PhantomData<T> {
|
||||
fn decode(d: &mut D) -> Result<PhantomData<T>, D::Error> {
|
||||
d.read_nil()?;
|
||||
Ok(PhantomData)
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Decoder, T: Decodable<D>> Decodable<D> for Box<[T]> {
|
||||
fn decode(d: &mut D) -> Result<Box<[T]>, D::Error> {
|
||||
let v: Vec<T> = Decodable::decode(d)?;
|
||||
Ok(v.into_boxed_slice())
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: Encoder, T: Encodable<S>> Encodable<S> for Rc<T> {
|
||||
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
||||
(**self).encode(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Decoder, T: Decodable<D>> Decodable<D> for Rc<T> {
|
||||
fn decode(d: &mut D) -> Result<Rc<T>, D::Error> {
|
||||
Ok(Rc::new(Decodable::decode(d)?))
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: Encoder, T: Encodable<S>> Encodable<S> for [T] {
|
||||
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
||||
s.emit_seq(self.len(), |s| {
|
||||
for (i, e) in self.iter().enumerate() {
|
||||
s.emit_seq_elt(i, |s| e.encode(s))?
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: Encoder, T: Encodable<S>> Encodable<S> for Vec<T> {
|
||||
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
||||
s.emit_seq(self.len(), |s| {
|
||||
for (i, e) in self.iter().enumerate() {
|
||||
s.emit_seq_elt(i, |s| e.encode(s))?
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Decoder, T: Decodable<D>> Decodable<D> for Vec<T> {
|
||||
fn decode(d: &mut D) -> Result<Vec<T>, D::Error> {
|
||||
d.read_seq(|d, len| {
|
||||
let mut v = Vec::with_capacity(len);
|
||||
for i in 0..len {
|
||||
v.push(d.read_seq_elt(i, |d| Decodable::decode(d))?);
|
||||
}
|
||||
Ok(v)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: Encoder> Encodable<S> for [u8; 20] {
|
||||
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
||||
s.emit_seq(self.len(), |s| {
|
||||
for (i, e) in self.iter().enumerate() {
|
||||
s.emit_seq_elt(i, |s| e.encode(s))?
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Decoder> Decodable<D> for [u8; 20] {
|
||||
fn decode(d: &mut D) -> Result<[u8; 20], D::Error> {
|
||||
d.read_seq(|d, len| {
|
||||
assert!(len == 20);
|
||||
let mut v = [0u8; 20];
|
||||
for i in 0..len {
|
||||
v[i] = d.read_seq_elt(i, |d| Decodable::decode(d))?;
|
||||
}
|
||||
Ok(v)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, S: Encoder, T: Encodable<S>> Encodable<S> for Cow<'a, [T]>
|
||||
where
|
||||
[T]: ToOwned<Owned = Vec<T>>,
|
||||
{
|
||||
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
||||
s.emit_seq(self.len(), |s| {
|
||||
for (i, e) in self.iter().enumerate() {
|
||||
s.emit_seq_elt(i, |s| e.encode(s))?
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Decoder, T: Decodable<D> + ToOwned> Decodable<D> for Cow<'static, [T]>
|
||||
where
|
||||
[T]: ToOwned<Owned = Vec<T>>,
|
||||
{
|
||||
fn decode(d: &mut D) -> Result<Cow<'static, [T]>, D::Error> {
|
||||
d.read_seq(|d, len| {
|
||||
let mut v = Vec::with_capacity(len);
|
||||
for i in 0..len {
|
||||
v.push(d.read_seq_elt(i, |d| Decodable::decode(d))?);
|
||||
}
|
||||
Ok(Cow::Owned(v))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: Encoder, T: Encodable<S>> Encodable<S> for Option<T> {
|
||||
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
||||
s.emit_option(|s| match *self {
|
||||
None => s.emit_option_none(),
|
||||
Some(ref v) => s.emit_option_some(|s| v.encode(s)),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Decoder, T: Decodable<D>> Decodable<D> for Option<T> {
|
||||
fn decode(d: &mut D) -> Result<Option<T>, D::Error> {
|
||||
d.read_option(|d, b| if b { Ok(Some(Decodable::decode(d)?)) } else { Ok(None) })
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: Encoder, T1: Encodable<S>, T2: Encodable<S>> Encodable<S> for Result<T1, T2> {
|
||||
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
||||
s.emit_enum("Result", |s| match *self {
|
||||
Ok(ref v) => {
|
||||
s.emit_enum_variant("Ok", 0, 1, |s| s.emit_enum_variant_arg(0, |s| v.encode(s)))
|
||||
}
|
||||
Err(ref v) => {
|
||||
s.emit_enum_variant("Err", 1, 1, |s| s.emit_enum_variant_arg(0, |s| v.encode(s)))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Decoder, T1: Decodable<D>, T2: Decodable<D>> Decodable<D> for Result<T1, T2> {
|
||||
fn decode(d: &mut D) -> Result<Result<T1, T2>, D::Error> {
|
||||
d.read_enum("Result", |d| {
|
||||
d.read_enum_variant(&["Ok", "Err"], |d, disr| match disr {
|
||||
0 => Ok(Ok(d.read_enum_variant_arg(0, |d| T1::decode(d))?)),
|
||||
1 => Ok(Err(d.read_enum_variant_arg(0, |d| T2::decode(d))?)),
|
||||
_ => {
|
||||
panic!(
|
||||
"Encountered invalid discriminant while \
|
||||
decoding `Result`."
|
||||
);
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! peel {
|
||||
($name:ident, $($other:ident,)*) => (tuple! { $($other,)* })
|
||||
}
|
||||
|
||||
/// Evaluates to the number of tokens passed to it.
|
||||
///
|
||||
/// Logarithmic counting: every one or two recursive expansions, the number of
|
||||
/// tokens to count is divided by two, instead of being reduced by one.
|
||||
/// Therefore, the recursion depth is the binary logarithm of the number of
|
||||
/// tokens to count, and the expanded tree is likewise very small.
|
||||
macro_rules! count {
|
||||
() => (0usize);
|
||||
($one:tt) => (1usize);
|
||||
($($pairs:tt $_p:tt)*) => (count!($($pairs)*) << 1usize);
|
||||
($odd:tt $($rest:tt)*) => (count!($($rest)*) | 1usize);
|
||||
}
|
||||
|
||||
macro_rules! tuple {
|
||||
() => ();
|
||||
( $($name:ident,)+ ) => (
|
||||
impl<D: Decoder, $($name: Decodable<D>),+> Decodable<D> for ($($name,)+) {
|
||||
#[allow(non_snake_case)]
|
||||
fn decode(d: &mut D) -> Result<($($name,)+), D::Error> {
|
||||
let len: usize = count!($($name)+);
|
||||
d.read_tuple(len, |d| {
|
||||
let mut i = 0;
|
||||
let ret = ($(d.read_tuple_arg({ i+=1; i-1 }, |d| -> Result<$name, D::Error> {
|
||||
Decodable::decode(d)
|
||||
})?,)+);
|
||||
Ok(ret)
|
||||
})
|
||||
}
|
||||
}
|
||||
impl<S: Encoder, $($name: Encodable<S>),+> Encodable<S> for ($($name,)+) {
|
||||
#[allow(non_snake_case)]
|
||||
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
||||
let ($(ref $name,)+) = *self;
|
||||
let mut n = 0;
|
||||
$(let $name = $name; n += 1;)+
|
||||
s.emit_tuple(n, |s| {
|
||||
let mut i = 0;
|
||||
$(s.emit_tuple_arg({ i+=1; i-1 }, |s| $name.encode(s))?;)+
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
}
|
||||
peel! { $($name,)+ }
|
||||
)
|
||||
}
|
||||
|
||||
tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, }
|
||||
|
||||
impl<S: Encoder> Encodable<S> for path::Path {
|
||||
fn encode(&self, e: &mut S) -> Result<(), S::Error> {
|
||||
self.to_str().unwrap().encode(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: Encoder> Encodable<S> for path::PathBuf {
|
||||
fn encode(&self, e: &mut S) -> Result<(), S::Error> {
|
||||
path::Path::encode(self, e)
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Decoder> Decodable<D> for path::PathBuf {
|
||||
fn decode(d: &mut D) -> Result<path::PathBuf, D::Error> {
|
||||
let bytes: String = Decodable::decode(d)?;
|
||||
Ok(path::PathBuf::from(bytes))
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: Encoder, T: Encodable<S> + Copy> Encodable<S> for Cell<T> {
|
||||
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
||||
self.get().encode(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Decoder, T: Decodable<D> + Copy> Decodable<D> for Cell<T> {
|
||||
fn decode(d: &mut D) -> Result<Cell<T>, D::Error> {
|
||||
Ok(Cell::new(Decodable::decode(d)?))
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: #15036
|
||||
// Should use `try_borrow`, returning a
|
||||
// `encoder.error("attempting to Encode borrowed RefCell")`
|
||||
// from `encode` when `try_borrow` returns `None`.
|
||||
|
||||
impl<S: Encoder, T: Encodable<S>> Encodable<S> for RefCell<T> {
|
||||
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
||||
self.borrow().encode(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Decoder, T: Decodable<D>> Decodable<D> for RefCell<T> {
|
||||
fn decode(d: &mut D) -> Result<RefCell<T>, D::Error> {
|
||||
Ok(RefCell::new(Decodable::decode(d)?))
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: Encoder, T: Encodable<S>> Encodable<S> for Arc<T> {
|
||||
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
||||
(**self).encode(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Decoder, T: Decodable<D>> Decodable<D> for Arc<T> {
|
||||
fn decode(d: &mut D) -> Result<Arc<T>, D::Error> {
|
||||
Ok(Arc::new(Decodable::decode(d)?))
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: Encoder, T: ?Sized + Encodable<S>> Encodable<S> for Box<T> {
|
||||
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
||||
(**self).encode(s)
|
||||
}
|
||||
}
|
||||
impl<D: Decoder, T: Decodable<D>> Decodable<D> for Box<T> {
|
||||
fn decode(d: &mut D) -> Result<Box<T>, D::Error> {
|
||||
Ok(box Decodable::decode(d)?)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue