Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

pull extra::{serialize, ebml} into a separate libserialize crate #11984

Merged
merged 1 commit into from
Feb 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 7 additions & 6 deletions mk/crates.mk
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,26 @@
# automatically generated for all stage/host/target combinations.
################################################################################

TARGET_CRATES := std extra green rustuv native flate arena glob term semver uuid sync
TARGET_CRATES := std extra green rustuv native flate arena glob term semver uuid serialize sync
HOST_CRATES := syntax rustc rustdoc
CRATES := $(TARGET_CRATES) $(HOST_CRATES)
TOOLS := compiletest rustdoc rustc

DEPS_std := native:rustrt
DEPS_extra := std term sync
DEPS_extra := std serialize sync term
DEPS_green := std
DEPS_rustuv := std native:uv native:uv_support
DEPS_native := std
DEPS_syntax := std extra term
DEPS_rustc := syntax native:rustllvm flate arena sync
DEPS_rustdoc := rustc native:sundown sync
DEPS_syntax := std extra term serialize
DEPS_rustc := syntax native:rustllvm flate arena serialize sync
DEPS_rustdoc := rustc native:sundown serialize sync
DEPS_flate := std native:miniz
DEPS_arena := std extra
DEPS_glob := std
DEPS_serialize := std
DEPS_term := std
DEPS_semver := std
DEPS_uuid := std extra
DEPS_uuid := std serialize
DEPS_sync := std

TOOL_DEPS_compiletest := extra green rustuv
Expand Down
5 changes: 3 additions & 2 deletions src/doc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ li {list-style-type: none; }
* [The `flate` compression library](flate/index.html)
* [The `glob` file path matching library](glob/index.html)
* [The `semver` version collation library](semver/index.html)
* [The `term` terminal-handling library](term/index.html)
* [The UUID library](uuid/index.html)
* [The `serialize` value encoding/decoding library](serialize/index.html)
* [The `sync` library for concurrency-enabled mechanisms and primitives](sync/index.html)
* [The `term` terminal-handling library](term/index.html)
* [The `uuid` 128-bit universally unique identifier library](uuid/index.html)

# Tooling

Expand Down
27 changes: 27 additions & 0 deletions src/libextra/dlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ use std::iter;

use container::Deque;

use serialize::{Encodable, Decodable, Encoder, Decoder};

/// A doubly-linked list.
pub struct DList<T> {
priv length: uint,
Expand Down Expand Up @@ -628,6 +630,31 @@ impl<A: Clone> Clone for DList<A> {
}
}

impl<
S: Encoder,
T: Encodable<S>
> Encodable<S> for DList<T> {
fn encode(&self, s: &mut S) {
s.emit_seq(self.len(), |s| {
for (i, e) in self.iter().enumerate() {
s.emit_seq_elt(i, |s| e.encode(s));
}
})
}
}

impl<D:Decoder,T:Decodable<D>> Decodable<D> for DList<T> {
fn decode(d: &mut D) -> DList<T> {
let mut list = DList::new();
d.read_seq(|d, len| {
for i in range(0u, len) {
list.push_back(d.read_seq_elt(i, |d| Decodable::decode(d)));
}
});
list
}
}

