Skip to content

Commit 6dd20c8

Browse files
committed
auto merge of #5630 : erickt/rust/serial, r=erickt
@nikomatsakis and I were talking about how the serializers were a bit too complicated. None of the users of With the `emit_option` and `read_option` functions, the serializers are now moving more high level. This patch series continues that trend. I've removed support for emitting specific string and vec types, and added support for emitting mapping types.
2 parents fbd8eae + 810c4d8 commit 6dd20c8

File tree

14 files changed

+837
-759
lines changed

14 files changed

+837
-759
lines changed

src/libcore/hashmap.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -393,10 +393,16 @@ pub mod linear {
393393
}
394394
}
395395
396-
pub impl<K:Hash + IterBytes + Eq,V> LinearMap<K, V> {
396+
pub impl<K: Hash + IterBytes + Eq, V> LinearMap<K, V> {
397397
/// Create an empty LinearMap
398398
fn new() -> LinearMap<K, V> {
399-
linear_map_with_capacity(INITIAL_CAPACITY)
399+
LinearMap::with_capacity(INITIAL_CAPACITY)
400+
}
401+
402+
/// Create an empty LinearMap with space for at least `n` elements in
403+
/// the hash table.
404+
fn with_capacity(capacity: uint) -> LinearMap<K, V> {
405+
linear_map_with_capacity(capacity)
400406
}
401407
402408
/// Reserve space for at least `n` elements in the hash table.
@@ -652,7 +658,15 @@ pub mod linear {
652658

653659
pub impl <T:Hash + IterBytes + Eq> LinearSet<T> {
654660
/// Create an empty LinearSet
655-
fn new() -> LinearSet<T> { LinearSet{map: LinearMap::new()} }
661+
fn new() -> LinearSet<T> {
662+
LinearSet::with_capacity(INITIAL_CAPACITY)
663+
}
664+
665+
/// Create an empty LinearSet with space for at least `n` elements in
666+
/// the hash table.
667+
fn with_capacity(capacity: uint) -> LinearSet<T> {
668+
LinearSet { map: LinearMap::with_capacity(capacity) }
669+
}
656670

657671
/// Reserve space for at least `n` elements in the hash table.
658672
fn reserve_at_least(&mut self, n: uint) {

src/libcore/vec.rs

+28
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,28 @@ pub fn consume<T>(mut v: ~[T], f: &fn(uint, v: T)) {
560560
}
561561
}
562562
563+
pub fn consume_reverse<T>(mut v: ~[T], f: &fn(uint, v: T)) {
564+
unsafe {
565+
do as_mut_buf(v) |p, ln| {
566+
let mut i = ln;
567+
while i > 0 {
568+
i -= 1;
569+
570+
// NB: This unsafe operation counts on init writing 0s to the
571+
// holes we create in the vector. That ensures that, if the
572+
// iterator fails then we won't try to clean up the consumed
573+
// elements during unwinding
574+
let mut x = intrinsics::init();
575+
let p = ptr::mut_offset(p, i);
576+
x <-> *p;
577+
f(i, x);
578+
}
579+
}
580+
581+
raw::set_len(&mut v, 0);
582+
}
583+
}
584+
563585
/// Remove the last element from a vector and return it
564586
pub fn pop<T>(v: &mut ~[T]) -> T {
565587
let ln = v.len();
@@ -1985,6 +2007,7 @@ pub trait OwnedVector<T> {
19852007
fn truncate(&mut self, newlen: uint);
19862008
fn retain(&mut self, f: &fn(t: &T) -> bool);
19872009
fn consume(self, f: &fn(uint, v: T));
2010+
fn consume_reverse(self, f: &fn(uint, v: T));
19882011
fn filter(self, f: &fn(t: &T) -> bool) -> ~[T];
19892012
fn partition(self, f: &fn(&T) -> bool) -> (~[T], ~[T]);
19902013
fn grow_fn(&mut self, n: uint, op: iter::InitOp<T>);
@@ -2046,6 +2069,11 @@ impl<T> OwnedVector<T> for ~[T] {
20462069
consume(self, f)
20472070
}
20482071

2072+
#[inline]
2073+
fn consume_reverse(self, f: &fn(uint, v: T)) {
2074+
consume_reverse(self, f)
2075+
}
2076+
20492077
#[inline]
20502078
fn filter(self, f: &fn(&T) -> bool) -> ~[T] {
20512079
filter(self, f)

src/librustc/middle/astencode.rs

+5-16
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ trait read_method_map_entry_helper {
558558
fn encode_method_map_entry(ecx: @e::EncodeContext,
559559
ebml_w: writer::Encoder,
560560
mme: method_map_entry) {
561-
do ebml_w.emit_rec {
561+
do ebml_w.emit_struct("method_map_entry", 3) {
562562
do ebml_w.emit_field(~"self_arg", 0u) {
563563
ebml_w.emit_arg(ecx, mme.self_arg);
564564
}
@@ -574,7 +574,7 @@ fn encode_method_map_entry(ecx: @e::EncodeContext,
574574
impl read_method_map_entry_helper for reader::Decoder {
575575
fn read_method_map_entry(&self, xcx: @ExtendedDecodeContext)
576576
-> method_map_entry {
577-
do self.read_rec {
577+
do self.read_struct("method_map_entry", 3) {
578578
method_map_entry {
579579
self_arg: self.read_field(~"self_arg", 0u, || {
580580
self.read_arg(xcx)
@@ -778,7 +778,7 @@ impl ebml_writer_helpers for writer::Encoder {
778778
779779
fn emit_tpbt(&self, ecx: @e::EncodeContext,
780780
tpbt: ty::ty_param_bounds_and_ty) {
781-
do self.emit_rec {
781+
do self.emit_struct("ty_param_bounds_and_ty", 3) {
782782
do self.emit_field(~"bounds", 0) {
783783
do self.emit_from_vec(*tpbt.bounds) |bs| {
784784
self.emit_bounds(ecx, *bs);
@@ -1045,7 +1045,7 @@ impl ebml_decoder_decoder_helpers for reader::Decoder {
10451045
fn read_ty_param_bounds_and_ty(&self, xcx: @ExtendedDecodeContext)
10461046
-> ty::ty_param_bounds_and_ty
10471047
{
1048-
do self.read_rec {
1048+
do self.read_struct("ty_param_bounds_and_ty", 3) {
10491049
ty::ty_param_bounds_and_ty {
10501050
bounds: self.read_field(~"bounds", 0u, || {
10511051
@self.read_to_vec(|| self.read_bounds(xcx) )
@@ -1212,7 +1212,6 @@ fn mk_ctxt() -> @fake_ext_ctxt {
12121212
#[cfg(test)]
12131213
fn roundtrip(in_item: Option<@ast::item>) {
12141214
use core::io;
1215-
use std::prettyprint;
12161215

12171216
let in_item = in_item.get();
12181217
let bytes = do io::with_bytes_writer |wr| {
@@ -1222,17 +1221,7 @@ fn roundtrip(in_item: Option<@ast::item>) {
12221221
let ebml_doc = reader::Doc(@bytes);
12231222
let out_item = decode_item_ast(ebml_doc);
12241223

1225-
let exp_str = do io::with_str_writer |w| {
1226-
in_item.encode(&prettyprint::Serializer(w))
1227-
};
1228-
let out_str = do io::with_str_writer |w| {
1229-
out_item.encode(&prettyprint::Serializer(w))
1230-
};
1231-
1232-
debug!("expected string: %s", exp_str);
1233-
debug!("actual string : %s", out_str);
1234-
1235-
assert!(exp_str == out_str);
1224+
assert_eq!(in_item, out_item);
12361225
}
12371226

12381227
#[test]

src/libstd/deque.rs

+32
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ pub impl<T> Deque<T> {
6666
get(self.elts, idx)
6767
}
6868

69+
/// Iterate over the elements in the deque
70+
fn each(&self, f: &fn(&T) -> bool) {
71+
self.eachi(|_i, e| f(e))
72+
}
73+
74+
/// Iterate over the elements in the deque by index
75+
fn eachi(&self, f: &fn(uint, &T) -> bool) {
76+
for uint::range(0, self.nelts) |i| {
77+
if !f(i, self.get(i as int)) { return; }
78+
}
79+
}
80+
6981
/// Remove and return the first element in the deque
7082
///
7183
/// Fails if the deque is empty
@@ -223,6 +235,7 @@ mod tests {
223235
assert!(*deq.get(3) == d);
224236
}
225237

238+
#[test]
226239
fn test_parameterized<T:Copy + Eq + Durable>(a: T, b: T, c: T, d: T) {
227240
let mut deq = Deque::new();
228241
assert!(deq.len() == 0);
@@ -300,4 +313,23 @@ mod tests {
300313
let reccy4 = RecCy { x: 19, y: 252, t: Two(17, 42) };
301314
test_parameterized::<RecCy>(reccy1, reccy2, reccy3, reccy4);
302315
}
316+
317+
#[test]
318+
fn test_eachi() {
319+
let mut deq = Deque::new();
320+
deq.add_back(1);
321+
deq.add_back(2);
322+
deq.add_back(3);
323+
324+
for deq.eachi |i, e| {
325+
assert_eq!(*e, i + 1);
326+
}
327+
328+
deq.pop_front();
329+
330+
for deq.eachi |i, e| {
331+
assert_eq!(*e, i + 2);
332+
}
333+
334+
}
303335
}

src/libstd/ebml.rs

+35-69
Original file line numberDiff line numberDiff line change
@@ -311,23 +311,10 @@ pub mod reader {
311311
fn read_f64(&self) -> f64 { fail!(~"read_f64()"); }
312312
fn read_f32(&self) -> f32 { fail!(~"read_f32()"); }
313313
fn read_float(&self) -> float { fail!(~"read_float()"); }
314-
315314
fn read_char(&self) -> char { fail!(~"read_char()"); }
316-
317-
fn read_owned_str(&self) -> ~str { doc_as_str(self.next_doc(EsStr)) }
318-
fn read_managed_str(&self) -> @str { fail!(~"read_managed_str()"); }
315+
fn read_str(&self) -> ~str { doc_as_str(self.next_doc(EsStr)) }
319316
320317
// Compound types:
321-
fn read_owned<T>(&self, f: &fn() -> T) -> T {
322-
debug!("read_owned()");
323-
f()
324-
}
325-
326-
fn read_managed<T>(&self, f: &fn() -> T) -> T {
327-
debug!("read_managed()");
328-
f()
329-
}
330-
331318
fn read_enum<T>(&self, name: &str, f: &fn() -> T) -> T {
332319
debug!("read_enum(%s)", name);
333320
self._check_label(name);
@@ -348,34 +335,20 @@ pub mod reader {
348335
f()
349336
}
350337
351-
fn read_owned_vec<T>(&self, f: &fn(uint) -> T) -> T {
352-
debug!("read_owned_vec()");
338+
fn read_seq<T>(&self, f: &fn(uint) -> T) -> T {
339+
debug!("read_seq()");
353340
do self.push_doc(self.next_doc(EsVec)) {
354341
let len = self._next_uint(EsVecLen);
355342
debug!(" len=%u", len);
356343
f(len)
357344
}
358345
}
359346
360-
fn read_managed_vec<T>(&self, f: &fn(uint) -> T) -> T {
361-
debug!("read_managed_vec()");
362-
do self.push_doc(self.next_doc(EsVec)) {
363-
let len = self._next_uint(EsVecLen);
364-
debug!(" len=%u", len);
365-
f(len)
366-
}
367-
}
368-
369-
fn read_vec_elt<T>(&self, idx: uint, f: &fn() -> T) -> T {
370-
debug!("read_vec_elt(idx=%u)", idx);
347+
fn read_seq_elt<T>(&self, idx: uint, f: &fn() -> T) -> T {
348+
debug!("read_seq_elt(idx=%u)", idx);
371349
self.push_doc(self.next_doc(EsVecElt), f)
372350
}
373351
374-
fn read_rec<T>(&self, f: &fn() -> T) -> T {
375-
debug!("read_rec()");
376-
f()
377-
}
378-
379352
fn read_struct<T>(&self, name: &str, _len: uint, f: &fn() -> T) -> T {
380353
debug!("read_struct(name=%s)", name);
381354
f()
@@ -387,16 +360,6 @@ pub mod reader {
387360
f()
388361
}
389362
390-
fn read_tup<T>(&self, len: uint, f: &fn() -> T) -> T {
391-
debug!("read_tup(len=%u)", len);
392-
f()
393-
}
394-
395-
fn read_tup_elt<T>(&self, idx: uint, f: &fn() -> T) -> T {
396-
debug!("read_tup_elt(idx=%u)", idx);
397-
f()
398-
}
399-
400363
fn read_option<T>(&self, f: &fn(bool) -> T) -> T {
401364
debug!("read_option()");
402365
do self.read_enum("Option") || {
@@ -409,6 +372,21 @@ pub mod reader {
409372
}
410373
}
411374
}
375+
376+
fn read_map<T>(&self, _f: &fn(uint) -> T) -> T {
377+
debug!("read_map()");
378+
fail!(~"read_map is unimplemented");
379+
}
380+
381+
fn read_map_elt_key<T>(&self, idx: uint, _f: &fn() -> T) -> T {
382+
debug!("read_map_elt_key(idx=%u)", idx);
383+
fail!(~"read_map_elt_val is unimplemented");
384+
}
385+
386+
fn read_map_elt_val<T>(&self, idx: uint, _f: &fn() -> T) -> T {
387+
debug!("read_map_elt_val(idx=%u)", idx);
388+
fail!(~"read_map_elt_val is unimplemented");
389+
}
412390
}
413391
}
414392
@@ -620,22 +598,10 @@ pub mod writer {
620598
fail!(~"Unimplemented: serializing a char");
621599
}
622600
623-
fn emit_borrowed_str(&self, v: &str) {
601+
fn emit_str(&self, v: &str) {
624602
self.wr_tagged_str(EsStr as uint, v)
625603
}
626604
627-
fn emit_owned_str(&self, v: &str) {
628-
self.emit_borrowed_str(v)
629-
}
630-
631-
fn emit_managed_str(&self, v: &str) {
632-
self.emit_borrowed_str(v)
633-
}
634-
635-
fn emit_borrowed(&self, f: &fn()) { f() }
636-
fn emit_owned(&self, f: &fn()) { f() }
637-
fn emit_managed(&self, f: &fn()) { f() }
638-
639605
fn emit_enum(&self, name: &str, f: &fn()) {
640606
self._emit_label(name);
641607
self.wr_tag(EsEnum as uint, f)
@@ -647,35 +613,23 @@ pub mod writer {
647613
}
648614
fn emit_enum_variant_arg(&self, _idx: uint, f: &fn()) { f() }
649615
650-
fn emit_borrowed_vec(&self, len: uint, f: &fn()) {
616+
fn emit_seq(&self, len: uint, f: &fn()) {
651617
do self.wr_tag(EsVec as uint) {
652618
self._emit_tagged_uint(EsVecLen, len);
653619
f()
654620
}
655621
}
656622
657-
fn emit_owned_vec(&self, len: uint, f: &fn()) {
658-
self.emit_borrowed_vec(len, f)
659-
}
660-
661-
fn emit_managed_vec(&self, len: uint, f: &fn()) {
662-
self.emit_borrowed_vec(len, f)
663-
}
664-
665-
fn emit_vec_elt(&self, _idx: uint, f: &fn()) {
623+
fn emit_seq_elt(&self, _idx: uint, f: &fn()) {
666624
self.wr_tag(EsVecElt as uint, f)
667625
}
668626
669-
fn emit_rec(&self, f: &fn()) { f() }
670627
fn emit_struct(&self, _name: &str, _len: uint, f: &fn()) { f() }
671628
fn emit_field(&self, name: &str, _idx: uint, f: &fn()) {
672629
self._emit_label(name);
673630
f()
674631
}
675632
676-
fn emit_tup(&self, _len: uint, f: &fn()) { f() }
677-
fn emit_tup_elt(&self, _idx: uint, f: &fn()) { f() }
678-
679633
fn emit_option(&self, f: &fn()) {
680634
self.emit_enum("Option", f);
681635
}
@@ -685,6 +639,18 @@ pub mod writer {
685639
fn emit_option_some(&self, f: &fn()) {
686640
self.emit_enum_variant("Some", 1, 1, f)
687641
}
642+
643+
fn emit_map(&self, _len: uint, _f: &fn()) {
644+
fail!(~"emit_map is unimplemented");
645+
}
646+
647+
fn emit_map_elt_key(&self, _idx: uint, _f: &fn()) {
648+
fail!(~"emit_map_elt_key is unimplemented");
649+
}
650+
651+
fn emit_map_elt_val(&self, _idx: uint, _f: &fn()) {
652+
fail!(~"emit_map_elt_val is unimplemented");
653+
}
688654
}
689655
}
690656

src/libstd/flatpipes.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -466,8 +466,8 @@ pub mod flatteners {
466466
fn from_writer(w: @Writer) -> Self;
467467
}
468468
469-
impl<'self> FromReader for json::Decoder<'self> {
470-
fn from_reader(r: @Reader) -> json::Decoder<'self> {
469+
impl FromReader for json::Decoder {
470+
fn from_reader(r: @Reader) -> json::Decoder {
471471
match json::from_reader(r) {
472472
Ok(json) => {
473473
json::Decoder(json)

0 commit comments

Comments
 (0)