Skip to content

Commit b8852e8

Browse files
committed
pull extra::{serialize, ebml} into a separate libserialize crate
- `extra::json` didn't make the cut, because of `extra::json` required dep on `extra::TreeMap`. If/when `extra::TreeMap` moves out of `extra`, then `extra::json` could move into `serialize` - `libextra`, `libsyntax` and `librustc` depend on the newly created `libserialize` - The extensions to various `extra` types like `DList`, `RingBuf`, `TreeMap` and `TreeSet` for `Encodable`/`Decodable` were moved into the respective modules in `extra` - There is some trickery, evident in `src/libextra/lib.rs` where a stub of `extra::serialize` is set up (in `src/libextra/serialize.rs`) for use in the stage0 build, where the snapshot rustc is still making deriving for `Encodable` and `Decodable` point at extra. Big props to @huonw for help working out the re-export solution for this extra: inline extra::serialize stub fix stuff clobbered in rebase + don't reexport serialize::serialize no more globs in libserialize syntax: fix import of libserialize traits librustc: fix bad imports in encoder/decoder add serialize dep to librustdoc fix failing run-pass tests w/ serialize dep adjust uuid dep more rebase de-clobbering for libserialize fixing tests, pushing libextra dep into cfg(test) fix doc code in extra::json adjust index.md links to serialize and uuid library
1 parent 2bf575c commit b8852e8

30 files changed

+259
-193
lines changed

mk/crates.mk

+7-6
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,26 @@
4949
# automatically generated for all stage/host/target combinations.
5050
################################################################################
5151

52-
TARGET_CRATES := std extra green rustuv native flate arena glob term semver uuid sync
52+
TARGET_CRATES := std extra green rustuv native flate arena glob term semver uuid serialize sync
5353
HOST_CRATES := syntax rustc rustdoc
5454
CRATES := $(TARGET_CRATES) $(HOST_CRATES)
5555
TOOLS := compiletest rustdoc rustc
5656

5757
DEPS_std := native:rustrt
58-
DEPS_extra := std term sync
58+
DEPS_extra := std serialize sync term
5959
DEPS_green := std
6060
DEPS_rustuv := std native:uv native:uv_support
6161
DEPS_native := std
62-
DEPS_syntax := std extra term
63-
DEPS_rustc := syntax native:rustllvm flate arena sync
64-
DEPS_rustdoc := rustc native:sundown sync
62+
DEPS_syntax := std extra term serialize
63+
DEPS_rustc := syntax native:rustllvm flate arena serialize sync
64+
DEPS_rustdoc := rustc native:sundown serialize sync
6565
DEPS_flate := std native:miniz
6666
DEPS_arena := std extra
6767
DEPS_glob := std
68+
DEPS_serialize := std
6869
DEPS_term := std
6970
DEPS_semver := std
70-
DEPS_uuid := std extra
71+
DEPS_uuid := std serialize
7172
DEPS_sync := std
7273

7374
TOOL_DEPS_compiletest := extra green rustuv

src/doc/index.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ li {list-style-type: none; }
4141
* [The `flate` compression library](flate/index.html)
4242
* [The `glob` file path matching library](glob/index.html)
4343
* [The `semver` version collation library](semver/index.html)
44-
* [The `term` terminal-handling library](term/index.html)
45-
* [The UUID library](uuid/index.html)
44+
* [The `serialize` value encoding/decoding library](serialize/index.html)
4645
* [The `sync` library for concurrency-enabled mechanisms and primitives](sync/index.html)
46+
* [The `term` terminal-handling library](term/index.html)
47+
* [The `uuid` 128-bit universally unique identifier library](uuid/index.html)
4748

4849
# Tooling
4950

src/libextra/dlist.rs

+27
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ use std::iter;
3030

3131
use container::Deque;
3232

33+
use serialize::{Encodable, Decodable, Encoder, Decoder};
34+
3335
/// A doubly-linked list.
3436
pub struct DList<T> {
3537
priv length: uint,
@@ -628,6 +630,31 @@ impl<A: Clone> Clone for DList<A> {
628630
}
629631
}
630632