#[cfg(test)]
mod tests {
use container::Deque;
Expand Down
22 changes: 13 additions & 9 deletions src/libextra/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,18 @@ A simple JSON document encoding a person, his/her age, address and phone numbers

Rust provides a mechanism for low boilerplate encoding & decoding
of values to and from JSON via the serialization API.
To be able to encode a piece of data, it must implement the `extra::serialize::Encodable` trait.
To be able to decode a piece of data, it must implement the `extra::serialize::Decodable` trait.
To be able to encode a piece of data, it must implement the `serialize::Encodable` trait.
To be able to decode a piece of data, it must implement the `serialize::Decodable` trait.
The Rust compiler provides an annotation to automatically generate
the code for these traits: `#[deriving(Decodable, Encodable)]`

To encode using Encodable :

```rust
extern mod serialize;
use extra::json;
use std::io;
use extra::serialize::Encodable;
use serialize::Encodable;

#[deriving(Encodable)]
pub struct TestStruct {
Expand Down Expand Up @@ -125,7 +126,8 @@ fn main() {
To decode a json string using `Decodable` trait :

```rust
use extra::serialize::Decodable;
extern mod serialize;
use serialize::Decodable;

#[deriving(Decodable)]
pub struct MyStruct {
Expand All @@ -150,8 +152,9 @@ Create a struct called TestStruct1 and serialize and deserialize it to and from
using the serialization API, using the derived serialization code.

```rust
extern mod serialize;
use extra::json;
use extra::serialize::{Encodable, Decodable};
use serialize::{Encodable, Decodable};

#[deriving(Decodable, Encodable)] //generate Decodable, Encodable impl.
pub struct TestStruct1 {
Expand Down Expand Up @@ -181,9 +184,10 @@ This example use the ToJson impl to unserialize the json string.
Example of `ToJson` trait implementation for TestStruct1.

```rust
extern mod serialize;
use extra::json;
use extra::json::ToJson;
use extra::serialize::{Encodable, Decodable};
use serialize::{Encodable, Decodable};
use extra::treemap::TreeMap;

#[deriving(Decodable, Encodable)] // generate Decodable, Encodable impl.
Expand Down Expand Up @@ -312,7 +316,7 @@ impl<'a> Encoder<'a> {
}

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

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

impl Json{
impl Json {
/// Encodes a json value into a io::writer. Uses a single line.
pub fn to_writer(&self, wr: &mut io::Writer) -> io::IoResult<()> {
let mut encoder = Encoder::new(wr);
Expand Down
13 changes: 11 additions & 2 deletions src/libextra/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ Rust extras are part of the standard Rust distribution.
#[deny(missing_doc)];

extern mod sync;
#[cfg(not(stage0))]
extern mod serialize;

#[cfg(stage0)]
pub mod serialize {
#[allow(missing_doc)];
// Temp re-export until after a snapshot
extern mod serialize = "serialize";
pub use self::serialize::{Encoder, Decoder, Encodable, Decodable,
EncoderHelpers, DecoderHelpers};
}

#[cfg(stage0)]
macro_rules! if_ok (
Expand Down Expand Up @@ -62,7 +73,6 @@ pub mod lru_cache;
// And ... other stuff

pub mod url;
pub mod ebml;
pub mod getopts;
pub mod json;
pub mod tempfile;
Expand All @@ -85,7 +95,6 @@ mod unicode;
// Compiler support modules

pub mod test;
pub mod serialize;

// A curious inner-module that's not exported that contains the binding
// 'extra' so that macro-expanded references to extra::serialize and such
Expand Down
27 changes: 27 additions & 0 deletions src/libextra/ringbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use std::iter::{Rev, RandomAccessIterator};

use container::Deque;

use serialize::{Encodable, Decodable, Encoder, Decoder};

static INITIAL_CAPACITY: uint = 8u; // 2^3
static MINIMUM_CAPACITY: uint = 2u;

Expand Down Expand Up @@ -402,6 +404,31 @@ impl<A> Extendable<A> for RingBuf<A> {
}
}

impl<
S: Encoder,
T: Encodable<S>
> Encodable<S> for RingBuf<T> {
fn encode(&self, s: &mut S) {
s.emit_seq(self.len(), |s| {
for (i, e) in self.iter().enumerate() {
s.emit_seq_elt(i, |s| e.encode(s));
}
})
}
}

impl<D:Decoder,T:Decodable<D>> Decodable<D> for RingBuf<T> {
fn decode(d: &mut D) -> RingBuf<T> {
let mut deque = RingBuf::new();
d.read_seq(|d, len| {
for i in range(0u, len) {
deque.push_back(d.read_seq_elt(i, |d| Decodable::decode(d)));
}
});
deque
}
}

#[cfg(test)]
mod tests {
use container::Deque;
Expand Down
67 changes: 67 additions & 0 deletions src/libextra/treemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use std::iter::{Peekable};
use std::cmp::Ordering;
use std::ptr;

use serialize::{Encodable, Decodable, Encoder, Decoder};

// This is implemented as an AA tree, which is a simplified variation of
// a red-black tree where red (horizontal) nodes can only be added
// as a right child. The time complexity is the same, and re-balancing
Expand Down Expand Up @@ -1004,6 +1006,71 @@ impl<T: TotalOrd> Extendable<T> for TreeSet<T> {
}
}

impl<
E: Encoder,
K: Encodable<E> + Eq + TotalOrd,
V: Encodable<E> + Eq
> Encodable<E> for TreeMap<K, V> {
fn encode(&self, e: &mut E) {
e.emit_map(self.len(), |e| {
let mut i = 0;
for (key, val) in self.iter() {
e.emit_map_elt_key(i, |e| key.encode(e));
e.emit_map_elt_val(i, |e| val.encode(e));
i += 1;
}
})
}
}

impl<
D: Decoder,
K: Decodable<D> + Eq + TotalOrd,
V: Decodable<D> + Eq
> Decodable<D> for TreeMap<K, V> {
fn decode(d: &mut D) -> TreeMap<K, V> {
d.read_map(|d, len| {
let mut map = TreeMap::new();
for i in range(0u, len) {
let key = d.read_map_elt_key(i, |d| Decodable::decode(d));
let val = d.read_map_elt_val(i, |d| Decodable::decode(d));
map.insert(key, val);
}
map
})
}
}

impl<
S: Encoder,
T: Encodable<S> + Eq + TotalOrd
> Encodable<S> for TreeSet<T> {
fn encode(&self, s: &mut S) {
s.emit_seq(self.len(), |s| {
let mut i = 0;
for e in self.iter() {
s.emit_seq_elt(i, |s| e.encode(s));
i += 1;
}
})
}
}

impl<
D: Decoder,
T: Decodable<D> + Eq + TotalOrd
> Decodable<D> for TreeSet<T> {
fn decode(d: &mut D) -> TreeSet<T> {
d.read_seq(|d, len| {
let mut set = TreeSet::new();
for i in range(0u, len) {
set.insert(d.read_seq_elt(i, |d| Decodable::decode(d)));
}
set
})
}
}

#[cfg(test)]
mod test_treemap {

Expand Down
1 change: 1 addition & 0 deletions src/librustc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ extern mod extra;
extern mod flate;
extern mod arena;
extern mod syntax;
extern mod serialize;
extern mod sync;

use back::link;
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/metadata/csearch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use middle::ty;
use middle::typeck;

use std::vec;
use reader = serialize::ebml::reader;
use std::rc::Rc;
use reader = extra::ebml::reader;
use syntax::ast;
use syntax::ast_map;
use syntax::diagnostic::expect;
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ use std::io::extensions::u64_from_be_bytes;
use std::option;
use std::rc::Rc;
use std::vec;
use extra::ebml::reader;
use extra::ebml;
use extra::serialize::Decodable;
use serialize::ebml::reader;
use serialize::ebml;
use serialize::Decodable;
use syntax::ast_map;
use syntax::attr;
use syntax::parse::token::{IdentInterner, special_idents};
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use middle::ty;
use middle::typeck;
use middle;

use extra::serialize::Encodable;
use serialize::Encodable;
use std::cast;
use std::cell::{Cell, RefCell};
use std::hashmap::{HashMap, HashSet};
Expand All @@ -45,7 +45,7 @@ use syntax::parse::token;
use syntax::visit::Visitor;
use syntax::visit;
use syntax;
use writer = extra::ebml::writer;
use writer = serialize::ebml::writer;

// used by astencode:
type abbrev_map = @RefCell<HashMap<ty::t, tyencode::ty_abbrev>>;
Expand Down
12 changes: 6 additions & 6 deletions src/librustc/middle/astencode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ use std::cast;
use std::io::Seek;
use std::rc::Rc;

use extra::ebml::reader;
use extra::ebml;
use extra::serialize;
use extra::serialize::{Encoder, Encodable, EncoderHelpers, DecoderHelpers};
use extra::serialize::{Decoder, Decodable};
use writer = extra::ebml::writer;
use serialize::ebml::reader;
use serialize::ebml;
use serialize;
use serialize::{Encoder, Encodable, EncoderHelpers, DecoderHelpers};
use serialize::{Decoder, Decodable};
use writer = serialize::ebml::writer;

#[cfg(test)] use syntax::parse;
#[cfg(test)] use syntax::print::pprust;
Expand Down
Loading