Fixed String and Option conflicts for the latest Rust

This commit is contained in:
kvark 2014-09-29 21:50:24 -04:00
parent 119bb0f590
commit bfd1fb0ee9
4 changed files with 54 additions and 53 deletions
serde_macros/src
src

@ -17,7 +17,6 @@ use syntax::ast::{
ItemStruct, ItemStruct,
Expr, Expr,
MutMutable, MutMutable,
LitNil,
LitStr, LitStr,
StructField, StructField,
Variant, Variant,
@ -25,7 +24,7 @@ use syntax::ast::{
use syntax::ast; use syntax::ast;
use syntax::attr; use syntax::attr;
use syntax::codemap::Span; use syntax::codemap::Span;
use syntax::ext::base::{ExtCtxt, ItemDecorator}; use syntax::ext::base::{ExtCtxt, Decorator};
use syntax::ext::build::AstBuilder; use syntax::ext::build::AstBuilder;
use syntax::ext::deriving::generic::{ use syntax::ext::deriving::generic::{
EnumMatching, EnumMatching,
@ -61,11 +60,11 @@ use rustc::plugin::Registry;
pub fn plugin_registrar(reg: &mut Registry) { pub fn plugin_registrar(reg: &mut Registry) {
reg.register_syntax_extension( reg.register_syntax_extension(
token::intern("deriving_serializable"), token::intern("deriving_serializable"),
ItemDecorator(box expand_deriving_serializable)); Decorator(box expand_deriving_serializable));
reg.register_syntax_extension( reg.register_syntax_extension(
token::intern("deriving_deserializable"), token::intern("deriving_deserializable"),
ItemDecorator(box expand_deriving_deserializable)); Decorator(box expand_deriving_deserializable));
} }
fn expand_deriving_serializable(cx: &mut ExtCtxt, fn expand_deriving_serializable(cx: &mut ExtCtxt,
@ -137,7 +136,7 @@ fn serializable_substructure(cx: &ExtCtxt,
); );
let len = fields.len(); let len = fields.len();
let mut stmts: Vec<P<ast::Stmt>> = definition.fields.iter() let stmts: Vec<P<ast::Stmt>> = definition.fields.iter()
.zip(fields.iter()) .zip(fields.iter())
.enumerate() .enumerate()
.map(|(i, (def, &FieldInfo { name, ref self_, span, .. }))| { .map(|(i, (def, &FieldInfo { name, ref self_, span, .. }))| {
@ -178,7 +177,7 @@ fn serializable_substructure(cx: &ExtCtxt,
let stmts: Vec<P<ast::Stmt>> = definition.variants.iter() let stmts: Vec<P<ast::Stmt>> = definition.variants.iter()
.zip(fields.iter()) .zip(fields.iter())
.map(|(def, &FieldInfo { ref self_, span, .. })| { .map(|(def, &FieldInfo { ref self_, .. })| {
let _serial_name = find_serial_name(def.node.attrs.iter()); let _serial_name = find_serial_name(def.node.attrs.iter());
quote_stmt!( quote_stmt!(
cx, cx,
@ -336,7 +335,7 @@ fn deserialize_struct_from_struct(
fields: &StaticFields, fields: &StaticFields,
deserializer: P<ast::Expr> deserializer: P<ast::Expr>
) -> P<ast::Expr> { ) -> P<ast::Expr> {
let expect_struct_field = cx.ident_of("expect_struct_field"); //let expect_struct_field = cx.ident_of("expect_struct_field");
let call = deserializable_static_fields( let call = deserializable_static_fields(
cx, cx,
@ -375,7 +374,7 @@ fn deserialize_struct_from_map(
// Declare each field. // Declare each field.
let let_fields: Vec<P<ast::Stmt>> = fields.iter() let let_fields: Vec<P<ast::Stmt>> = fields.iter()
.map(|&(name, span)| { .map(|&(name, _)| {
quote_stmt!(cx, let mut $name = None) quote_stmt!(cx, let mut $name = None)
}) })
.collect(); .collect();
@ -494,7 +493,7 @@ fn deserialize_enum(
name, name,
serial_names.as_slice(), serial_names.as_slice(),
parts, parts,
|cx, span, _| { |cx, _, _| {
quote_expr!(cx, try!($deserializer.expect_enum_elt())) quote_expr!(cx, try!($deserializer.expect_enum_elt()))
} }
); );

@ -13,6 +13,8 @@ use std::gc::{GC, Gc};
use std::hash::Hash; use std::hash::Hash;
use std::num; use std::num;
use std::rc::Rc; use std::rc::Rc;
use std::option;
use std::string;
use std::sync::Arc; use std::sync::Arc;
#[deriving(Clone, PartialEq, Show)] #[deriving(Clone, PartialEq, Show)]
@ -33,7 +35,7 @@ pub enum Token {
F64(f64), F64(f64),
Char(char), Char(char),
Str(&'static str), Str(&'static str),
String(String), String(string::String),
Option(bool), Option(bool),
TupleStart(uint), TupleStart(uint),
@ -293,7 +295,7 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
} }
#[inline] #[inline]
fn expect_string(&mut self, token: Token) -> Result<String, E> { fn expect_string(&mut self, token: Token) -> Result<string::String, E> {
match token { match token {
Char(value) => Ok(value.to_string()), Char(value) => Ok(value.to_string()),
Str(value) => Ok(value.to_string()), Str(value) => Ok(value.to_string()),
@ -305,7 +307,7 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
#[inline] #[inline]
fn expect_option< fn expect_option<
T: Deserializable<Self, E> T: Deserializable<Self, E>
>(&mut self, token: Token) -> Result<Option<T>, E> { >(&mut self, token: Token) -> Result<option::Option<T>, E> {
match token { match token {
Option(false) => Ok(None), Option(false) => Ok(None),
Option(true) => { Option(true) => {
@ -429,7 +431,7 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
#[inline] #[inline]
fn expect_seq_elt_or_end< fn expect_seq_elt_or_end<
T: Deserializable<Self, E> T: Deserializable<Self, E>
>(&mut self) -> Result<Option<T>, E> { >(&mut self) -> Result<option::Option<T>, E> {
match try!(self.expect_token()) { match try!(self.expect_token()) {
End => Ok(None), End => Ok(None),
token => { token => {
@ -473,7 +475,7 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
fn expect_map_elt_or_end< fn expect_map_elt_or_end<
K: Deserializable<Self, E>, K: Deserializable<Self, E>,
V: Deserializable<Self, E> V: Deserializable<Self, E>
>(&mut self) -> Result<Option<(K, V)>, E> { >(&mut self) -> Result<option::Option<(K, V)>, E> {
match try!(self.expect_token()) { match try!(self.expect_token()) {
End => Ok(None), End => Ok(None),
token => { token => {
@ -513,7 +515,7 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
struct SeqDeserializer<'a, D: 'a, E> { struct SeqDeserializer<'a, D: 'a, E> {
d: &'a mut D, d: &'a mut D,
len: uint, len: uint,
err: Option<E>, err: option::Option<E>,
} }
impl< impl<
@ -523,7 +525,7 @@ impl<
T: Deserializable<D, E> T: Deserializable<D, E>
> Iterator<T> for SeqDeserializer<'a, D, E> { > Iterator<T> for SeqDeserializer<'a, D, E> {
#[inline] #[inline]
fn next(&mut self) -> Option<T> { fn next(&mut self) -> option::Option<T> {
match self.d.expect_seq_elt_or_end() { match self.d.expect_seq_elt_or_end() {
Ok(next) => next, Ok(next) => next,
Err(err) => { Err(err) => {
@ -534,7 +536,7 @@ impl<
} }
#[inline] #[inline]
fn size_hint(&self) -> (uint, Option<uint>) { fn size_hint(&self) -> (uint, option::Option<uint>) {
(self.len, Some(self.len)) (self.len, Some(self.len))
} }
} }
@ -544,7 +546,7 @@ impl<
struct MapDeserializer<'a, D:'a, E> { struct MapDeserializer<'a, D:'a, E> {
d: &'a mut D, d: &'a mut D,
len: uint, len: uint,
err: Option<E>, err: option::Option<E>,
} }
impl< impl<
@ -555,7 +557,7 @@ impl<
V: Deserializable<D, E> V: Deserializable<D, E>
> Iterator<(K, V)> for MapDeserializer<'a, D, E> { > Iterator<(K, V)> for MapDeserializer<'a, D, E> {
#[inline] #[inline]
fn next(&mut self) -> Option<(K, V)> { fn next(&mut self) -> option::Option<(K, V)> {
match self.d.expect_map_elt_or_end() { match self.d.expect_map_elt_or_end() {
Ok(next) => next, Ok(next) => next,
Err(err) => { Err(err) => {
@ -566,7 +568,7 @@ impl<
} }
#[inline] #[inline]
fn size_hint(&self) -> (uint, Option<uint>) { fn size_hint(&self) -> (uint, option::Option<uint>) {
(self.len, Some(self.len)) (self.len, Some(self.len))
} }
} }
@ -611,7 +613,7 @@ impl_deserializable!(f32, expect_num)
impl_deserializable!(f64, expect_num) impl_deserializable!(f64, expect_num)
impl_deserializable!(char, expect_char) impl_deserializable!(char, expect_char)
impl_deserializable!(&'static str, expect_str) impl_deserializable!(&'static str, expect_str)
impl_deserializable!(String, expect_string) impl_deserializable!(string::String, expect_string)
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
@ -665,9 +667,9 @@ impl<
D: Deserializer<E>, D: Deserializer<E>,
E, E,
T: Deserializable<D ,E> T: Deserializable<D ,E>
> Deserializable<D, E> for Option<T> { > Deserializable<D, E> for option::Option<T> {
#[inline] #[inline]
fn deserialize_token(d: &mut D, token: Token) -> Result<Option<T>, E> { fn deserialize_token(d: &mut D, token: Token) -> Result<option::Option<T>, E> {
d.expect_option(token) d.expect_option(token)
} }
} }

@ -11,7 +11,7 @@
use std::collections::TreeMap; use std::collections::TreeMap;
use std::str::StrAllocating; use std::str::StrAllocating;
use super::{Json, List, Object, ToJson}; use super::{Json, JsonObject, List, Object, ToJson};
pub struct ListBuilder { pub struct ListBuilder {
list: Vec<Json>, list: Vec<Json>,
@ -44,7 +44,7 @@ impl ListBuilder {
} }
pub struct ObjectBuilder { pub struct ObjectBuilder {
object: Object, object: JsonObject,
} }
impl ObjectBuilder { impl ObjectBuilder {

@ -274,7 +274,7 @@ use std::num::{FPNaN, FPInfinite};
use std::num; use std::num;
use std::str::ScalarValue; use std::str::ScalarValue;
use std::str; use std::str;
use std::string::String; use std::string;
use std::vec::Vec; use std::vec::Vec;
use std::vec; use std::vec;
@ -291,13 +291,13 @@ pub enum Json {
Boolean(bool), Boolean(bool),
Integer(i64), Integer(i64),
Floating(f64), Floating(f64),
String(String), String(string::String),
List(List), List(JsonList),
Object(Object), Object(JsonObject),
} }
pub type List = Vec<Json>; pub type JsonList = Vec<Json>;
pub type Object = TreeMap<String, Json>; pub type JsonObject = TreeMap<string::String, Json>;
impl Json { impl Json {
/// Serializes a json value into an io::writer. Uses a single line. /// Serializes a json value into an io::writer. Uses a single line.
@ -314,7 +314,7 @@ impl Json {
} }
/// Serializes a json value into a string /// Serializes a json value into a string
pub fn to_pretty_string(&self) -> String { pub fn to_pretty_string(&self) -> string::String {
let mut wr = MemWriter::new(); let mut wr = MemWriter::new();
self.to_pretty_writer(wr.by_ref()).unwrap(); self.to_pretty_writer(wr.by_ref()).unwrap();
str::from_utf8(wr.unwrap().as_slice()).unwrap().to_string() str::from_utf8(wr.unwrap().as_slice()).unwrap().to_string()
@ -322,7 +322,7 @@ impl Json {
/// If the Json value is an Object, returns the value associated with the provided key. /// If the Json value is an Object, returns the value associated with the provided key.
/// Otherwise, returns None. /// Otherwise, returns None.
pub fn find<'a>(&'a self, key: &String) -> Option<&'a Json>{ pub fn find<'a>(&'a self, key: &string::String) -> Option<&'a Json>{
match self { match self {
&Object(ref map) => map.find(key), &Object(ref map) => map.find(key),
_ => None _ => None
@ -332,7 +332,7 @@ impl Json {
/// Attempts to get a nested Json Object for each key in `keys`. /// Attempts to get a nested Json Object for each key in `keys`.
/// If any key is found not to exist, find_path will return None. /// If any key is found not to exist, find_path will return None.
/// Otherwise, it will return the Json value associated with the final key. /// Otherwise, it will return the Json value associated with the final key.
pub fn find_path<'a>(&'a self, keys: &[&String]) -> Option<&'a Json>{ pub fn find_path<'a>(&'a self, keys: &[&string::String]) -> Option<&'a Json>{
let mut target = self; let mut target = self;
for key in keys.iter() { for key in keys.iter() {
match target.find(*key) { match target.find(*key) {
@ -346,7 +346,7 @@ impl Json {
/// If the Json value is an Object, performs a depth-first search until /// If the Json value is an Object, performs a depth-first search until
/// a value associated with the provided key is found. If no value is found /// a value associated with the provided key is found. If no value is found
/// or the Json value is not an Object, returns None. /// or the Json value is not an Object, returns None.
pub fn search<'a>(&'a self, key: &String) -> Option<&'a Json> { pub fn search<'a>(&'a self, key: &string::String) -> Option<&'a Json> {
match self { match self {
&Object(ref map) => { &Object(ref map) => {
match map.find(key) { match map.find(key) {
@ -374,7 +374,7 @@ impl Json {
/// If the Json value is an Object, returns the associated TreeMap. /// If the Json value is an Object, returns the associated TreeMap.
/// Returns None otherwise. /// Returns None otherwise.
pub fn as_object<'a>(&'a self) -> Option<&'a Object> { pub fn as_object<'a>(&'a self) -> Option<&'a JsonObject> {
match *self { match *self {
Object(ref map) => Some(map), Object(ref map) => Some(map),
_ => None _ => None
@ -388,7 +388,7 @@ impl Json {
/// If the Json value is a List, returns the associated vector. /// If the Json value is a List, returns the associated vector.
/// Returns None otherwise. /// Returns None otherwise.
pub fn as_list<'a>(&'a self) -> Option<&'a List> { pub fn as_list<'a>(&'a self) -> Option<&'a JsonList> {
match *self { match *self {
List(ref list) => Some(list), List(ref list) => Some(list),
_ => None _ => None
@ -564,7 +564,7 @@ impl<D: de::Deserializer<E>, E> de::Deserializable<D, E> for Json {
enum JsonDeserializerState { enum JsonDeserializerState {
JsonDeserializerValueState(Json), JsonDeserializerValueState(Json),
JsonDeserializerListState(vec::MoveItems<Json>), JsonDeserializerListState(vec::MoveItems<Json>),
JsonDeserializerObjectState(treemap::MoveEntries<String, Json>), JsonDeserializerObjectState(treemap::MoveEntries<string::String, Json>),
JsonDeserializerEndState, JsonDeserializerEndState,
} }
@ -595,12 +595,12 @@ impl Iterator<Result<de::Token, ParserError>> for JsonDeserializer {
String(x) => de::String(x), String(x) => de::String(x),
List(x) => { List(x) => {
let len = x.len(); let len = x.len();
self.stack.push(JsonDeserializerListState(x.move_iter())); self.stack.push(JsonDeserializerListState(x.into_iter()));
de::SeqStart(len) de::SeqStart(len)
} }
Object(x) => { Object(x) => {
let len = x.len(); let len = x.len();
self.stack.push(JsonDeserializerObjectState(x.move_iter())); self.stack.push(JsonDeserializerObjectState(x.into_iter()));
de::MapStart(len) de::MapStart(len)
} }
}; };
@ -716,7 +716,7 @@ impl de::Deserializer<ParserError> for JsonDeserializer {
self.stack.push(JsonDeserializerEndState); self.stack.push(JsonDeserializerEndState);
for field in fields.move_iter().rev() { for field in fields.into_iter().rev() {
self.stack.push(JsonDeserializerValueState(field)); self.stack.push(JsonDeserializerValueState(field));
} }
@ -786,9 +786,9 @@ pub enum ParserError {
/// msg, line, col /// msg, line, col
SyntaxError(ErrorCode, uint, uint), SyntaxError(ErrorCode, uint, uint),
IoError(io::IoErrorKind, &'static str), IoError(io::IoErrorKind, &'static str),
ExpectedError(String, String), ExpectedError(string::String, string::String),
MissingFieldError(String), MissingFieldError(string::String),
UnknownVariantError(String), UnknownVariantError(string::String),
} }
// Builder and Parser have the same errors. // Builder and Parser have the same errors.
@ -1422,9 +1422,9 @@ pub fn to_vec<
#[inline] #[inline]
pub fn to_string< pub fn to_string<
T: ser::Serializable<Serializer<MemWriter>, io::IoError> T: ser::Serializable<Serializer<MemWriter>, io::IoError>
>(value: &T) -> Result<String, Vec<u8>> { >(value: &T) -> Result<string::String, Vec<u8>> {
let buf = to_vec(value); let buf = to_vec(value);
String::from_utf8(buf) string::String::from_utf8(buf)
} }
/// Encode the specified struct into a json `[u8]` buffer. /// Encode the specified struct into a json `[u8]` buffer.
@ -1440,9 +1440,9 @@ pub fn to_pretty_vec<
/// Encode the specified struct into a json `String` buffer. /// Encode the specified struct into a json `String` buffer.
pub fn to_pretty_string< pub fn to_pretty_string<
T: ser::Serializable<PrettySerializer<MemWriter>, io::IoError> T: ser::Serializable<PrettySerializer<MemWriter>, io::IoError>
>(value: &T) -> Result<String, Vec<u8>> { >(value: &T) -> Result<string::String, Vec<u8>> {
let buf = to_pretty_vec(value); let buf = to_pretty_vec(value);
String::from_utf8(buf) string::String::from_utf8(buf)
} }
/* /*
@ -1862,9 +1862,9 @@ impl<Iter: Iterator<char>> Parser<Iter> {
Ok(n) Ok(n)
} }
fn parse_string(&mut self) -> Result<String, ParserError> { fn parse_string(&mut self) -> Result<string::String, ParserError> {
let mut escape = false; let mut escape = false;
let mut res = String::new(); let mut res = string::String::new();
loop { loop {
self.bump(); self.bump();
@ -2260,7 +2260,7 @@ impl<'a> ToJson for &'a str {
fn to_json(&self) -> Json { String(self.to_string()) } fn to_json(&self) -> Json { String(self.to_string()) }
} }
impl ToJson for String { impl ToJson for string::String {
fn to_json(&self) -> Json { String((*self).clone()) } fn to_json(&self) -> Json { String((*self).clone()) }
} }
@ -2306,7 +2306,7 @@ impl<A:ToJson> ToJson for Vec<A> {
fn to_json(&self) -> Json { List(self.iter().map(|elt| elt.to_json()).collect()) } fn to_json(&self) -> Json { List(self.iter().map(|elt| elt.to_json()).collect()) }
} }
impl<A:ToJson> ToJson for TreeMap<String, A> { impl<A:ToJson> ToJson for TreeMap<string::String, A> {
fn to_json(&self) -> Json { fn to_json(&self) -> Json {
let mut d = TreeMap::new(); let mut d = TreeMap::new();
for (key, value) in self.iter() { for (key, value) in self.iter() {
@ -2316,7 +2316,7 @@ impl<A:ToJson> ToJson for TreeMap<String, A> {
} }
} }
impl<A:ToJson> ToJson for HashMap<String, A> { impl<A:ToJson> ToJson for HashMap<string::String, A> {
fn to_json(&self) -> Json { fn to_json(&self) -> Json {
let mut d = TreeMap::new(); let mut d = TreeMap::new();
for (key, value) in self.iter() { for (key, value) in self.iter() {