Remove lifetime from serde_test::Token

This commit is contained in:
David Tolnay 2017-04-05 00:17:50 -07:00
parent 92bc23e484
commit 5871fb9ce0
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
8 changed files with 95 additions and 81 deletions

View File

@ -8,7 +8,7 @@ use token::Token;
use std::fmt::Debug;
/// Runs both `assert_ser_tokens` and `assert_de_tokens`.
pub fn assert_tokens<'de, T>(value: &T, tokens: &[Token<'static>])
pub fn assert_tokens<'de, T>(value: &T, tokens: &[Token])
where T: Serialize + Deserialize<'de> + PartialEq + Debug
{
assert_ser_tokens(value, tokens);
@ -35,7 +35,7 @@ pub fn assert_ser_tokens_error<T>(value: &T, tokens: &[Token], error: Error)
}
/// Asserts that the given `tokens` deserialize into `value`.
pub fn assert_de_tokens<'de, T>(value: &T, tokens: &[Token<'static>])
pub fn assert_de_tokens<'de, T>(value: &T, tokens: &[Token])
where T: Deserialize<'de> + PartialEq + Debug
{
let mut de = Deserializer::new(tokens.to_vec().into_iter());
@ -45,7 +45,7 @@ pub fn assert_de_tokens<'de, T>(value: &T, tokens: &[Token<'static>])
}
/// Asserts that the given `tokens` yield `error` when deserializing.
pub fn assert_de_tokens_error<'de, T>(tokens: &[Token<'static>], error: Error)
pub fn assert_de_tokens_error<'de, T>(tokens: &[Token], error: Error)
where T: Deserialize<'de> + PartialEq + Debug
{
let mut de = Deserializer::new(tokens.to_vec().into_iter());

View File

@ -9,13 +9,13 @@ use token::Token;
/// A `Deserializer` that reads from a list of tokens.
pub struct Deserializer<I>
where I: Iterator<Item = Token<'static>>
where I: Iterator<Item = Token>
{
tokens: iter::Peekable<I>,
}
impl<I> Deserializer<I>
where I: Iterator<Item = Token<'static>>
where I: Iterator<Item = Token>
{
/// Creates the deserializer.
pub fn new(tokens: I) -> Deserializer<I> {
@ -23,7 +23,7 @@ impl<I> Deserializer<I>
}
/// Pulls the next token off of the deserializer, ignoring it.
pub fn next_token(&mut self) -> Option<Token<'static>> {
pub fn next_token(&mut self) -> Option<Token> {
self.tokens.next()
}
@ -43,8 +43,8 @@ impl<I> Deserializer<I>
fn visit_seq<'de, V>(&mut self,
len: Option<usize>,
sep: Token<'static>,
end: Token<'static>,
sep: Token,
end: Token,
visitor: V)
-> Result<V::Value, Error>
where V: Visitor<'de>
@ -61,8 +61,8 @@ impl<I> Deserializer<I>
fn visit_map<'de, V>(&mut self,
len: Option<usize>,
sep: Token<'static>,
end: Token<'static>,
sep: Token,
end: Token,
visitor: V)
-> Result<V::Value, Error>
where V: Visitor<'de>
@ -79,7 +79,7 @@ impl<I> Deserializer<I>
}
impl<'de, 'a, I> de::Deserializer<'de> for &'a mut Deserializer<I>
where I: Iterator<Item = Token<'static>>
where I: Iterator<Item = Token>
{
type Error = Error;
@ -106,10 +106,10 @@ impl<'de, 'a, I> de::Deserializer<'de> for &'a mut Deserializer<I>
Some(Token::Char(v)) => visitor.visit_char(v),
Some(Token::Str(v)) => visitor.visit_str(v),
Some(Token::BorrowedStr(v)) => visitor.visit_borrowed_str(v),
Some(Token::String(v)) => visitor.visit_string(v),
Some(Token::String(v)) => visitor.visit_string(v.to_owned()),
Some(Token::Bytes(v)) => visitor.visit_bytes(v),
Some(Token::BorrowedBytes(v)) => visitor.visit_borrowed_bytes(v),
Some(Token::ByteBuf(v)) => visitor.visit_byte_buf(v),
Some(Token::ByteBuf(v)) => visitor.visit_byte_buf(v.to_vec()),
Some(Token::Option(false)) => visitor.visit_none(),
Some(Token::Option(true)) => visitor.visit_some(self),
Some(Token::Unit) => visitor.visit_unit(),
@ -354,16 +354,16 @@ impl<'de, 'a, I> de::Deserializer<'de> for &'a mut Deserializer<I>
//////////////////////////////////////////////////////////////////////////
struct DeserializerSeqVisitor<'a, I: 'a>
where I: Iterator<Item = Token<'static>>
where I: Iterator<Item = Token>
{
de: &'a mut Deserializer<I>,
len: Option<usize>,
sep: Token<'static>,
end: Token<'static>,
sep: Token,
end: Token,
}
impl<'de, 'a, I> SeqVisitor<'de> for DeserializerSeqVisitor<'a, I>
where I: Iterator<Item = Token<'static>>
where I: Iterator<Item = Token>
{
type Error = Error;
@ -392,16 +392,16 @@ impl<'de, 'a, I> SeqVisitor<'de> for DeserializerSeqVisitor<'a, I>
//////////////////////////////////////////////////////////////////////////
struct DeserializerMapVisitor<'a, I: 'a>
where I: Iterator<Item = Token<'static>>
where I: Iterator<Item = Token>
{
de: &'a mut Deserializer<I>,
len: Option<usize>,
sep: Token<'static>,
end: Token<'static>,
sep: Token,
end: Token,
}
impl<'de, 'a, I> MapVisitor<'de> for DeserializerMapVisitor<'a, I>
where I: Iterator<Item = Token<'static>>
where I: Iterator<Item = Token>
{
type Error = Error;
@ -436,13 +436,13 @@ impl<'de, 'a, I> MapVisitor<'de> for DeserializerMapVisitor<'a, I>
//////////////////////////////////////////////////////////////////////////
struct DeserializerEnumVisitor<'a, I: 'a>
where I: Iterator<Item = Token<'static>>
where I: Iterator<Item = Token>
{
de: &'a mut Deserializer<I>,
}
impl<'de, 'a, I> EnumVisitor<'de> for DeserializerEnumVisitor<'a, I>
where I: Iterator<Item = Token<'static>>
where I: Iterator<Item = Token>
{
type Error = Error;
type Variant = Self;
@ -469,7 +469,7 @@ impl<'de, 'a, I> EnumVisitor<'de> for DeserializerEnumVisitor<'a, I>
}
impl<'de, 'a, I> VariantVisitor<'de> for DeserializerEnumVisitor<'a, I>
where I: Iterator<Item = Token<'static>>
where I: Iterator<Item = Token>
{
type Error = Error;
@ -558,14 +558,14 @@ impl<'de, 'a, I> VariantVisitor<'de> for DeserializerEnumVisitor<'a, I>
//////////////////////////////////////////////////////////////////////////
struct EnumMapVisitor<'a, I: 'a>
where I: Iterator<Item = Token<'static>>
where I: Iterator<Item = Token>
{
de: &'a mut Deserializer<I>,
variant: Option<&'a str>,
}
impl<'a, I: 'a> EnumMapVisitor<'a, I>
where I: Iterator<Item = Token<'static>>
where I: Iterator<Item = Token>
{
fn new(de: &'a mut Deserializer<I>, variant: &'a str) -> Self {
EnumMapVisitor {
@ -576,7 +576,7 @@ impl<'a, I: 'a> EnumMapVisitor<'a, I>
}
impl<'de, 'a, I: 'a> MapVisitor<'de> for EnumMapVisitor<'a, I>
where I: Iterator<Item = Token<'static>>
where I: Iterator<Item = Token>
{
type Error = Error;

View File

@ -15,7 +15,7 @@ pub enum Error {
InvalidName(&'static str),
/// `Serialize` generated a token that didn't match the test.
UnexpectedToken(Token<'static>),
UnexpectedToken(Token),
/// The expected token list was too short.
EndOfTokens,

View File

@ -7,14 +7,14 @@ use token::Token;
/// A `Serializer` that ensures that a value serializes to a given list of tokens.
pub struct Serializer<'a, I>
where I: Iterator<Item = &'a Token<'a>>
where I: Iterator<Item = &'a Token>
{
tokens: I,
phantom: PhantomData<&'a Token<'a>>,
phantom: PhantomData<&'a Token>,
}
impl<'a, I> Serializer<'a, I>
where I: Iterator<Item = &'a Token<'a>>
where I: Iterator<Item = &'a Token>
{
/// Creates the serializer.
pub fn new(tokens: I) -> Serializer<'a, I> {
@ -25,13 +25,27 @@ impl<'a, I> Serializer<'a, I>
}
/// Pulls the next token off of the serializer, ignoring it.
pub fn next_token(&mut self) -> Option<&'a Token<'a>> {
pub fn next_token(&mut self) -> Option<&'a Token> {
self.tokens.next()
}
}
macro_rules! assert_next_token {
($self:ident, $expected:ident($arg:expr)) => {
match $self.tokens.next() {
Some(&Token::$expected(v)) if v == $arg => {}
Some(other) => {
panic!("expected {}({:?}) but serialized as {:?}", stringify!($expected), $arg, other);
}
None => {
panic!("expected {}({:?}) after end of serialized tokens", stringify!($expected), $arg);
}
}
}
}
impl<'s, 'a, I> ser::Serializer for &'s mut Serializer<'a, I>
where I: Iterator<Item = &'a Token<'a>>
where I: Iterator<Item = &'a Token>
{
type Ok = ();
type Error = Error;
@ -105,12 +119,12 @@ impl<'s, 'a, I> ser::Serializer for &'s mut Serializer<'a, I>
}
fn serialize_str(self, v: &str) -> Result<(), Error> {
assert_eq!(self.tokens.next(), Some(&Token::Str(v)));
assert_next_token!(self, Str(v));
Ok(())
}
fn serialize_bytes(self, value: &[u8]) -> Result<(), Self::Error> {
assert_eq!(self.tokens.next(), Some(&Token::Bytes(value)));
assert_next_token!(self, Bytes(value));
Ok(())
}
@ -119,15 +133,15 @@ impl<'s, 'a, I> ser::Serializer for &'s mut Serializer<'a, I>
Ok(())
}
fn serialize_unit_struct(self, name: &str) -> Result<(), Error> {
fn serialize_unit_struct(self, name: &'static str) -> Result<(), Error> {
assert_eq!(self.tokens.next(), Some(&Token::UnitStruct(name)));
Ok(())
}
fn serialize_unit_variant(self,
name: &str,
name: &'static str,
_variant_index: usize,
variant: &str)
variant: &'static str)
-> Result<(), Error> {
assert_eq!(self.tokens.next(), Some(&Token::EnumUnit(name, variant)));
Ok(())
@ -141,9 +155,9 @@ impl<'s, 'a, I> ser::Serializer for &'s mut Serializer<'a, I>
}
fn serialize_newtype_variant<T: ?Sized>(self,
name: &str,
name: &'static str,
_variant_index: usize,
variant: &str,
variant: &'static str,
value: &T)
-> Result<(), Error>
where T: Serialize
@ -186,9 +200,9 @@ impl<'s, 'a, I> ser::Serializer for &'s mut Serializer<'a, I>
}
fn serialize_tuple_variant(self,
name: &str,
name: &'static str,
_variant_index: usize,
variant: &str,
variant: &'static str,
len: usize)
-> Result<Self, Error> {
assert_eq!(self.tokens.next(),
@ -201,15 +215,15 @@ impl<'s, 'a, I> ser::Serializer for &'s mut Serializer<'a, I>
Ok(self)
}
fn serialize_struct(self, name: &str, len: usize) -> Result<Self, Error> {
fn serialize_struct(self, name: &'static str, len: usize) -> Result<Self, Error> {
assert_eq!(self.tokens.next(), Some(&Token::StructStart(name, len)));
Ok(self)
}
fn serialize_struct_variant(self,
name: &str,
name: &'static str,
_variant_index: usize,
variant: &str,
variant: &'static str,
len: usize)
-> Result<Self, Error> {
assert_eq!(self.tokens.next(),
@ -219,7 +233,7 @@ impl<'s, 'a, I> ser::Serializer for &'s mut Serializer<'a, I>
}
impl<'s, 'a, I> ser::SerializeSeq for &'s mut Serializer<'a, I>
where I: Iterator<Item = &'a Token<'a>>
where I: Iterator<Item = &'a Token>
{
type Ok = ();
type Error = Error;
@ -238,7 +252,7 @@ impl<'s, 'a, I> ser::SerializeSeq for &'s mut Serializer<'a, I>
}
impl<'s, 'a, I> ser::SerializeTuple for &'s mut Serializer<'a, I>
where I: Iterator<Item = &'a Token<'a>>
where I: Iterator<Item = &'a Token>
{
type Ok = ();
type Error = Error;
@ -257,7 +271,7 @@ impl<'s, 'a, I> ser::SerializeTuple for &'s mut Serializer<'a, I>
}
impl<'s, 'a, I> ser::SerializeTupleStruct for &'s mut Serializer<'a, I>
where I: Iterator<Item = &'a Token<'a>>
where I: Iterator<Item = &'a Token>
{
type Ok = ();
type Error = Error;
@ -276,7 +290,7 @@ impl<'s, 'a, I> ser::SerializeTupleStruct for &'s mut Serializer<'a, I>
}
impl<'s, 'a, I> ser::SerializeTupleVariant for &'s mut Serializer<'a, I>
where I: Iterator<Item = &'a Token<'a>>
where I: Iterator<Item = &'a Token>
{
type Ok = ();
type Error = Error;
@ -295,7 +309,7 @@ impl<'s, 'a, I> ser::SerializeTupleVariant for &'s mut Serializer<'a, I>
}
impl<'s, 'a, I> ser::SerializeMap for &'s mut Serializer<'a, I>
where I: Iterator<Item = &'a Token<'a>>
where I: Iterator<Item = &'a Token>
{
type Ok = ();
type Error = Error;
@ -320,7 +334,7 @@ impl<'s, 'a, I> ser::SerializeMap for &'s mut Serializer<'a, I>
}
impl<'s, 'a, I> ser::SerializeStruct for &'s mut Serializer<'a, I>
where I: Iterator<Item = &'a Token<'a>>
where I: Iterator<Item = &'a Token>
{
type Ok = ();
type Error = Error;
@ -343,7 +357,7 @@ impl<'s, 'a, I> ser::SerializeStruct for &'s mut Serializer<'a, I>
}
impl<'s, 'a, I> ser::SerializeStructVariant for &'s mut Serializer<'a, I>
where I: Iterator<Item = &'a Token<'a>>
where I: Iterator<Item = &'a Token>
{
type Ok = ();
type Error = Error;

View File

@ -1,5 +1,5 @@
#[derive(Clone, PartialEq, Debug)]
pub enum Token<'a> {
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum Token {
/// A serialized `bool`.
Bool(bool),
@ -37,22 +37,22 @@ pub enum Token<'a> {
Char(char),
/// A serialized `str`.
Str(&'a str),
Str(&'static str),
/// A borrowed `str`.
BorrowedStr(&'a str),
BorrowedStr(&'static str),
/// A serialized `String`.
String(String),
String(&'static str),
/// A serialized `[u8]`
Bytes(&'a [u8]),
Bytes(&'static [u8]),
/// A borrowed `[u8]`.
BorrowedBytes(&'a [u8]),
BorrowedBytes(&'static [u8]),
/// A serialized `ByteBuf`
ByteBuf(Vec<u8>),
ByteBuf(&'static [u8]),
/// The header to a serialized `Option<T>`.
///
@ -64,13 +64,13 @@ pub enum Token<'a> {
Unit,
/// A serialized unit struct of the given name.
UnitStruct(&'a str),
UnitStruct(&'static str),
/// The header to a serialized newtype struct of the given name.
///
/// Newtype structs are serialized with this header, followed by the value contained in the
/// newtype struct.
StructNewType(&'a str),
StructNewType(&'static str),
/// The header to an enum of the given name.
///
@ -79,19 +79,19 @@ pub enum Token<'a> {
/// `assert_tokens` will fail if this token is used.
///
/// TODO: Trash this.
EnumStart(&'a str),
EnumStart(&'static str),
/// A unit variant of an enum of the given name, of the given name.
///
/// The first string represents the name of the enum, and the second represents the name of the
/// variant.
EnumUnit(&'a str, &'a str),
EnumUnit(&'static str, &'static str),
/// The header to a newtype variant of an enum of the given name, of the given name.
///
/// The first string represents the name of the enum, and the second represents the name of the
/// variant. The value contained within this enum works the same as `StructNewType`.
EnumNewType(&'a str, &'a str),
EnumNewType(&'static str, &'static str),
/// The header to a sequence of the given length.
///
@ -123,7 +123,7 @@ pub enum Token<'a> {
TupleEnd,
/// The header to a tuple struct of the given name and length.
TupleStructStart(&'a str, usize),
TupleStructStart(&'static str, usize),
/// A separator, similar to `TupleSep`.
TupleStructSep,
@ -147,7 +147,7 @@ pub enum Token<'a> {
MapEnd,
/// The header of a struct of the given name and length, similar to `MapStart`.
StructStart(&'a str, usize),
StructStart(&'static str, usize),
/// A separator, similar to `MapSep`.
StructSep,
@ -156,7 +156,7 @@ pub enum Token<'a> {
StructEnd,
/// The header to a tuple variant of an enum of the given name, of the given name and length.
EnumSeqStart(&'a str, &'a str, usize),
EnumSeqStart(&'static str, &'static str, usize),
/// A separator, similar to `TupleSep`.
EnumSeqSep,
@ -166,7 +166,7 @@ pub enum Token<'a> {
/// The header of a struct variant of an enum of the given name, of the given name and length,
/// similar to `StructStart`.
EnumMapStart(&'a str, &'a str, usize),
EnumMapStart(&'static str, &'static str, usize),
/// A separator, similar to `StructSep`.
EnumMapSep,

View File

@ -28,7 +28,7 @@ fn test_borrowed_str() {
fn test_borrowed_str_from_string() {
assert_de_tokens_error::<&str>(
&[
Token::String("borrowed".to_owned()),
Token::String("borrowed"),
],
Error::Message("invalid type: string \"borrowed\", expected a borrowed string".to_owned()),
);
@ -68,7 +68,7 @@ fn test_borrowed_bytes() {
fn test_borrowed_bytes_from_bytebuf() {
assert_de_tokens_error::<&[u8]>(
&[
Token::ByteBuf(b"borrowed".to_vec()),
Token::ByteBuf(b"borrowed"),
],
Error::Message("invalid type: byte array, expected a borrowed byte array".to_owned()),
);

View File

@ -18,9 +18,9 @@ fn test_bytes() {
fn test_byte_buf() {
let empty = ByteBuf::new();
assert_tokens(&empty, &[Token::Bytes(b"")]);
assert_de_tokens(&empty, &[Token::ByteBuf(Vec::new())]);
assert_de_tokens(&empty, &[Token::ByteBuf(b"")]);
assert_de_tokens(&empty, &[Token::Str("")]);
assert_de_tokens(&empty, &[Token::String(String::new())]);
assert_de_tokens(&empty, &[Token::String("")]);
assert_de_tokens(&empty, &[
Token::SeqStart(None),
Token::SeqEnd,
@ -32,9 +32,9 @@ fn test_byte_buf() {
let buf = ByteBuf::from(vec![65, 66, 67]);
assert_tokens(&buf, &[Token::Bytes(b"ABC")]);
assert_de_tokens(&buf, &[Token::ByteBuf(vec![65, 66, 67])]);
assert_de_tokens(&buf, &[Token::ByteBuf(b"ABC")]);
assert_de_tokens(&buf, &[Token::Str("ABC")]);
assert_de_tokens(&buf, &[Token::String("ABC".to_owned())]);
assert_de_tokens(&buf, &[Token::String("ABC")]);
assert_de_tokens(&buf, &[
Token::SeqStart(None),
Token::SeqSep,

View File

@ -137,7 +137,7 @@ macro_rules! declare_error_tests {
}
}
fn assert_de_tokens_ignore(ignorable_tokens: &[Token<'static>]) {
fn assert_de_tokens_ignore(ignorable_tokens: &[Token]) {
#[derive(PartialEq, Debug, Deserialize)]
struct IgnoreBase {
a: i32,
@ -147,7 +147,7 @@ fn assert_de_tokens_ignore(ignorable_tokens: &[Token<'static>]) {
// Embed the tokens to be ignored in the normal token
// stream for an IgnoreBase type
let concated_tokens : Vec<Token<'static>> = vec![
let concated_tokens : Vec<Token> = vec![
Token::MapStart(Some(2)),
Token::MapSep,
Token::Str("a"),
@ -215,11 +215,11 @@ declare_tests! {
test_char {
'a' => &[Token::Char('a')],
'a' => &[Token::Str("a")],
'a' => &[Token::String("a".to_owned())],
'a' => &[Token::String("a")],
}
test_string {
"abc".to_owned() => &[Token::Str("abc")],
"abc".to_owned() => &[Token::String("abc".to_owned())],
"abc".to_owned() => &[Token::String("abc")],
"a".to_owned() => &[Token::Char('a')],
}
test_option {
@ -767,7 +767,7 @@ declare_tests! {
Token::StructSep,
Token::Str("b"),
Token::String("overwritten".to_string()),
Token::String("overwritten"),
Token::StructEnd,
],
StructDefault { a: 100, b: "default".to_string() } => &[
@ -903,7 +903,7 @@ declare_tests! {
}
test_path_buf {
PathBuf::from("/usr/local/lib") => &[
Token::String("/usr/local/lib".to_owned()),
Token::String("/usr/local/lib"),
],
}
test_cstring {