Skip to content

Commit 03268bb

Browse files
committed
auto merge of #20514 : alexcrichton/rust/serialize-associated-type, r=aturon
This commit moves the libserialize crate (and will force the hand of the rustc-serialize crate) to not require the `old_orphan_check` feature gate as well as using associated types wherever possible. Concretely, the following changes were made: * The error type of `Encoder` and `Decoder` is now an associated type, meaning that these traits have no type parameters. * The `Encoder` and `Decoder` type parameters on the `Encodable` and `Decodable` traits have moved to the corresponding method of the trait. This movement alleviates the dependency on `old_orphan_check` but implies that implementations can no longer be specialized for the type of encoder/decoder being implemented. Due to the trait definitions changing, this is a: [breaking-change]
2 parents 8e83af6 + 0cb7a40 commit 03268bb

20 files changed

+5755
-437
lines changed

src/librbml/lib.rs

+464-1
Large diffs are not rendered by default.

src/librustc/middle/astencode.rs

+23
Original file line numberDiff line numberDiff line change
@@ -263,18 +263,27 @@ trait def_id_encoder_helpers {
263263
fn emit_def_id(&mut self, did: ast::DefId);
264264
}
265265

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

273+
#[cfg(not(stage0))]
274+
impl<S:serialize::Encoder> def_id_encoder_helpers for S {
275+
fn emit_def_id(&mut self, did: ast::DefId) {
276+
did.encode(self).ok().unwrap()
277+
}
278+
}
279+
272280
trait def_id_decoder_helpers {
273281
fn read_def_id(&mut self, dcx: &DecodeContext) -> ast::DefId;
274282
fn read_def_id_nodcx(&mut self,
275283
cdata: &cstore::crate_metadata) -> ast::DefId;
276284
}
277285

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

300+
#[cfg(not(stage0))]
301+
impl<D:serialize::Decoder> def_id_decoder_helpers for D {
302+
fn read_def_id(&mut self, dcx: &DecodeContext) -> ast::DefId {
303+
let did: ast::DefId = Decodable::decode(self).ok().unwrap();
304+
did.tr(dcx)
305+
}
306+
307+
fn read_def_id_nodcx(&mut self,
308+
cdata: &cstore::crate_metadata) -> ast::DefId {
309+
let did: ast::DefId = Decodable::decode(self).ok().unwrap();
310+
decoder::translate_def_id(cdata, did)
311+
}
312+
}
313+
291314
// ______________________________________________________________________
292315
// Encoding and decoding the AST itself
293316
//

src/libserialize/collection_impls.rs

