Skip to content

Simplify serialize::{En,De}coder, add support for serializing maps, and other misc fns #5630

New issue

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

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

Already on GitHub? # to your account

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
bdf81e1
core: Inline mallocing wrapper functions
erickt Mar 28, 2013
b05e148
std: change Decoder::read_option to return a generic type
erickt Mar 28, 2013
2c658fa
std: remove prettyprint
erickt Mar 29, 2013
1dd11c7
core: add LinearMap::with_capacity
erickt Mar 29, 2013
8b43c62
std: remove Encoder::emit_{owned,managed}_str and Decoder::read_{owne…
erickt Mar 29, 2013
63fc887
std: remove Encoder::emit_{owned,managed} and Decoder::read_{owned,ma…
erickt Mar 29, 2013
590bbce
std: remove Encoder::emit_{owned,managed}_vec and Decoder::read_{owne…
erickt Mar 29, 2013
e99cdcf
std: remove Encoder::emit_tup{,_elt} and Decoder::read_tup{,_elt}
erickt Mar 29, 2013
90b3658
std: remove Encoder::read_rec and Decoder::emit_rec
erickt Mar 29, 2013
ed62f6d
core: add consume_reverse
erickt Mar 29, 2013
d1a83e6
std: add Encoder::emit_map and Decoder::read_map
erickt Mar 29, 2013
31563f5
Merge remote-tracking branch 'remotes/origin/incoming' into serial
erickt Mar 29, 2013
bdef3f1
std: fix json deserializing vectors and a test
erickt Mar 29, 2013
3564389
Merge remote-tracking branch 'remotes/origin/incoming' into serial
erickt Mar 29, 2013
529ae38
Merge remote-tracking branch 'remotes/origin/incoming' into serial
erickt Mar 30, 2013
e5c7a9e
std: add serialization support for dlist, linearset, triemap, trieset…
erickt Mar 30, 2013
0de7635
Merge remote-tracking branch 'remotes/origin/incoming' into serial
erickt Mar 30, 2013
909d8f0
std: Add Deque::eachi and a Deque serializer support
erickt Mar 30, 2013
9bbf384
std: clean up the json pretty printer tests
erickt Mar 30, 2013
f364cf5
syntax: fix auto_encode test.
erickt Mar 30, 2013
5b7d608
std: add more json decoder tests.
erickt Mar 30, 2013
810c4d8
rustc: fix astencode test
erickt Mar 30, 2013
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
20 changes: 17 additions & 3 deletions src/libcore/hashmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,10 +393,16 @@ pub mod linear {
}
}

pub impl<K:Hash + IterBytes + Eq,V> LinearMap<K, V> {
pub impl<K: Hash + IterBytes + Eq, V> LinearMap<K, V> {
/// Create an empty LinearMap
fn new() -> LinearMap<K, V> {
linear_map_with_capacity(INITIAL_CAPACITY)
LinearMap::with_capacity(INITIAL_CAPACITY)
}

/// Create an empty LinearMap with space for at least `n` elements in
/// the hash table.
fn with_capacity(capacity: uint) -> LinearMap<K, V> {
linear_map_with_capacity(capacity)
}

/// Reserve space for at least `n` elements in the hash table.
Expand Down Expand Up @@ -652,7 +658,15 @@ pub mod linear {

pub impl <T:Hash + IterBytes + Eq> LinearSet<T> {
/// Create an empty LinearSet
fn new() -> LinearSet<T> { LinearSet{map: LinearMap::new()} }
fn new() -> LinearSet<T> {
LinearSet::with_capacity(INITIAL_CAPACITY)
}

/// Create an empty LinearSet with space for at least `n` elements in
/// the hash table.
fn with_capacity(capacity: uint) -> LinearSet<T> {
LinearSet { map: LinearMap::with_capacity(capacity) }
}

/// Reserve space for at least `n` elements in the hash table.
fn reserve_at_least(&mut self, n: uint) {
Expand Down
28 changes: 28 additions & 0 deletions src/libcore/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,28 @@ pub fn consume<T>(mut v: ~[T], f: &fn(uint, v: T)) {
}
}

pub fn consume_reverse<T>(mut v: ~[T], f: &fn(uint, v: T)) {
unsafe {
do as_mut_buf(v) |p, ln| {
let mut i = ln;
while i > 0 {
i -= 1;

// NB: This unsafe operation counts on init writing 0s to the
// holes we create in the vector. That ensures that, if the
// iterator fails then we won't try to clean up the consumed
// elements during unwinding
let mut x = intrinsics::init();
let p = ptr::mut_offset(p, i);
x <-> *p;
f(i, x);
}
}

raw::set_len(&mut v, 0);
}
}

/// Remove the last element from a vector and return it
pub fn pop<T>(v: &mut ~[T]) -> T {
let ln = v.len();
Expand Down Expand Up @@ -1985,6 +2007,7 @@ pub trait OwnedVector<T> {
fn truncate(&mut self, newlen: uint);
fn retain(&mut self, f: &fn(t: &T) -> bool);
fn consume(self, f: &fn(uint, v: T));
fn consume_reverse(self, f: &fn(uint, v: T));
fn filter(self, f: &fn(t: &T) -> bool) -> ~[T];
fn partition(self, f: &fn(&T) -> bool) -> (~[T], ~[T]);
fn grow_fn(&mut self, n: uint, op: iter::InitOp<T>);
Expand Down Expand Up @@ -2046,6 +2069,11 @@ impl<T> OwnedVector<T> for ~[T] {
consume(self, f)
}

#[inline]
fn consume_reverse(self, f: &fn(uint, v: T)) {
consume_reverse(self, f)
}

#[inline]
fn filter(self, f: &fn(&T) -> bool) -> ~[T] {
filter(self, f)
Expand Down
21 changes: 5 additions & 16 deletions src/librustc/middle/astencode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ trait read_method_map_entry_helper {
fn encode_method_map_entry(ecx: @e::EncodeContext,
ebml_w: writer::Encoder,
mme: method_map_entry) {
do ebml_w.emit_rec {
do ebml_w.emit_struct("method_map_entry", 3) {
do ebml_w.emit_field(~"self_arg", 0u) {
ebml_w.emit_arg(ecx, mme.self_arg);
}
Expand All @@ -574,7 +574,7 @@ fn encode_method_map_entry(ecx: @e::EncodeContext,
impl read_method_map_entry_helper for reader::Decoder {
fn read_method_map_entry(&self, xcx: @ExtendedDecodeContext)
-> method_map_entry {
do self.read_rec {
do self.read_struct("method_map_entry", 3) {
method_map_entry {
self_arg: self.read_field(~"self_arg", 0u, || {
self.read_arg(xcx)
Expand Down Expand Up @@ -778,7 +778,7 @@ impl ebml_writer_helpers for writer::Encoder {

fn emit_tpbt(&self, ecx: @e::EncodeContext,
tpbt: ty::ty_param_bounds_and_ty) {
do self.emit_rec {
do self.emit_struct("ty_param_bounds_and_ty", 3) {
do self.emit_field(~"bounds", 0) {
do self.emit_from_vec(*tpbt.bounds) |bs| {
self.emit_bounds(ecx, *bs);
Expand Down Expand Up @@ -1045,7 +1045,7 @@ impl ebml_decoder_decoder_helpers for reader::Decoder {
fn read_ty_param_bounds_and_ty(&self, xcx: @ExtendedDecodeContext)
-> ty::ty_param_bounds_and_ty
{
do self.read_rec {
do self.read_struct("ty_param_bounds_and_ty", 3) {
ty::ty_param_bounds_and_ty {
bounds: self.read_field(~"bounds", 0u, || {
@self.read_to_vec(|| self.read_bounds(xcx) )
Expand Down Expand Up @@ -1212,7 +1212,6 @@ fn mk_ctxt() -> @fake_ext_ctxt {
#[cfg(test)]
fn roundtrip(in_item: Option<@ast::item>) {
use core::io;
use std::prettyprint;

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

let exp_str = do io::with_str_writer |w| {
in_item.encode(&prettyprint::Serializer(w))
};
let out_str = do io::with_str_writer |w| {
out_item.encode(&prettyprint::Serializer(w))
};

debug!("expected string: %s", exp_str);
debug!("actual string : %s", out_str);

assert!(exp_str == out_str);
assert_eq!(in_item, out_item);
}

#[test]
Expand Down
32 changes: 32 additions & 0 deletions src/libstd/deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ pub impl<T> Deque<T> {
get(self.elts, idx)
}

/// Iterate over the elements in the deque
fn each(&self, f: &fn(&T) -> bool) {
self.eachi(|_i, e| f(e))
}

/// Iterate over the elements in the deque by index
fn eachi(&self, f: &fn(uint, &T) -> bool) {
for uint::range(0, self.nelts) |i| {
if !f(i, self.get(i as int)) { return; }
}
}

/// Remove and return the first element in the deque
///
/// Fails if the deque is empty
Expand Down Expand Up @@ -223,6 +235,7 @@ mod tests {
assert!(*deq.get(3) == d);
}

#[test]
fn test_parameterized<T:Copy + Eq + Durable>(a: T, b: T, c: T, d: T) {
let mut deq = Deque::new();
assert!(deq.len() == 0);
Expand Down Expand Up @@ -300,4 +313,23 @@ mod tests {
let reccy4 = RecCy { x: 19, y: 252, t: Two(17, 42) };
test_parameterized::<RecCy>(reccy1, reccy2, reccy3, reccy4);
}

#[test]
fn test_eachi() {
let mut deq = Deque::new();
deq.add_back(1);
deq.add_back(2);
deq.add_back(3);

for deq.eachi |i, e| {
assert_eq!(*e, i + 1);
}

deq.pop_front();

for deq.eachi |i, e| {
assert_eq!(*e, i + 2);
}

}
}
104 changes: 35 additions & 69 deletions src/libstd/ebml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,23 +311,10 @@ pub mod reader {
fn read_f64(&self) -> f64 { fail!(~"read_f64()"); }
fn read_f32(&self) -> f32 { fail!(~"read_f32()"); }
fn read_float(&self) -> float { fail!(~"read_float()"); }

fn read_char(&self) -> char { fail!(~"read_char()"); }

fn read_owned_str(&self) -> ~str { doc_as_str(self.next_doc(EsStr)) }
fn read_managed_str(&self) -> @str { fail!(~"read_managed_str()"); }
fn read_str(&self) -> ~str { doc_as_str(self.next_doc(EsStr)) }

// Compound types:
fn read_owned<T>(&self, f: &fn() -> T) -> T {
debug!("read_owned()");
f()
}

fn read_managed<T>(&self, f: &fn() -> T) -> T {
debug!("read_managed()");
f()
}

fn read_enum<T>(&self, name: &str, f: &fn() -> T) -> T {
debug!("read_enum(%s)", name);
self._check_label(name);
Expand All @@ -348,34 +335,20 @@ pub mod reader {
f()
}

fn read_owned_vec<T>(&self, f: &fn(uint) -> T) -> T {
debug!("read_owned_vec()");
fn read_seq<T>(&self, f: &fn(uint) -> T) -> T {
debug!("read_seq()");
do self.push_doc(self.next_doc(EsVec)) {
let len = self._next_uint(EsVecLen);
debug!(" len=%u", len);
f(len)
}
}

fn read_managed_vec<T>(&self, f: &fn(uint) -> T) -> T {
debug!("read_managed_vec()");
do self.push_doc(self.next_doc(EsVec)) {
let len = self._next_uint(EsVecLen);
debug!(" len=%u", len);
f(len)
}
}

fn read_vec_elt<T>(&self, idx: uint, f: &fn() -> T) -> T {
debug!("read_vec_elt(idx=%u)", idx);
fn read_seq_elt<T>(&self, idx: uint, f: &fn() -> T) -> T {
debug!("read_seq_elt(idx=%u)", idx);
self.push_doc(self.next_doc(EsVecElt), f)
}

fn read_rec<T>(&self, f: &fn() -> T) -> T {
debug!("read_rec()");
f()
}

fn read_struct<T>(&self, name: &str, _len: uint, f: &fn() -> T) -> T {
debug!("read_struct(name=%s)", name);
f()
Expand All @@ -387,16 +360,6 @@ pub mod reader {
f()
}

fn read_tup<T>(&self, len: uint, f: &fn() -> T) -> T {
debug!("read_tup(len=%u)", len);
f()
}

fn read_tup_elt<T>(&self, idx: uint, f: &fn() -> T) -> T {
debug!("read_tup_elt(idx=%u)", idx);
f()
}

fn read_option<T>(&self, f: &fn(bool) -> T) -> T {
debug!("read_option()");
do self.read_enum("Option") || {
Expand All @@ -409,6 +372,21 @@ pub mod reader {
}
}
}

fn read_map<T>(&self, _f: &fn(uint) -> T) -> T {
debug!("read_map()");
fail!(~"read_map is unimplemented");
}

fn read_map_elt_key<T>(&self, idx: uint, _f: &fn() -> T) -> T {
debug!("read_map_elt_key(idx=%u)", idx);
fail!(~"read_map_elt_val is unimplemented");
}

fn read_map_elt_val<T>(&self, idx: uint, _f: &fn() -> T) -> T {
debug!("read_map_elt_val(idx=%u)", idx);
fail!(~"read_map_elt_val is unimplemented");
}
}
}

Expand Down Expand Up @@ -620,22 +598,10 @@ pub mod writer {
fail!(~"Unimplemented: serializing a char");
}

fn emit_borrowed_str(&self, v: &str) {
fn emit_str(&self, v: &str) {
self.wr_tagged_str(EsStr as uint, v)
}

fn emit_owned_str(&self, v: &str) {
self.emit_borrowed_str(v)
}

fn emit_managed_str(&self, v: &str) {
self.emit_borrowed_str(v)
}

fn emit_borrowed(&self, f: &fn()) { f() }
fn emit_owned(&self, f: &fn()) { f() }
fn emit_managed(&self, f: &fn()) { f() }

fn emit_enum(&self, name: &str, f: &fn()) {
self._emit_label(name);
self.wr_tag(EsEnum as uint, f)
Expand All @@ -647,35 +613,23 @@ pub mod writer {
}
fn emit_enum_variant_arg(&self, _idx: uint, f: &fn()) { f() }

fn emit_borrowed_vec(&self, len: uint, f: &fn()) {
fn emit_seq(&self, len: uint, f: &fn()) {
do self.wr_tag(EsVec as uint) {
self._emit_tagged_uint(EsVecLen, len);
f()
}
}

fn emit_owned_vec(&self, len: uint, f: &fn()) {
self.emit_borrowed_vec(len, f)
}

fn emit_managed_vec(&self, len: uint, f: &fn()) {
self.emit_borrowed_vec(len, f)
}

fn emit_vec_elt(&self, _idx: uint, f: &fn()) {
fn emit_seq_elt(&self, _idx: uint, f: &fn()) {
self.wr_tag(EsVecElt as uint, f)
}

fn emit_rec(&self, f: &fn()) { f() }
fn emit_struct(&self, _name: &str, _len: uint, f: &fn()) { f() }
fn emit_field(&self, name: &str, _idx: uint, f: &fn()) {
self._emit_label(name);
f()
}

fn emit_tup(&self, _len: uint, f: &fn()) { f() }
fn emit_tup_elt(&self, _idx: uint, f: &fn()) { f() }

fn emit_option(&self, f: &fn()) {
self.emit_enum("Option", f);
}
Expand All @@ -685,6 +639,18 @@ pub mod writer {
fn emit_option_some(&self, f: &fn()) {
self.emit_enum_variant("Some", 1, 1, f)
}

fn emit_map(&self, _len: uint, _f: &fn()) {
fail!(~"emit_map is unimplemented");
}

fn emit_map_elt_key(&self, _idx: uint, _f: &fn()) {
fail!(~"emit_map_elt_key is unimplemented");
}

fn emit_map_elt_val(&self, _idx: uint, _f: &fn()) {
fail!(~"emit_map_elt_val is unimplemented");
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/libstd/flatpipes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,8 +466,8 @@ pub mod flatteners {
fn from_writer(w: @Writer) -> Self;
}

impl<'self> FromReader for json::Decoder<'self> {
fn from_reader(r: @Reader) -> json::Decoder<'self> {
impl FromReader for json::Decoder {
fn from_reader(r: @Reader) -> json::Decoder {
match json::from_reader(r) {
Ok(json) => {
json::Decoder(json)
Expand Down
Loading