auto merge of #12453 : alexcrichton/rust/move-json, r=brson
This also inverts the dependency between libserialize and libcollections. cc #8784
This commit is contained in:
commit
994b48cf52
@ -68,15 +68,15 @@ DEPS_rustdoc := rustc native:sundown serialize sync getopts collections \
|
||||
DEPS_flate := std extra native:miniz
|
||||
DEPS_arena := std collections
|
||||
DEPS_glob := std
|
||||
DEPS_serialize := std
|
||||
DEPS_serialize := std collections
|
||||
DEPS_term := std collections
|
||||
DEPS_semver := std
|
||||
DEPS_uuid := std serialize
|
||||
DEPS_sync := std
|
||||
DEPS_getopts := std
|
||||
DEPS_collections := std serialize
|
||||
DEPS_collections := std
|
||||
DEPS_fourcc := syntax std
|
||||
DEPS_num := std extra
|
||||
DEPS_num := std
|
||||
DEPS_test := std extra collections getopts serialize term
|
||||
DEPS_time := std serialize
|
||||
|
||||
|
@ -875,16 +875,16 @@ An example of what will and will not work for `use` items:
|
||||
|
||||
~~~~
|
||||
# #[allow(unused_imports)];
|
||||
use foo::extra::json; // good: foo is at the root of the crate
|
||||
use foo::native::start; // good: foo is at the root of the crate
|
||||
use foo::baz::foobaz; // good: foo is at the root of the crate
|
||||
|
||||
mod foo {
|
||||
extern crate extra;
|
||||
extern crate native;
|
||||
|
||||
use foo::extra::json; // good: foo is at crate root
|
||||
// use extra::json::*; // bad: extra is not at the crate root
|
||||
use self::baz::foobaz; // good: self refers to module 'foo'
|
||||
use foo::bar::foobar; // good: foo is at crate root
|
||||
use foo::native::start; // good: foo is at crate root
|
||||
// use native::start; // bad: native is not at the crate root
|
||||
use self::baz::foobaz; // good: self refers to module 'foo'
|
||||
use foo::bar::foobar; // good: foo is at crate root
|
||||
|
||||
pub mod bar {
|
||||
pub fn foobar() { }
|
||||
|
@ -30,8 +30,6 @@ use std::iter;
|
||||
|
||||
use deque::Deque;
|
||||
|
||||
use serialize::{Encodable, Decodable, Encoder, Decoder};
|
||||
|
||||
/// A doubly-linked list.
|
||||
pub struct DList<T> {
|
||||
priv length: uint,
|
||||
@ -630,31 +628,6 @@ 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 {
|
||||
extern crate test;
|
||||
|
@ -15,7 +15,7 @@
|
||||
|
||||
use std::num::Bitwise;
|
||||
|
||||
#[deriving(Clone, Eq, Hash, Show, Encodable, Decodable)]
|
||||
#[deriving(Clone, Eq, Hash, Show)]
|
||||
/// A specialized Set implementation to use enum types.
|
||||
pub struct EnumSet<E> {
|
||||
// We must maintain the invariant that no bits are set
|
||||
|
@ -65,8 +65,6 @@ use std::vec::{Items, MutItems};
|
||||
use std::vec_ng::Vec;
|
||||
use std::vec_ng;
|
||||
|
||||
use serialize::{Encodable, Decodable, Encoder, Decoder};
|
||||
|
||||
static INITIAL_CAPACITY: uint = 32u; // 2^5
|
||||
|
||||
struct Bucket<K,V> {
|
||||
@ -912,71 +910,6 @@ pub type SetAlgebraItems<'a, T> =
|
||||
FilterMap<'static,(&'a HashSet<T>, &'a T), &'a T,
|
||||
Zip<Repeat<&'a HashSet<T>>,SetItems<'a,T>>>;
|
||||
|
||||
impl<
|
||||
E: Encoder,
|
||||
K: Encodable<E> + Hash + Eq,
|
||||
V: Encodable<E>
|
||||
> Encodable<E> for HashMap<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> + Hash + Eq,
|
||||
V: Decodable<D>
|
||||
> Decodable<D> for HashMap<K, V> {
|
||||
fn decode(d: &mut D) -> HashMap<K, V> {
|
||||
d.read_map(|d, len| {
|
||||
let mut map = HashMap::with_capacity(len);
|
||||
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> + Hash + Eq
|
||||
> Encodable<S> for HashSet<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> + Hash + Eq
|
||||
> Decodable<D> for HashSet<T> {
|
||||
fn decode(d: &mut D) -> HashSet<T> {
|
||||
d.read_seq(|d, len| {
|
||||
let mut set = HashSet::with_capacity(len);
|
||||
for i in range(0u, len) {
|
||||
set.insert(d.read_seq_elt(i, |d| Decodable::decode(d)));
|
||||
}
|
||||
set
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test_map {
|
||||
use super::{HashMap, HashSet};
|
||||
|
@ -19,7 +19,6 @@
|
||||
|
||||
#[feature(macro_rules, managed_boxes)];
|
||||
|
||||
extern crate serialize;
|
||||
#[cfg(test)] extern crate test;
|
||||
|
||||
pub use bitv::Bitv;
|
||||
|
@ -19,8 +19,6 @@ use std::iter::{Rev, RandomAccessIterator};
|
||||
|
||||
use deque::Deque;
|
||||
|
||||
use serialize::{Encodable, Decodable, Encoder, Decoder};
|
||||
|
||||
static INITIAL_CAPACITY: uint = 8u; // 2^3
|
||||
static MINIMUM_CAPACITY: uint = 2u;
|
||||
|
||||
@ -404,31 +402,6 @@ 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 {
|
||||
extern crate test;
|
||||
|
@ -17,8 +17,6 @@ use std::cmp::Ordering;
|
||||
use std::mem::{replace, swap};
|
||||
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
|
||||
@ -1006,71 +1004,6 @@ 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 {
|
||||
|
||||
|
@ -16,8 +16,6 @@ use std::mem::init;
|
||||
use std::vec;
|
||||
use std::vec::{Items, MutItems};
|
||||
|
||||
use serialize::{Encodable, Decodable, Encoder, Decoder};
|
||||
|
||||
// FIXME: #5244: need to manually update the TrieNode constructor
|
||||
static SHIFT: uint = 4;
|
||||
static SIZE: uint = 1 << SHIFT;
|
||||
@ -620,59 +618,6 @@ impl<'a> Iterator<uint> for SetItems<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<
|
||||
E: Encoder,
|
||||
V: Encodable<E>
|
||||
> Encodable<E> for TrieMap<V> {
|
||||
fn encode(&self, e: &mut E) {
|
||||
e.emit_map(self.len(), |e| {
|
||||
for (i, (key, val)) in self.iter().enumerate() {
|
||||
e.emit_map_elt_key(i, |e| key.encode(e));
|
||||
e.emit_map_elt_val(i, |e| val.encode(e));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl<
|
||||
D: Decoder,
|
||||
V: Decodable<D>
|
||||
> Decodable<D> for TrieMap<V> {
|
||||
fn decode(d: &mut D) -> TrieMap<V> {
|
||||
d.read_map(|d, len| {
|
||||
let mut map = TrieMap::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> Encodable<S> for TrieSet {
|
||||
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> Decodable<D> for TrieSet {
|
||||
fn decode(d: &mut D) -> TrieSet {
|
||||
d.read_seq(|d, len| {
|
||||
let mut set = TrieSet::new();
|
||||
for i in range(0u, len) {
|
||||
set.insert(d.read_seq_elt(i, |d| Decodable::decode(d)));
|
||||
}
|
||||
set
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test_map {
|
||||
use super::{TrieMap, TrieNode, Internal, External};
|
||||
|
@ -42,19 +42,9 @@ extern crate time;
|
||||
// Utility modules
|
||||
pub mod c_vec;
|
||||
pub mod url;
|
||||
pub mod json;
|
||||
pub mod tempfile;
|
||||
pub mod workcache;
|
||||
pub mod stats;
|
||||
|
||||
#[cfg(unicode)]
|
||||
mod unicode;
|
||||
|
||||
// A curious inner-module that's not exported that contains the binding
|
||||
// 'extra' so that macro-expanded references to extra::serialize and such
|
||||
// can be resolved within libextra.
|
||||
#[doc(hidden)]
|
||||
pub mod extra {
|
||||
pub use serialize;
|
||||
}
|
||||
|
||||
|
@ -10,8 +10,8 @@
|
||||
|
||||
#[allow(missing_doc)];
|
||||
|
||||
use json;
|
||||
use json::ToJson;
|
||||
use serialize::json;
|
||||
use serialize::json::ToJson;
|
||||
use serialize::{Encoder, Encodable, Decoder, Decodable};
|
||||
use sync::{Arc,RWArc};
|
||||
use collections::TreeMap;
|
||||
|
@ -15,8 +15,6 @@
|
||||
#[crate_type = "dylib"];
|
||||
#[license = "MIT/ASL2"];
|
||||
|
||||
extern crate extra;
|
||||
|
||||
pub mod bigint;
|
||||
pub mod rational;
|
||||
pub mod complex;
|
||||
|
@ -27,8 +27,7 @@ use middle;
|
||||
use util::common::time;
|
||||
use util::ppaux;
|
||||
|
||||
use extra::json;
|
||||
use serialize::Encodable;
|
||||
use serialize::{json, Encodable};
|
||||
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::io;
|
||||
|
@ -42,7 +42,7 @@ use std::vec;
|
||||
use collections::{HashMap, HashSet};
|
||||
|
||||
use sync::Arc;
|
||||
use extra::json::ToJson;
|
||||
use serialize::json::ToJson;
|
||||
use syntax::ast;
|
||||
use syntax::attr;
|
||||
use syntax::parse::token::InternedString;
|
||||
|
@ -30,8 +30,7 @@ use std::local_data;
|
||||
use std::io;
|
||||
use std::io::{File, MemWriter};
|
||||
use std::str;
|
||||
use extra::json;
|
||||
use serialize::{Decodable, Encodable};
|
||||
use serialize::{json, Decodable, Encodable};
|
||||
|
||||
pub mod clean;
|
||||
pub mod core;
|
||||
|
@ -10,7 +10,7 @@
|
||||
|
||||
use clean;
|
||||
|
||||
use extra::json;
|
||||
use serialize::json;
|
||||
use dl = std::unstable::dynamic_lib;
|
||||
|
||||
pub type PluginJson = Option<(~str, json::Json)>;
|
||||
|
281
src/libserialize/collection_impls.rs
Normal file
281
src/libserialize/collection_impls.rs
Normal file
@ -0,0 +1,281 @@
|
||||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
//! Implementations of serialization for structures found in libcollections
|
||||
|
||||
use std::uint;
|
||||
use std::hash::Hash;
|
||||
|
||||
use {Decodable, Encodable, Decoder, Encoder};
|
||||
use collections::{DList, RingBuf, TreeMap, TreeSet, Deque, HashMap, HashSet,
|
||||
TrieMap, TrieSet};
|
||||
use collections::enum_set::{EnumSet, CLike};
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<
|
||||
S: Encoder,
|
||||
T: Encodable<S> + CLike
|
||||
> Encodable<S> for EnumSet<T> {
|
||||
fn encode(&self, s: &mut S) {
|
||||
let mut bits = 0;
|
||||
for item in self.iter() {
|
||||
bits |= item.to_uint();
|
||||
}
|
||||
s.emit_uint(bits);
|
||||
}
|
||||
}
|
||||
|
||||
impl<
|
||||
D: Decoder,
|
||||
T: Decodable<D> + CLike
|
||||
> Decodable<D> for EnumSet<T> {
|
||||
fn decode(d: &mut D) -> EnumSet<T> {
|
||||
let bits = d.read_uint();
|
||||
let mut set = EnumSet::empty();
|
||||
for bit in range(0, uint::BITS) {
|
||||
if bits & (1 << bit) != 0 {
|
||||
set.add(CLike::from_uint(1 << bit));
|
||||
}
|
||||
}
|
||||
set
|
||||
}
|
||||
}
|
||||
|
||||
impl<
|
||||
E: Encoder,
|
||||
K: Encodable<E> + Hash + Eq,
|
||||
V: Encodable<E>
|
||||
> Encodable<E> for HashMap<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> + Hash + Eq,
|
||||
V: Decodable<D>
|
||||
> Decodable<D> for HashMap<K, V> {
|
||||
fn decode(d: &mut D) -> HashMap<K, V> {
|
||||
d.read_map(|d, len| {
|
||||
let mut map = HashMap::with_capacity(len);
|
||||
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> + Hash + Eq
|
||||
> Encodable<S> for HashSet<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> + Hash + Eq
|
||||
> Decodable<D> for HashSet<T> {
|
||||
fn decode(d: &mut D) -> HashSet<T> {
|
||||
d.read_seq(|d, len| {
|
||||
let mut set = HashSet::with_capacity(len);
|
||||
for i in range(0u, len) {
|
||||
set.insert(d.read_seq_elt(i, |d| Decodable::decode(d)));
|
||||
}
|
||||
set
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<
|
||||
E: Encoder,
|
||||
V: Encodable<E>
|
||||
> Encodable<E> for TrieMap<V> {
|
||||
fn encode(&self, e: &mut E) {
|
||||
e.emit_map(self.len(), |e| {
|
||||
for (i, (key, val)) in self.iter().enumerate() {
|
||||
e.emit_map_elt_key(i, |e| key.encode(e));
|
||||
e.emit_map_elt_val(i, |e| val.encode(e));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl<
|
||||
D: Decoder,
|
||||
V: Decodable<D>
|
||||
> Decodable<D> for TrieMap<V> {
|
||||
fn decode(d: &mut D) -> TrieMap<V> {
|
||||
d.read_map(|d, len| {
|
||||
let mut map = TrieMap::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> Encodable<S> for TrieSet {
|
||||
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> Decodable<D> for TrieSet {
|
||||
fn decode(d: &mut D) -> TrieSet {
|
||||
d.read_seq(|d, len| {
|
||||
let mut set = TrieSet::new();
|
||||
for i in range(0u, len) {
|
||||
set.insert(d.read_seq_elt(i, |d| Decodable::decode(d)));
|
||||
}
|
||||
set
|
||||
})
|
||||
}
|
||||
}
|
@ -59,11 +59,8 @@ the code for these traits: `#[deriving(Decodable, Encodable)]`
|
||||
To encode using Encodable :
|
||||
|
||||
```rust
|
||||
extern crate extra;
|
||||
extern crate serialize;
|
||||
use extra::json;
|
||||
use std::io;
|
||||
use serialize::Encodable;
|
||||
use serialize::{json, Encodable};
|
||||
|
||||
#[deriving(Encodable)]
|
||||
pub struct TestStruct {
|
||||
@ -84,7 +81,7 @@ Two wrapper functions are provided to encode a Encodable object
|
||||
into a string (~str) or buffer (~[u8]): `str_encode(&m)` and `buffer_encode(&m)`.
|
||||
|
||||
```rust
|
||||
use extra::json;
|
||||
use serialize::json;
|
||||
let to_encode_object = ~"example of string to encode";
|
||||
let encoded_str: ~str = json::Encoder::str_encode(&to_encode_object);
|
||||
```
|
||||
@ -99,11 +96,11 @@ A basic `ToJson` example using a TreeMap of attribute name / attribute value:
|
||||
|
||||
|
||||
```rust
|
||||
extern crate extra;
|
||||
extern crate collections;
|
||||
extern crate serialize;
|
||||
|
||||
use extra::json;
|
||||
use extra::json::ToJson;
|
||||
use serialize::json;
|
||||
use serialize::json::ToJson;
|
||||
use collections::TreeMap;
|
||||
|
||||
pub struct MyStruct {
|
||||
@ -130,9 +127,8 @@ fn main() {
|
||||
To decode a JSON string using `Decodable` trait :
|
||||
|
||||
```rust
|
||||
extern crate extra;
|
||||
extern crate serialize;
|
||||
use serialize::Decodable;
|
||||
use serialize::{json, Decodable};
|
||||
|
||||
#[deriving(Decodable)]
|
||||
pub struct MyStruct {
|
||||
@ -143,8 +139,8 @@ pub struct MyStruct {
|
||||
fn main() {
|
||||
let json_str_to_decode: ~str =
|
||||
~"{\"attr1\":1,\"attr2\":\"toto\"}";
|
||||
let json_object = extra::json::from_str(json_str_to_decode);
|
||||
let mut decoder = extra::json::Decoder::new(json_object.unwrap());
|
||||
let json_object = json::from_str(json_str_to_decode);
|
||||
let mut decoder = json::Decoder::new(json_object.unwrap());
|
||||
let decoded_object: MyStruct = Decodable::decode(&mut decoder); // create the final object
|
||||
}
|
||||
```
|
||||
@ -157,10 +153,8 @@ Create a struct called TestStruct1 and serialize and deserialize it to and from
|
||||
using the serialization API, using the derived serialization code.
|
||||
|
||||
```rust
|
||||
extern crate extra;
|
||||
extern crate serialize;
|
||||
use extra::json;
|
||||
use serialize::{Encodable, Decodable};
|
||||
use serialize::{json, Encodable, Decodable};
|
||||
|
||||
#[deriving(Decodable, Encodable)] //generate Decodable, Encodable impl.
|
||||
pub struct TestStruct1 {
|
||||
@ -176,9 +170,9 @@ fn main() {
|
||||
{data_int: 1, data_str:~"toto", data_vector:~[2,3,4,5]};
|
||||
let encoded_str: ~str = json::Encoder::str_encode(&to_encode_object);
|
||||
|
||||
// To deserialize use the `extra::json::from_str` and `extra::json::Decoder`
|
||||
// To deserialize use the `json::from_str` and `json::Decoder`
|
||||
|
||||
let json_object = extra::json::from_str(encoded_str);
|
||||
let json_object = json::from_str(encoded_str);
|
||||
let mut decoder = json::Decoder::new(json_object.unwrap());
|
||||
let decoded1: TestStruct1 = Decodable::decode(&mut decoder); // create the final object
|
||||
}
|
||||
@ -190,13 +184,11 @@ This example use the ToJson impl to deserialize the JSON string.
|
||||
Example of `ToJson` trait implementation for TestStruct1.
|
||||
|
||||
```rust
|
||||
extern crate extra;
|
||||
extern crate serialize;
|
||||
extern crate collections;
|
||||
|
||||
use extra::json;
|
||||
use extra::json::ToJson;
|
||||
use serialize::{Encodable, Decodable};
|
||||
use serialize::json::ToJson;
|
||||
use serialize::{json, Encodable, Decodable};
|
||||
use collections::TreeMap;
|
||||
|
||||
#[deriving(Decodable, Encodable)] // generate Decodable, Encodable impl.
|
||||
@ -242,8 +234,7 @@ use std::num;
|
||||
use std::str;
|
||||
use std::fmt;
|
||||
|
||||
use serialize::Encodable;
|
||||
use serialize;
|
||||
use Encodable;
|
||||
use collections::TreeMap;
|
||||
|
||||
macro_rules! try( ($e:expr) => (
|
||||
@ -324,7 +315,7 @@ impl<'a> Encoder<'a> {
|
||||
}
|
||||
|
||||
/// Encode the specified struct into a json [u8]
|
||||
pub fn buffer_encode<T:serialize::Encodable<Encoder<'a>>>(to_encode_object: &T) -> ~[u8] {
|
||||
pub fn buffer_encode<T:Encodable<Encoder<'a>>>(to_encode_object: &T) -> ~[u8] {
|
||||
//Serialize the object in a string using a writer
|
||||
let mut m = MemWriter::new();
|
||||
{
|
||||
@ -335,13 +326,13 @@ impl<'a> Encoder<'a> {
|
||||
}
|
||||
|
||||
/// Encode the specified struct into a json str
|
||||
pub fn str_encode<T:serialize::Encodable<Encoder<'a>>>(to_encode_object: &T) -> ~str {
|
||||
pub fn str_encode<T:Encodable<Encoder<'a>>>(to_encode_object: &T) -> ~str {
|
||||
let buff:~[u8] = Encoder::buffer_encode(to_encode_object);
|
||||
str::from_utf8_owned(buff).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> serialize::Encoder for Encoder<'a> {
|
||||
impl<'a> ::Encoder for Encoder<'a> {
|
||||
fn emit_nil(&mut self) { try!(write!(self.wr, "null")) }
|
||||
|
||||
fn emit_uint(&mut self, v: uint) { self.emit_f64(v as f64); }
|
||||
@ -502,7 +493,7 @@ impl<'a> PrettyEncoder<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> serialize::Encoder for PrettyEncoder<'a> {
|
||||
impl<'a> ::Encoder for PrettyEncoder<'a> {
|
||||
fn emit_nil(&mut self) { try!(write!(self.wr, "null")); }
|
||||
|
||||
fn emit_uint(&mut self, v: uint) { self.emit_f64(v as f64); }
|
||||
@ -683,7 +674,7 @@ impl<'a> serialize::Encoder for PrettyEncoder<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: serialize::Encoder> serialize::Encodable<E> for Json {
|
||||
impl<E: ::Encoder> Encodable<E> for Json {
|
||||
fn encode(&self, e: &mut E) {
|
||||
match *self {
|
||||
Number(v) => v.encode(e),
|
||||
@ -1154,7 +1145,7 @@ impl Decoder {
|
||||
}
|
||||
}
|
||||
|
||||
impl serialize::Decoder for Decoder {
|
||||
impl ::Decoder for Decoder {
|
||||
fn read_nil(&mut self) -> () {
|
||||
debug!("read_nil");
|
||||
match self.stack.pop().unwrap() {
|
||||
@ -1591,10 +1582,10 @@ impl fmt::Show for Error {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
use {Encodable, Decodable};
|
||||
use super::{Encoder, Decoder, Error, Boolean, Number, List, String, Null,
|
||||
PrettyEncoder, Object, Json, from_str};
|
||||
use std::io;
|
||||
use serialize::{Encodable, Decodable};
|
||||
use collections::TreeMap;
|
||||
|
||||
#[deriving(Eq, Encodable, Decodable)]
|
@ -26,12 +26,15 @@ Core encoding and decoding interfaces.
|
||||
#[cfg(test)]
|
||||
extern crate test;
|
||||
|
||||
extern crate collections;
|
||||
|
||||
pub use self::serialize::{Decoder, Encoder, Decodable, Encodable,
|
||||
DecoderHelpers, EncoderHelpers};
|
||||
DecoderHelpers, EncoderHelpers};
|
||||
|
||||
mod serialize;
|
||||
mod collection_impls;
|
||||
|
||||
pub mod base64;
|
||||
pub mod ebml;
|
||||
pub mod hex;
|
||||
|
||||
pub mod json;
|
||||
|
@ -1201,8 +1201,7 @@ pub enum InlinedItem {
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
extern crate extra;
|
||||
use self::extra::json;
|
||||
use serialize::json;
|
||||
use serialize;
|
||||
use codemap::*;
|
||||
use super::*;
|
||||
|
@ -283,10 +283,8 @@ pub fn maybe_aborted<T>(result: T, mut p: Parser) -> T {
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
extern crate extra;
|
||||
use self::extra::json;
|
||||
use super::*;
|
||||
use serialize::Encodable;
|
||||
use serialize::{json, Encodable};
|
||||
use std::io;
|
||||
use std::io::MemWriter;
|
||||
use std::str;
|
||||
|
@ -29,13 +29,12 @@ extern crate term;
|
||||
extern crate time;
|
||||
|
||||
use collections::TreeMap;
|
||||
use extra::json::ToJson;
|
||||
use extra::json;
|
||||
use extra::stats::Stats;
|
||||
use extra::stats;
|
||||
use time::precise_time_ns;
|
||||
use getopts::{OptGroup, optflag, optopt};
|
||||
use serialize::Decodable;
|
||||
use serialize::{json, Decodable};
|
||||
use serialize::json::ToJson;
|
||||
use term::Terminal;
|
||||
use term::color::{Color, RED, YELLOW, GREEN, CYAN};
|
||||
|
||||
|
@ -12,8 +12,8 @@
|
||||
|
||||
#[allow(unused_imports)];
|
||||
|
||||
extern crate extra;
|
||||
use extra::json::Object;
|
||||
extern crate serialize;
|
||||
use serialize::json::Object;
|
||||
|
||||
pub fn main() {
|
||||
println!("Hello world!");
|
||||
|
@ -11,10 +11,10 @@
|
||||
// except according to those terms.
|
||||
|
||||
extern crate collections;
|
||||
extern crate extra;
|
||||
extern crate serialize;
|
||||
|
||||
use extra::json;
|
||||
use collections::HashMap;
|
||||
use serialize::json;
|
||||
use std::option;
|
||||
|
||||
enum object {
|
||||
@ -25,7 +25,7 @@ enum object {
|
||||
fn lookup(table: ~json::Object, key: ~str, default: ~str) -> ~str
|
||||
{
|
||||
match table.find(&key) {
|
||||
option::Some(&extra::json::String(ref s)) => {
|
||||
option::Some(&json::String(ref s)) => {
|
||||
(*s).clone()
|
||||
}
|
||||
option::Some(value) => {
|
||||
@ -38,10 +38,10 @@ fn lookup(table: ~json::Object, key: ~str, default: ~str) -> ~str
|
||||
}
|
||||
}
|
||||
|
||||
fn add_interface(_store: int, managed_ip: ~str, data: extra::json::Json) -> (~str, object)
|
||||
fn add_interface(_store: int, managed_ip: ~str, data: json::Json) -> (~str, object)
|
||||
{
|
||||
match &data {
|
||||
&extra::json::Object(ref interface) => {
|
||||
&json::Object(ref interface) => {
|
||||
let name = lookup((*interface).clone(), ~"ifDescr", ~"");
|
||||
let label = format!("{}-{}", managed_ip, name);
|
||||
|
||||
@ -54,12 +54,12 @@ fn add_interface(_store: int, managed_ip: ~str, data: extra::json::Json) -> (~st
|
||||
}
|
||||
}
|
||||
|
||||
fn add_interfaces(store: int, managed_ip: ~str, device: HashMap<~str, extra::json::Json>)
|
||||
fn add_interfaces(store: int, managed_ip: ~str, device: HashMap<~str, json::Json>)
|
||||
-> ~[(~str, object)]
|
||||
{
|
||||
match device.get(&~"interfaces")
|
||||
{
|
||||
&extra::json::List(ref interfaces) =>
|
||||
&json::List(ref interfaces) =>
|
||||
{
|
||||
interfaces.map(|interface| {
|
||||
add_interface(store, managed_ip.clone(), (*interface).clone())
|
||||
|
@ -11,11 +11,9 @@
|
||||
|
||||
// ignore-fast
|
||||
|
||||
extern crate extra;
|
||||
extern crate serialize;
|
||||
|
||||
use extra::json;
|
||||
use serialize::Decodable;
|
||||
use serialize::{json, Decodable};
|
||||
|
||||
trait JD : Decodable<json::Decoder> { }
|
||||
|
||||
|
@ -13,10 +13,8 @@
|
||||
// Issue #4036: Test for an issue that arose around fixing up type inference
|
||||
// byproducts in vtable records.
|
||||
|
||||
extern crate extra;
|
||||
extern crate serialize;
|
||||
use extra::json;
|
||||
use serialize::Decodable;
|
||||
use serialize::{json, Decodable};
|
||||
|
||||
pub fn main() {
|
||||
let json = json::from_str("[1]").unwrap();
|
||||
|
Loading…
Reference in New Issue
Block a user