633+
impl<
634+
S: Encoder,
635+
T: Encodable<S>
636+
> Encodable<S> for DList<T> {
637+
fn encode(&self, s: &mut S) {
638+
s.emit_seq(self.len(), |s| {
639+
for (i, e) in self.iter().enumerate() {
640+
s.emit_seq_elt(i, |s| e.encode(s));
641+
}
642+
})
643+
}
644+
}
645+
646+
impl<D:Decoder,T:Decodable<D>> Decodable<D> for DList<T> {
647+
fn decode(d: &mut D) -> DList<T> {
648+
let mut list = DList::new();
649+
d.read_seq(|d, len| {
650+
for i in range(0u, len) {
651+
list.push_back(d.read_seq_elt(i, |d| Decodable::decode(d)));
652+
}
653+
});
654+
list
655+
}
656+
}
657+
631658
#[cfg(test)]
632659
mod tests {
633660
use container::Deque;

src/libextra/json.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,18 @@ A simple JSON document encoding a person, his/her age, address and phone numbers
5151
5252
Rust provides a mechanism for low boilerplate encoding & decoding
5353
of values to and from JSON via the serialization API.
54-
To be able to encode a piece of data, it must implement the `extra::serialize::Encodable` trait.
55-
To be able to decode a piece of data, it must implement the `extra::serialize::Decodable` trait.
54+
To be able to encode a piece of data, it must implement the `serialize::Encodable` trait.
55+
To be able to decode a piece of data, it must implement the `serialize::Decodable` trait.
5656
The Rust compiler provides an annotation to automatically generate
5757
the code for these traits: `#[deriving(Decodable, Encodable)]`
5858
5959
To encode using Encodable :
6060
6161
```rust
62+
extern mod serialize;
6263
use extra::json;
6364
use std::io;
64-
use extra::serialize::Encodable;
65+
use serialize::Encodable;
6566
6667
#[deriving(Encodable)]
6768
pub struct TestStruct {
@@ -125,7 +126,8 @@ fn main() {
125126
To decode a json string using `Decodable` trait :
126127
127128
```rust
128-
use extra::serialize::Decodable;
129+
extern mod serialize;
130+
use serialize::Decodable;
129131
130132
#[deriving(Decodable)]
131133
pub struct MyStruct {
@@ -150,8 +152,9 @@ Create a struct called TestStruct1 and serialize and deserialize it to and from
150152
using the serialization API, using the derived serialization code.
151153
152154
```rust
155+
extern mod serialize;
153156
use extra::json;
154-
use extra::serialize::{Encodable, Decodable};
157+
use serialize::{Encodable, Decodable};
155158
156159
#[deriving(Decodable, Encodable)] //generate Decodable, Encodable impl.
157160
pub struct TestStruct1 {
@@ -181,9 +184,10 @@ This example use the ToJson impl to unserialize the json string.
181184
Example of `ToJson` trait implementation for TestStruct1.
182185
183186
```rust
187+
extern mod serialize;
184188
use extra::json;
185189
use extra::json::ToJson;
186-
use extra::serialize::{Encodable, Decodable};
190+
use serialize::{Encodable, Decodable};
187191
use extra::treemap::TreeMap;
188192
189193
#[deriving(Decodable, Encodable)] // generate Decodable, Encodable impl.
@@ -312,7 +316,7 @@ impl<'a> Encoder<'a> {
312316
}
313317

314318
/// Encode the specified struct into a json [u8]
315-
pub fn buffer_encode<T:Encodable<Encoder<'a>>>(to_encode_object: &T) -> ~[u8] {
319+
pub fn buffer_encode<T:serialize::Encodable<Encoder<'a>>>(to_encode_object: &T) -> ~[u8] {
316320
//Serialize the object in a string using a writer
317321
let mut m = MemWriter::new();
318322
{
@@ -323,7 +327,7 @@ impl<'a> Encoder<'a> {
323327
}
324328

325329
/// Encode the specified struct into a json str
326-
pub fn str_encode<T:Encodable<Encoder<'a>>>(to_encode_object: &T) -> ~str {
330+
pub fn str_encode<T:serialize::Encodable<Encoder<'a>>>(to_encode_object: &T) -> ~str {
327331
let buff:~[u8] = Encoder::buffer_encode(to_encode_object);
328332
str::from_utf8_owned(buff).unwrap()
329333
}
@@ -684,7 +688,7 @@ impl<E: serialize::Encoder> serialize::Encodable<E> for Json {
684688
}
685689
}
686690

687-
impl Json{
691+
impl Json {
688692
/// Encodes a json value into a io::writer. Uses a single line.
689693
pub fn to_writer(&self, wr: &mut io::Writer) -> io::IoResult<()> {
690694
let mut encoder = Encoder::new(wr);

src/libextra/lib.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ Rust extras are part of the standard Rust distribution.
3535
#[deny(missing_doc)];
3636

3737
extern mod sync;
38+
#[cfg(not(stage0))]
39+
extern mod serialize;
40+
41+
#[cfg(stage0)]
42+
pub mod serialize {
43+
#[allow(missing_doc)];
44+
// Temp re-export until after a snapshot
45+
extern mod serialize = "serialize";
46+
pub use self::serialize::{Encoder, Decoder, Encodable, Decodable,
47+
EncoderHelpers, DecoderHelpers};
48+
}
3849

3950
#[cfg(stage0)]
4051
macro_rules! if_ok (
@@ -62,7 +73,6 @@ pub mod lru_cache;
6273
// And ... other stuff
6374

6475
pub mod url;
65-
pub mod ebml;
6676
pub mod getopts;
6777
pub mod json;
6878
pub mod tempfile;
@@ -85,7 +95,6 @@ mod unicode;
8595
// Compiler support modules
8696

8797
pub mod test;
88-
pub mod serialize;
8998

9099
// A curious inner-module that's not exported that contains the binding
91100
// 'extra' so that macro-expanded references to extra::serialize and such

src/libextra/ringbuf.rs

+27
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ use std::iter::{Rev, RandomAccessIterator};
1919

2020
use container::Deque;
2121

22+
use serialize::{Encodable, Decodable, Encoder, Decoder};
23+
2224
static INITIAL_CAPACITY: uint = 8u; // 2^3
2325
static MINIMUM_CAPACITY: uint = 2u;
2426

@@ -402,6 +404,31 @@ impl<A> Extendable<A> for RingBuf<A> {
402404
}
403405
}
404406

407+
impl<
408+
S: Encoder,
409+
T: Encodable<S>
410+
> Encodable<S> for RingBuf<T> {
411+
fn encode(&self, s: &mut S) {
412+
s.emit_seq(self.len(), |s| {
413+
for (i, e) in self.iter().enumerate() {
414+
s.emit_seq_elt(i, |s| e.encode(s));
415+
}
416+
})
417+
}
418+
}
419+
420+
impl<D:Decoder,T:Decodable<D>> Decodable<D> for RingBuf<T> {
421+
fn decode(d: &mut D) -> RingBuf<T> {
422+
let mut deque = RingBuf::new();
423+
d.read_seq(|d, len| {
424+
for i in range(0u, len) {
425+
deque.push_back(d.read_seq_elt(i, |d| Decodable::decode(d)));
426+
}
427+
});
428+
deque
429+
}
430+
}
431+
405432
#[cfg(test)]
406433
mod tests {
407434
use container::Deque;

src/libextra/treemap.rs

+67
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use std::iter::{Peekable};
1717
use std::cmp::Ordering;
1818
use std::ptr;
1919

20+
use serialize::{Encodable, Decodable, Encoder, Decoder};
21+
2022
// This is implemented as an AA tree, which is a simplified variation of
2123
// a red-black tree where red (horizontal) nodes can only be added
2224
// as a right child. The time complexity is the same, and re-balancing
@@ -1004,6 +1006,71 @@ impl<T: TotalOrd> Extendable<T> for TreeSet<T> {
10041006
}
10051007
}
10061008

1009+
impl<
1010+
E: Encoder,
1011+
K: Encodable<E> + Eq + TotalOrd,
1012+
V: Encodable<E> + Eq
1013+
> Encodable<E> for TreeMap<K, V> {
1014+
fn encode(&self, e: &mut E) {
1015+
e.emit_map(self.len(), |e| {
1016+
let mut i = 0;
1017+
for (key, val) in self.iter() {
1018+
e.emit_map_elt_key(i, |e| key.encode(e));
1019+
e.emit_map_elt_val(i, |e| val.encode(e));
1020+
i += 1;
1021+
}
1022+
})
1023+
}
1024+
}
1025+
1026+
impl<
1027+
D: Decoder,
1028+
K: Decodable<D> + Eq + TotalOrd,
1029+
V: Decodable<D> + Eq
1030+
> Decodable<D> for TreeMap<K, V> {
1031+
fn decode(d: &mut D) -> TreeMap<K, V> {
1032+
d.read_map(|d, len| {
1033+
let mut map = TreeMap::new();
1034+
for i in range(0u, len) {
1035+
let key = d.read_map_elt_key(i, |d| Decodable::decode(d));
1036+
let val = d.read_map_elt_val(i, |d| Decodable::decode(d));
1037+
map.insert(key, val);
1038+
}
1039+
map
1040+
})
1041+
}
1042+
}
1043+
1044+
impl<
1045+
S: Encoder,
1046+
T: Encodable<S> + Eq + TotalOrd
1047+
> Encodable<S> for TreeSet<T> {
1048+
fn encode(&self, s: &mut S) {
1049+
s.emit_seq(self.len(), |s| {
1050+
let mut i = 0;
1051+
for e in self.iter() {
1052+
s.emit_seq_elt(i, |s| e.encode(s));
1053+
i += 1;
1054+
}
1055+
})
1056+
}
1057+
}
1058+
1059+
impl<
1060+
D: Decoder,
1061+
T: Decodable<D> + Eq + TotalOrd
1062+
> Decodable<D> for TreeSet<T> {
1063+
fn decode(d: &mut D) -> TreeSet<T> {
1064+
d.read_seq(|d, len| {
1065+
let mut set = TreeSet::new();
1066+
for i in range(0u, len) {
1067+
set.insert(d.read_seq_elt(i, |d| Decodable::decode(d)));
1068+
}
1069+
set
1070+
})
1071+
}
1072+
}
1073+
10071074
#[cfg(test)]
10081075
mod test_treemap {
10091076

src/librustc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ extern mod extra;
3535
extern mod flate;
3636
extern mod arena;
3737
extern mod syntax;
38+
extern mod serialize;
3839
extern mod sync;
3940

4041
use back::link;

src/librustc/metadata/csearch.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use middle::ty;
1818
use middle::typeck;
1919

2020
use std::vec;
21+
use reader = serialize::ebml::reader;
2122
use std::rc::Rc;
22-
use reader = extra::ebml::reader;
2323
use syntax::ast;
2424
use syntax::ast_map;
2525
use syntax::diagnostic::expect;

src/librustc/metadata/decoder.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ use std::io::extensions::u64_from_be_bytes;
3131
use std::option;
3232
use std::rc::Rc;
3333
use std::vec;
34-
use extra::ebml::reader;
35-
use extra::ebml;
36-
use extra::serialize::Decodable;
34+
use serialize::ebml::reader;
35+
use serialize::ebml;
36+
use serialize::Decodable;
3737
use syntax::ast_map;
3838
use syntax::attr;
3939
use syntax::parse::token::{IdentInterner, special_idents};

src/librustc/metadata/encoder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use middle::ty;
2222
use middle::typeck;
2323
use middle;
2424

25-
use extra::serialize::Encodable;
25+
use serialize::Encodable;
2626
use std::cast;
2727
use std::cell::{Cell, RefCell};
2828
use std::hashmap::{HashMap, HashSet};
@@ -45,7 +45,7 @@ use syntax::parse::token;
4545
use syntax::visit::Visitor;
4646
use syntax::visit;
4747
use syntax;
48-
use writer = extra::ebml::writer;
48+
use writer = serialize::ebml::writer;
4949

5050
// used by astencode:
5151
type abbrev_map = @RefCell<HashMap<ty::t, tyencode::ty_abbrev>>;

src/librustc/middle/astencode.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ use std::cast;
3737
use std::io::Seek;
3838
use std::rc::Rc;
3939

40-
use extra::ebml::reader;
41-
use extra::ebml;
42-
use extra::serialize;
43-
use extra::serialize::{Encoder, Encodable, EncoderHelpers, DecoderHelpers};
44-
use extra::serialize::{Decoder, Decodable};
45-
use writer = extra::ebml::writer;
40+
use serialize::ebml::reader;
41+
use serialize::ebml;
42+
use serialize;
43+
use serialize::{Encoder, Encodable, EncoderHelpers, DecoderHelpers};
44+
use serialize::{Decoder, Decodable};
45+
use writer = serialize::ebml::writer;
4646

4747
#[cfg(test)] use syntax::parse;
4848
#[cfg(test)] use syntax::print::pprust;

0 commit comments

Comments
 (0)