Skip to content

serialize: Use assoc types + less old_orphan_check #20514

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 5, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
465 changes: 464 additions & 1 deletion src/librbml/lib.rs

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions src/librustc/middle/astencode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,18 +263,27 @@ trait def_id_encoder_helpers {
fn emit_def_id(&mut self, did: ast::DefId);
}

#[cfg(stage0)]
impl<S:serialize::Encoder<E>, E> def_id_encoder_helpers for S {
fn emit_def_id(&mut self, did: ast::DefId) {
did.encode(self).ok().unwrap()
}
}

#[cfg(not(stage0))]
impl<S:serialize::Encoder> def_id_encoder_helpers for S {
fn emit_def_id(&mut self, did: ast::DefId) {
did.encode(self).ok().unwrap()
}
}

trait def_id_decoder_helpers {
fn read_def_id(&mut self, dcx: &DecodeContext) -> ast::DefId;
fn read_def_id_nodcx(&mut self,
cdata: &cstore::crate_metadata) -> ast::DefId;
}

#[cfg(stage0)]
impl<D:serialize::Decoder<E>, E> def_id_decoder_helpers for D {
fn read_def_id(&mut self, dcx: &DecodeContext) -> ast::DefId {
let did: ast::DefId = Decodable::decode(self).ok().unwrap();
Expand All @@ -288,6 +297,20 @@ impl<D:serialize::Decoder<E>, E> def_id_decoder_helpers for D {
}
}

#[cfg(not(stage0))]
impl<D:serialize::Decoder> def_id_decoder_helpers for D {
fn read_def_id(&mut self, dcx: &DecodeContext) -> ast::DefId {
let did: ast::DefId = Decodable::decode(self).ok().unwrap();
did.tr(dcx)
}

fn read_def_id_nodcx(&mut self,
cdata: &cstore::crate_metadata) -> ast::DefId {
let did: ast::DefId = Decodable::decode(self).ok().unwrap();
decoder::translate_def_id(cdata, did)
}
}

// ______________________________________________________________________
// Encoding and decoding the AST itself
//
Expand Down
128 changes: 47 additions & 81 deletions src/libserialize/collection_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ use std::collections::{DList, RingBuf, BTreeMap, BTreeSet, HashMap, HashSet, Vec
use collections::enum_set::{EnumSet, CLike};

impl<
E,
S: Encoder<E>,
T: Encodable<S, E>
> Encodable<S, E> for DList<T> {
fn encode(&self, s: &mut S) -> Result<(), E> {
T: Encodable
> Encodable for DList<T> {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
s.emit_seq(self.len(), |s| {
for (i, e) in self.iter().enumerate() {
try!(s.emit_seq_elt(i, |s| e.encode(s)));
Expand All @@ -33,8 +31,8 @@ impl<
}
}

impl<E, D:Decoder<E>,T:Decodable<D, E>> Decodable<D, E> for DList<T> {
fn decode(d: &mut D) -> Result<DList<T>, E> {
impl<T:Decodable> Decodable for DList<T> {
fn decode<D: Decoder>(d: &mut D) -> Result<DList<T>, D::Error> {
d.read_seq(|d, len| {
let mut list = DList::new();
for i in range(0u, len) {
Expand All @@ -45,12 +43,8 @@ impl<E, D:Decoder<E>,T:Decodable<D, E>> Decodable<D, E> for DList<T> {
}
}

impl<
E,
S: Encoder<E>,
T: Encodable<S, E>
> Encodable<S, E> for RingBuf<T> {
fn encode(&self, s: &mut S) -> Result<(), E> {
impl<T: Encodable> Encodable for RingBuf<T> {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
s.emit_seq(self.len(), |s| {
for (i, e) in self.iter().enumerate() {
try!(s.emit_seq_elt(i, |s| e.encode(s)));
Expand All @@ -60,8 +54,8 @@ impl<
}
}

impl<E, D:Decoder<E>,T:Decodable<D, E>> Decodable<D, E> for RingBuf<T> {
fn decode(d: &mut D) -> Result<RingBuf<T>, E> {
impl<T:Decodable> Decodable for RingBuf<T> {
fn decode<D: Decoder>(d: &mut D) -> Result<RingBuf<T>, D::Error> {
d.read_seq(|d, len| {
let mut deque: RingBuf<T> = RingBuf::new();
for i in range(0u, len) {
Expand All @@ -73,12 +67,10 @@ impl<E, D:Decoder<E>,T:Decodable<D, E>> Decodable<D, E> for RingBuf<T> {
}

impl<
E,
S: Encoder<E>,
K: Encodable<S, E> + PartialEq + Ord,
V: Encodable<S, E> + PartialEq
> Encodable<S, E> for BTreeMap<K, V> {
fn encode(&self, e: &mut S) -> Result<(), E> {
K: Encodable + PartialEq + Ord,
V: Encodable + PartialEq
> Encodable for BTreeMap<K, V> {
fn encode<S: Encoder>(&self, e: &mut S) -> Result<(), S::Error> {
e.emit_map(self.len(), |e| {
let mut i = 0;
for (key, val) in self.iter() {
Expand All @@ -92,12 +84,10 @@ impl<
}

impl<
E,
D: Decoder<E>,
K: Decodable<D, E> + PartialEq + Ord,
V: Decodable<D, E> + PartialEq
> Decodable<D, E> for BTreeMap<K, V> {
fn decode(d: &mut D) -> Result<BTreeMap<K, V>, E> {
K: Decodable + PartialEq + Ord,
V: Decodable + PartialEq
> Decodable for BTreeMap<K, V> {
fn decode<D: Decoder>(d: &mut D) -> Result<BTreeMap<K, V>, D::Error> {
d.read_map(|d, len| {
let mut map = BTreeMap::new();
for i in range(0u, len) {
Expand All @@ -111,11 +101,9 @@ impl<
}

impl<
E,
S: Encoder<E>,
T: Encodable<S, E> + PartialEq + Ord
> Encodable<S, E> for BTreeSet<T> {
fn encode(&self, s: &mut S) -> Result<(), E> {
T: Encodable + PartialEq + Ord
> Encodable for BTreeSet<T> {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
s.emit_seq(self.len(), |s| {
let mut i = 0;
for e in self.iter() {
Expand All @@ -128,11 +116,9 @@ impl<
}

impl<
E,
D: Decoder<E>,
T: Decodable<D, E> + PartialEq + Ord
> Decodable<D, E> for BTreeSet<T> {
fn decode(d: &mut D) -> Result<BTreeSet<T>, E> {
T: Decodable + PartialEq + Ord
> Decodable for BTreeSet<T> {
fn decode<D: Decoder>(d: &mut D) -> Result<BTreeSet<T>, D::Error> {
d.read_seq(|d, len| {
let mut set = BTreeSet::new();
for i in range(0u, len) {
Expand All @@ -144,11 +130,9 @@ impl<
}

impl<
E,
S: Encoder<E>,
T: Encodable<S, E> + CLike
> Encodable<S, E> for EnumSet<T> {
fn encode(&self, s: &mut S) -> Result<(), E> {
T: Encodable + CLike
> Encodable for EnumSet<T> {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
let mut bits = 0;
for item in self.iter() {
bits |= item.to_uint();
Expand All @@ -158,11 +142,9 @@ impl<
}

impl<
E,
D: Decoder<E>,
T: Decodable<D, E> + CLike
> Decodable<D, E> for EnumSet<T> {
fn decode(d: &mut D) -> Result<EnumSet<T>, E> {
T: Decodable + CLike
> Decodable for EnumSet<T> {
fn decode<D: Decoder>(d: &mut D) -> Result<EnumSet<T>, D::Error> {
let bits = try!(d.read_uint());
let mut set = EnumSet::new();
for bit in range(0, uint::BITS) {
Expand All @@ -175,14 +157,12 @@ impl<
}

impl<
E,
S: Encoder<E>,
K: Encodable<S, E> + Hash<X> + Eq,
V: Encodable<S, E>,
K: Encodable + Hash<X> + Eq,
V: Encodable,
X,
H: Hasher<X>
> Encodable<S, E> for HashMap<K, V, H> {
fn encode(&self, e: &mut S) -> Result<(), E> {
> Encodable for HashMap<K, V, H> {
fn encode<S: Encoder>(&self, e: &mut S) -> Result<(), S::Error> {
e.emit_map(self.len(), |e| {
let mut i = 0;
for (key, val) in self.iter() {
Expand All @@ -196,14 +176,12 @@ impl<
}

impl<
E,
D: Decoder<E>,
K: Decodable<D, E> + Hash<S> + Eq,
V: Decodable<D, E>,
K: Decodable + Hash<S> + Eq,
V: Decodable,
S,
H: Hasher<S> + Default
> Decodable<D, E> for HashMap<K, V, H> {
fn decode(d: &mut D) -> Result<HashMap<K, V, H>, E> {
> Decodable for HashMap<K, V, H> {
fn decode<D: Decoder>(d: &mut D) -> Result<HashMap<K, V, H>, D::Error> {
d.read_map(|d, len| {
let hasher = Default::default();
let mut map = HashMap::with_capacity_and_hasher(len, hasher);
Expand All @@ -218,13 +196,11 @@ impl<
}

impl<
E,
S: Encoder<E>,
T: Encodable<S, E> + Hash<X> + Eq,
T: Encodable + Hash<X> + Eq,
X,
H: Hasher<X>
> Encodable<S, E> for HashSet<T, H> {
fn encode(&self, s: &mut S) -> Result<(), E> {
> Encodable for HashSet<T, H> {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
s.emit_seq(self.len(), |s| {
let mut i = 0;
for e in self.iter() {
Expand All @@ -237,13 +213,11 @@ impl<
}

impl<
E,
D: Decoder<E>,
T: Decodable<D, E> + Hash<S> + Eq,
T: Decodable + Hash<S> + Eq,
S,
H: Hasher<S> + Default
> Decodable<D, E> for HashSet<T, H> {
fn decode(d: &mut D) -> Result<HashSet<T, H>, E> {
> Decodable for HashSet<T, H> {
fn decode<D: Decoder>(d: &mut D) -> Result<HashSet<T, H>, D::Error> {
d.read_seq(|d, len| {
let mut set = HashSet::with_capacity_and_hasher(len, Default::default());
for i in range(0u, len) {
Expand All @@ -254,12 +228,8 @@ impl<
}
}

impl<
E,
S: Encoder<E>,
V: Encodable<S, E>
> Encodable<S, E> for VecMap<V> {
fn encode(&self, e: &mut S) -> Result<(), E> {
impl<V: Encodable> Encodable for VecMap<V> {
fn encode<S: Encoder>(&self, e: &mut S) -> Result<(), S::Error> {
e.emit_map(self.len(), |e| {
for (i, (key, val)) in self.iter().enumerate() {
try!(e.emit_map_elt_key(i, |e| key.encode(e)));
Expand All @@ -270,12 +240,8 @@ impl<
}
}

impl<
E,
D: Decoder<E>,
V: Decodable<D, E>
> Decodable<D, E> for VecMap<V> {
fn decode(d: &mut D) -> Result<VecMap<V>, E> {
impl<V: Decodable> Decodable for VecMap<V> {
fn decode<D: Decoder>(d: &mut D) -> Result<VecMap<V>, D::Error> {
d.read_map(|d, len| {
let mut map = VecMap::new();
for i in range(0u, len) {
Expand Down
Loading