Skip to content

Commit 1dc7eb8

Browse files
committed
syntax: Fix encoding and decoding spans
The protocol for `serialize::{En,De}code` doesn't allow for two integers to be serialized next to each other. This switches the protocol to serializing `Span`s as a struct. rbml structs don't have any overhead, so the metadata shouldn't increase in size, but it allows the json format to be properly generated, albeit slightly more heavy than when it was just serializing a span as a u64. Closes #31025. s
1 parent c4c9628 commit 1dc7eb8

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/libsyntax/codemap.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,16 +164,31 @@ impl Eq for Span {}
164164

165165
impl Encodable for Span {
166166
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
167-
try!(s.emit_u32(self.lo.0));
168-
s.emit_u32(self.hi.0)
167+
s.emit_struct("Span", 2, |s| {
168+
try!(s.emit_struct_field("lo", 0, |s| {
169+
self.lo.encode(s)
170+
}));
171+
172+
s.emit_struct_field("hi", 1, |s| {
173+
self.hi.encode(s)
174+
})
175+
})
169176
}
170177
}
171178

172179
impl Decodable for Span {
173180
fn decode<D: Decoder>(d: &mut D) -> Result<Span, D::Error> {
174-
let lo = BytePos(try! { d.read_u32() });
175-
let hi = BytePos(try! { d.read_u32() });
176-
Ok(mk_sp(lo, hi))
181+
d.read_struct("Span", 2, |d| {
182+
let lo = try!(d.read_struct_field("lo", 0, |d| {
183+
BytePos::decode(d)
184+
}));
185+
186+
let hi = try!(d.read_struct_field("hi", 1, |d| {
187+
BytePos::decode(d)
188+
}));
189+
190+
Ok(mk_sp(lo, hi))
191+
})
177192
}
178193
}
179194

0 commit comments

Comments
 (0)