Standardize on u32 as the type of a variant index
This commit is contained in:
parent
a38b24136b
commit
52e93150e6
@ -66,13 +66,13 @@ macro_rules! __serialize_unimplemented_helper {
|
||||
__serialize_unimplemented_method!(serialize_unit_struct(&str) -> Ok);
|
||||
};
|
||||
(unit_variant) => {
|
||||
__serialize_unimplemented_method!(serialize_unit_variant(&str, usize, &str) -> Ok);
|
||||
__serialize_unimplemented_method!(serialize_unit_variant(&str, u32, &str) -> Ok);
|
||||
};
|
||||
(newtype_struct) => {
|
||||
__serialize_unimplemented_method!(serialize_newtype_struct<T>(&str, &T) -> Ok);
|
||||
};
|
||||
(newtype_variant) => {
|
||||
__serialize_unimplemented_method!(serialize_newtype_variant<T>(&str, usize, &str, &T) -> Ok);
|
||||
__serialize_unimplemented_method!(serialize_newtype_variant<T>(&str, u32, &str, &T) -> Ok);
|
||||
};
|
||||
(seq) => {
|
||||
type SerializeSeq = $crate::ser::Impossible<Self::Ok, Self::Error>;
|
||||
@ -91,7 +91,7 @@ macro_rules! __serialize_unimplemented_helper {
|
||||
};
|
||||
(tuple_variant) => {
|
||||
type SerializeTupleVariant = $crate::ser::Impossible<Self::Ok, Self::Error>;
|
||||
__serialize_unimplemented_method!(serialize_tuple_variant(&str, usize, &str, usize) -> SerializeTupleVariant);
|
||||
__serialize_unimplemented_method!(serialize_tuple_variant(&str, u32, &str, usize) -> SerializeTupleVariant);
|
||||
};
|
||||
(map) => {
|
||||
type SerializeMap = $crate::ser::Impossible<Self::Ok, Self::Error>;
|
||||
@ -103,7 +103,7 @@ macro_rules! __serialize_unimplemented_helper {
|
||||
};
|
||||
(struct_variant) => {
|
||||
type SerializeStructVariant = $crate::ser::Impossible<Self::Ok, Self::Error>;
|
||||
__serialize_unimplemented_method!(serialize_struct_variant(&str, usize, &str, usize) -> SerializeStructVariant);
|
||||
__serialize_unimplemented_method!(serialize_struct_variant(&str, u32, &str, usize) -> SerializeStructVariant);
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -184,7 +184,7 @@ impl<S> Serializer for TaggedSerializer<S>
|
||||
|
||||
fn serialize_unit_variant(self,
|
||||
_: &'static str,
|
||||
_: usize,
|
||||
_: u32,
|
||||
inner_variant: &'static str)
|
||||
-> Result<Self::Ok, Self::Error> {
|
||||
let mut map = try!(self.delegate.serialize_map(Some(2)));
|
||||
@ -204,7 +204,7 @@ impl<S> Serializer for TaggedSerializer<S>
|
||||
|
||||
fn serialize_newtype_variant<T: ?Sized>(self,
|
||||
_: &'static str,
|
||||
_: usize,
|
||||
_: u32,
|
||||
inner_variant: &'static str,
|
||||
inner_value: &T)
|
||||
-> Result<Self::Ok, Self::Error>
|
||||
@ -238,7 +238,7 @@ impl<S> Serializer for TaggedSerializer<S>
|
||||
#[cfg(not(any(feature = "std", feature = "collections")))]
|
||||
fn serialize_tuple_variant(self,
|
||||
_: &'static str,
|
||||
_: usize,
|
||||
_: u32,
|
||||
_: &'static str,
|
||||
_: usize)
|
||||
-> Result<Self::SerializeTupleVariant, Self::Error> {
|
||||
@ -250,7 +250,7 @@ impl<S> Serializer for TaggedSerializer<S>
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
fn serialize_tuple_variant(self,
|
||||
_: &'static str,
|
||||
_: usize,
|
||||
_: u32,
|
||||
inner_variant: &'static str,
|
||||
len: usize)
|
||||
-> Result<Self::SerializeTupleVariant, Self::Error> {
|
||||
@ -278,7 +278,7 @@ impl<S> Serializer for TaggedSerializer<S>
|
||||
#[cfg(not(any(feature = "std", feature = "collections")))]
|
||||
fn serialize_struct_variant(self,
|
||||
_: &'static str,
|
||||
_: usize,
|
||||
_: u32,
|
||||
_: &'static str,
|
||||
_: usize)
|
||||
-> Result<Self::SerializeStructVariant, Self::Error> {
|
||||
@ -290,7 +290,7 @@ impl<S> Serializer for TaggedSerializer<S>
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
fn serialize_struct_variant(self,
|
||||
_: &'static str,
|
||||
_: usize,
|
||||
_: u32,
|
||||
inner_variant: &'static str,
|
||||
len: usize)
|
||||
-> Result<Self::SerializeStructVariant, Self::Error> {
|
||||
@ -444,18 +444,18 @@ mod content {
|
||||
|
||||
Unit,
|
||||
UnitStruct(&'static str),
|
||||
UnitVariant(&'static str, usize, &'static str),
|
||||
UnitVariant(&'static str, u32, &'static str),
|
||||
NewtypeStruct(&'static str, Box<Content>),
|
||||
NewtypeVariant(&'static str, usize, &'static str, Box<Content>),
|
||||
NewtypeVariant(&'static str, u32, &'static str, Box<Content>),
|
||||
|
||||
Seq(Vec<Content>),
|
||||
SeqFixedSize(Vec<Content>),
|
||||
Tuple(Vec<Content>),
|
||||
TupleStruct(&'static str, Vec<Content>),
|
||||
TupleVariant(&'static str, usize, &'static str, Vec<Content>),
|
||||
TupleVariant(&'static str, u32, &'static str, Vec<Content>),
|
||||
Map(Vec<(Content, Content)>),
|
||||
Struct(&'static str, Vec<(&'static str, Content)>),
|
||||
StructVariant(&'static str, usize, &'static str, Vec<(&'static str, Content)>),
|
||||
StructVariant(&'static str, u32, &'static str, Vec<(&'static str, Content)>),
|
||||
}
|
||||
|
||||
impl Serialize for Content {
|
||||
@ -645,7 +645,7 @@ mod content {
|
||||
|
||||
fn serialize_unit_variant(self,
|
||||
name: &'static str,
|
||||
variant_index: usize,
|
||||
variant_index: u32,
|
||||
variant: &'static str)
|
||||
-> Result<Content, E> {
|
||||
Ok(Content::UnitVariant(name, variant_index, variant))
|
||||
@ -660,7 +660,7 @@ mod content {
|
||||
|
||||
fn serialize_newtype_variant<T: ?Sized + Serialize>(self,
|
||||
name: &'static str,
|
||||
variant_index: usize,
|
||||
variant_index: u32,
|
||||
variant: &'static str,
|
||||
value: &T)
|
||||
-> Result<Content, E> {
|
||||
@ -706,7 +706,7 @@ mod content {
|
||||
|
||||
fn serialize_tuple_variant(self,
|
||||
name: &'static str,
|
||||
variant_index: usize,
|
||||
variant_index: u32,
|
||||
variant: &'static str,
|
||||
len: usize)
|
||||
-> Result<Self::SerializeTupleVariant, E> {
|
||||
@ -737,7 +737,7 @@ mod content {
|
||||
|
||||
fn serialize_struct_variant(self,
|
||||
name: &'static str,
|
||||
variant_index: usize,
|
||||
variant_index: u32,
|
||||
variant: &'static str,
|
||||
len: usize)
|
||||
-> Result<Self::SerializeStructVariant, E> {
|
||||
@ -825,7 +825,7 @@ mod content {
|
||||
|
||||
struct SerializeTupleVariant<E> {
|
||||
name: &'static str,
|
||||
variant_index: usize,
|
||||
variant_index: u32,
|
||||
variant: &'static str,
|
||||
fields: Vec<Content>,
|
||||
error: PhantomData<E>,
|
||||
@ -916,7 +916,7 @@ mod content {
|
||||
|
||||
struct SerializeStructVariant<E> {
|
||||
name: &'static str,
|
||||
variant_index: usize,
|
||||
variant_index: u32,
|
||||
variant: &'static str,
|
||||
fields: Vec<(&'static str, Content)>,
|
||||
error: PhantomData<E>,
|
||||
|
@ -449,7 +449,7 @@ pub trait Serializer: Sized {
|
||||
/// ```
|
||||
fn serialize_unit_variant(self,
|
||||
name: &'static str,
|
||||
variant_index: usize,
|
||||
variant_index: u32,
|
||||
variant: &'static str)
|
||||
-> Result<Self::Ok, Self::Error>;
|
||||
|
||||
@ -504,7 +504,7 @@ pub trait Serializer: Sized {
|
||||
/// ```
|
||||
fn serialize_newtype_variant<T: ?Sized + Serialize>(self,
|
||||
name: &'static str,
|
||||
variant_index: usize,
|
||||
variant_index: u32,
|
||||
variant: &'static str,
|
||||
value: &T)
|
||||
-> Result<Self::Ok, Self::Error>;
|
||||
@ -686,7 +686,7 @@ pub trait Serializer: Sized {
|
||||
/// ```
|
||||
fn serialize_tuple_variant(self,
|
||||
name: &'static str,
|
||||
variant_index: usize,
|
||||
variant_index: u32,
|
||||
variant: &'static str,
|
||||
len: usize)
|
||||
-> Result<Self::SerializeTupleVariant, Self::Error>;
|
||||
@ -806,7 +806,7 @@ pub trait Serializer: Sized {
|
||||
/// ```
|
||||
fn serialize_struct_variant(self,
|
||||
name: &'static str,
|
||||
variant_index: usize,
|
||||
variant_index: u32,
|
||||
variant: &'static str,
|
||||
len: usize)
|
||||
-> Result<Self::SerializeStructVariant, Self::Error>;
|
||||
|
@ -6,6 +6,8 @@ use fragment::{Fragment, Stmts, Match};
|
||||
use internals::ast::{Body, Field, Item, Style, Variant};
|
||||
use internals::{self, attr};
|
||||
|
||||
use std::u32;
|
||||
|
||||
pub fn expand_derive_serialize(item: &syn::DeriveInput) -> Result<Tokens, String> {
|
||||
let ctxt = internals::Ctxt::new();
|
||||
let item = Item::from_ast(&ctxt, item);
|
||||
@ -149,6 +151,8 @@ fn serialize_struct(ident: &syn::Ident,
|
||||
fields: &[Field],
|
||||
item_attrs: &attr::Item)
|
||||
-> Fragment {
|
||||
assert!(fields.len() as u64 <= u32::MAX as u64);
|
||||
|
||||
let serialize_fields =
|
||||
serialize_struct_visitor(ident,
|
||||
fields,
|
||||
@ -187,13 +191,15 @@ fn serialize_item_enum(ident: &syn::Ident,
|
||||
variants: &[Variant],
|
||||
item_attrs: &attr::Item)
|
||||
-> Fragment {
|
||||
assert!(variants.len() as u64 <= u32::MAX as u64);
|
||||
|
||||
let arms: Vec<_> = variants.iter()
|
||||
.enumerate()
|
||||
.map(|(variant_index, variant)| {
|
||||
serialize_variant(ident,
|
||||
generics,
|
||||
variant,
|
||||
variant_index,
|
||||
variant_index as u32,
|
||||
item_attrs)
|
||||
})
|
||||
.collect();
|
||||
@ -208,7 +214,7 @@ fn serialize_item_enum(ident: &syn::Ident,
|
||||
fn serialize_variant(ident: &syn::Ident,
|
||||
generics: &syn::Generics,
|
||||
variant: &Variant,
|
||||
variant_index: usize,
|
||||
variant_index: u32,
|
||||
item_attrs: &attr::Item)
|
||||
-> Tokens {
|
||||
let variant_ident = variant.ident.clone();
|
||||
@ -292,7 +298,7 @@ fn serialize_variant(ident: &syn::Ident,
|
||||
fn serialize_externally_tagged_variant(ident: &syn::Ident,
|
||||
generics: &syn::Generics,
|
||||
variant: &Variant,
|
||||
variant_index: usize,
|
||||
variant_index: u32,
|
||||
item_attrs: &attr::Item)
|
||||
-> Fragment {
|
||||
let type_name = item_attrs.name().serialize_name();
|
||||
@ -538,7 +544,7 @@ fn serialize_untagged_variant(ident: &syn::Ident,
|
||||
enum TupleVariant {
|
||||
ExternallyTagged {
|
||||
type_name: String,
|
||||
variant_index: usize,
|
||||
variant_index: u32,
|
||||
variant_name: String,
|
||||
},
|
||||
Untagged,
|
||||
@ -589,7 +595,7 @@ fn serialize_tuple_variant(context: TupleVariant,
|
||||
|
||||
enum StructVariant<'a> {
|
||||
ExternallyTagged {
|
||||
variant_index: usize,
|
||||
variant_index: u32,
|
||||
variant_name: String,
|
||||
},
|
||||
InternallyTagged { tag: &'a str, variant_name: String },
|
||||
|
@ -146,7 +146,7 @@ impl<'s, 'a> ser::Serializer for &'s mut Serializer<'a> {
|
||||
|
||||
fn serialize_unit_variant(self,
|
||||
name: &'static str,
|
||||
_variant_index: usize,
|
||||
_variant_index: u32,
|
||||
variant: &'static str)
|
||||
-> Result<(), Error> {
|
||||
if self.tokens.first() == Some(&Token::Enum(name)) {
|
||||
@ -168,7 +168,7 @@ impl<'s, 'a> ser::Serializer for &'s mut Serializer<'a> {
|
||||
|
||||
fn serialize_newtype_variant<T: ?Sized>(self,
|
||||
name: &'static str,
|
||||
_variant_index: usize,
|
||||
_variant_index: u32,
|
||||
variant: &'static str,
|
||||
value: &T)
|
||||
-> Result<(), Error>
|
||||
@ -217,7 +217,7 @@ impl<'s, 'a> ser::Serializer for &'s mut Serializer<'a> {
|
||||
|
||||
fn serialize_tuple_variant(self,
|
||||
name: &'static str,
|
||||
_variant_index: usize,
|
||||
_variant_index: u32,
|
||||
variant: &'static str,
|
||||
len: usize)
|
||||
-> Result<Self, Error> {
|
||||
@ -237,7 +237,7 @@ impl<'s, 'a> ser::Serializer for &'s mut Serializer<'a> {
|
||||
|
||||
fn serialize_struct_variant(self,
|
||||
name: &'static str,
|
||||
_variant_index: usize,
|
||||
_variant_index: u32,
|
||||
variant: &'static str,
|
||||
len: usize)
|
||||
-> Result<Self, Error> {
|
||||
|
Loading…
x
Reference in New Issue
Block a user