+47-81
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@ use std::collections::{DList, RingBuf, BTreeMap, BTreeSet, HashMap, HashSet, Vec
1919
use collections::enum_set::{EnumSet, CLike};
2020

2121
impl<
22-
E,
23-
S: Encoder<E>,
24-
T: Encodable<S, E>
25-
> Encodable<S, E> for DList<T> {
26-
fn encode(&self, s: &mut S) -> Result<(), E> {
22+
T: Encodable
23+
> Encodable for DList<T> {
24+
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
2725
s.emit_seq(self.len(), |s| {
2826
for (i, e) in self.iter().enumerate() {
2927
try!(s.emit_seq_elt(i, |s| e.encode(s)));
@@ -33,8 +31,8 @@ impl<
3331
}
3432
}
3533

36-
impl<E, D:Decoder<E>,T:Decodable<D, E>> Decodable<D, E> for DList<T> {
37-
fn decode(d: &mut D) -> Result<DList<T>, E> {
34+
impl<T:Decodable> Decodable for DList<T> {
35+
fn decode<D: Decoder>(d: &mut D) -> Result<DList<T>, D::Error> {
3836
d.read_seq(|d, len| {
3937
let mut list = DList::new();
4038
for i in range(0u, len) {
@@ -45,12 +43,8 @@ impl<E, D:Decoder<E>,T:Decodable<D, E>> Decodable<D, E> for DList<T> {
4543
}
4644
}
4745

48-
impl<
49-
E,
50-
S: Encoder<E>,
51-
T: Encodable<S, E>
52-
> Encodable<S, E> for RingBuf<T> {
53-
fn encode(&self, s: &mut S) -> Result<(), E> {
46+
impl<T: Encodable> Encodable for RingBuf<T> {
47+
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
5448
s.emit_seq(self.len(), |s| {
5549
for (i, e) in self.iter().enumerate() {
5650
try!(s.emit_seq_elt(i, |s| e.encode(s)));
@@ -60,8 +54,8 @@ impl<
6054
}
6155
}
6256

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

7569
impl<
76-
E,
77-
S: Encoder<E>,
78-
K: Encodable<S, E> + PartialEq + Ord,
79-
V: Encodable<S, E> + PartialEq
80-
> Encodable<S, E> for BTreeMap<K, V> {
81-
fn encode(&self, e: &mut S) -> Result<(), E> {
70+
K: Encodable + PartialEq + Ord,
71+
V: Encodable + PartialEq
72+
> Encodable for BTreeMap<K, V> {
73+
fn encode<S: Encoder>(&self, e: &mut S) -> Result<(), S::Error> {
8274
e.emit_map(self.len(), |e| {
8375
let mut i = 0;
8476
for (key, val) in self.iter() {
@@ -92,12 +84,10 @@ impl<
9284
}
9385

9486
impl<
95-
E,
96-
D: Decoder<E>,
97-
K: Decodable<D, E> + PartialEq + Ord,
98-
V: Decodable<D, E> + PartialEq
99-
> Decodable<D, E> for BTreeMap<K, V> {
100-
fn decode(d: &mut D) -> Result<BTreeMap<K, V>, E> {
87+
K: Decodable + PartialEq + Ord,
88+
V: Decodable + PartialEq
89+
> Decodable for BTreeMap<K, V> {
90+
fn decode<D: Decoder>(d: &mut D) -> Result<BTreeMap<K, V>, D::Error> {
10191
d.read_map(|d, len| {
10292
let mut map = BTreeMap::new();
10393
for i in range(0u, len) {
@@ -111,11 +101,9 @@ impl<
111101
}
112102

113103
impl<
114-
E,
115-
S: Encoder<E>,
116-
T: Encodable<S, E> + PartialEq + Ord
117-
> Encodable<S, E> for BTreeSet<T> {
118-
fn encode(&self, s: &mut S) -> Result<(), E> {
104+
T: Encodable + PartialEq + Ord
105+
> Encodable for BTreeSet<T> {
106+
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
119107
s.emit_seq(self.len(), |s| {
120108
let mut i = 0;
121109
for e in self.iter() {
@@ -128,11 +116,9 @@ impl<
128116
}
129117

130118
impl<
131-
E,
132-
D: Decoder<E>,
133-
T: Decodable<D, E> + PartialEq + Ord
134-
> Decodable<D, E> for BTreeSet<T> {
135-
fn decode(d: &mut D) -> Result<BTreeSet<T>, E> {
119+
T: Decodable + PartialEq + Ord
120+
> Decodable for BTreeSet<T> {
121+
fn decode<D: Decoder>(d: &mut D) -> Result<BTreeSet<T>, D::Error> {
136122
d.read_seq(|d, len| {
137123
let mut set = BTreeSet::new();
138124
for i in range(0u, len) {
@@ -144,11 +130,9 @@ impl<
144130
}
145131

146132
impl<
147-
E,
148-
S: Encoder<E>,
149-
T: Encodable<S, E> + CLike
150-
> Encodable<S, E> for EnumSet<T> {
151-
fn encode(&self, s: &mut S) -> Result<(), E> {
133+
T: Encodable + CLike
134+
> Encodable for EnumSet<T> {
135+
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
152136
let mut bits = 0;
153137
for item in self.iter() {
154138
bits |= item.to_uint();
@@ -158,11 +142,9 @@ impl<
158142
}
159143

160144
impl<
161-
E,
162-
D: Decoder<E>,
163-
T: Decodable<D, E> + CLike
164-
> Decodable<D, E> for EnumSet<T> {
165-
fn decode(d: &mut D) -> Result<EnumSet<T>, E> {
145+
T: Decodable + CLike
146+
> Decodable for EnumSet<T> {
147+
fn decode<D: Decoder>(d: &mut D) -> Result<EnumSet<T>, D::Error> {
166148
let bits = try!(d.read_uint());
167149
let mut set = EnumSet::new();
168150
for bit in range(0, uint::BITS) {
@@ -175,14 +157,12 @@ impl<
175157
}
176158

177159
impl<
178-
E,
179-
S: Encoder<E>,
180-
K: Encodable<S, E> + Hash<X> + Eq,
181-
V: Encodable<S, E>,
160+
K: Encodable + Hash<X> + Eq,
161+
V: Encodable,
182162
X,
183163
H: Hasher<X>
184-
> Encodable<S, E> for HashMap<K, V, H> {
185-
fn encode(&self, e: &mut S) -> Result<(), E> {
164+
> Encodable for HashMap<K, V, H> {
165+
fn encode<S: Encoder>(&self, e: &mut S) -> Result<(), S::Error> {
186166
e.emit_map(self.len(), |e| {
187167
let mut i = 0;
188168
for (key, val) in self.iter() {
@@ -196,14 +176,12 @@ impl<
196176
}
197177

198178
impl<
199-
E,
200-
D: Decoder<E>,
201-
K: Decodable<D, E> + Hash<S> + Eq,
202-
V: Decodable<D, E>,
179+
K: Decodable + Hash<S> + Eq,
180+
V: Decodable,
203181
S,
204182
H: Hasher<S> + Default
205-
> Decodable<D, E> for HashMap<K, V, H> {
206-
fn decode(d: &mut D) -> Result<HashMap<K, V, H>, E> {
183+
> Decodable for HashMap<K, V, H> {
184+
fn decode<D: Decoder>(d: &mut D) -> Result<HashMap<K, V, H>, D::Error> {
207185
d.read_map(|d, len| {
208186
let hasher = Default::default();
209187
let mut map = HashMap::with_capacity_and_hasher(len, hasher);
@@ -218,13 +196,11 @@ impl<
218196
}
219197

220198
impl<
221-
E,
222-
S: Encoder<E>,
223-
T: Encodable<S, E> + Hash<X> + Eq,
199+
T: Encodable + Hash<X> + Eq,
224200
X,
225201
H: Hasher<X>
226-
> Encodable<S, E> for HashSet<T, H> {
227-
fn encode(&self, s: &mut S) -> Result<(), E> {
202+
> Encodable for HashSet<T, H> {
203+
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
228204
s.emit_seq(self.len(), |s| {
229205
let mut i = 0;
230206
for e in self.iter() {
@@ -237,13 +213,11 @@ impl<
237213
}
238214

239215
impl<
240-
E,
241-
D: Decoder<E>,
242-
T: Decodable<D, E> + Hash<S> + Eq,
216+
T: Decodable + Hash<S> + Eq,
243217
S,
244218
H: Hasher<S> + Default
245-
> Decodable<D, E> for HashSet<T, H> {
246-
fn decode(d: &mut D) -> Result<HashSet<T, H>, E> {
219+
> Decodable for HashSet<T, H> {
220+
fn decode<D: Decoder>(d: &mut D) -> Result<HashSet<T, H>, D::Error> {
247221
d.read_seq(|d, len| {
248222
let mut set = HashSet::with_capacity_and_hasher(len, Default::default());
249223
for i in range(0u, len) {
@@ -254,12 +228,8 @@ impl<
254228
}
255229
}
256230

257-
impl<
258-
E,
259-
S: Encoder<E>,
260-
V: Encodable<S, E>
261-
> Encodable<S, E> for VecMap<V> {
262-
fn encode(&self, e: &mut S) -> Result<(), E> {
231+
impl<V: Encodable> Encodable for VecMap<V> {
232+
fn encode<S: Encoder>(&self, e: &mut S) -> Result<(), S::Error> {
263233
e.emit_map(self.len(), |e| {
264234
for (i, (key, val)) in self.iter().enumerate() {
265235
try!(e.emit_map_elt_key(i, |e| key.encode(e)));
@@ -270,12 +240,8 @@ impl<
270240
}
271241
}
272242

273-
impl<
274-
E,
275-
D: Decoder<E>,
276-
V: Decodable<D, E>
277-
> Decodable<D, E> for VecMap<V> {
278-
fn decode(d: &mut D) -> Result<VecMap<V>, E> {
243+
impl<V: Decodable> Decodable for VecMap<V> {
244+
fn decode<D: Decoder>(d: &mut D) -> Result<VecMap<V>, D::Error> {
279245
d.read_map(|d, len| {
280246
let mut map = VecMap::new();
281247
for i in range(0u, len) {

0 commit comments

Comments
 (0)