-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added examples showing when tuples are valid
- Loading branch information
1 parent
013e04a
commit 19e9e92
Showing
3 changed files
with
107 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use std::io::Write; | ||
use serde::ser::{Error as SerError, Serialize, SerializeTuple}; | ||
|
||
use ser::Serializer; | ||
use ser::helpers; | ||
use error::Error; | ||
|
||
pub struct Tuple<'a, W: 'a + Write> { | ||
parent: &'a mut Serializer<W>, | ||
} | ||
|
||
|
||
impl<'w, W> Tuple<'w, W> | ||
where | ||
W: 'w + Write, | ||
{ | ||
pub fn new(parent: &'w mut Serializer<W>) -> Tuple<'w, W> { | ||
Tuple { parent } | ||
} | ||
} | ||
|
||
impl<'w, W> SerializeTuple for Tuple<'w, W> | ||
where | ||
W: 'w + Write | ||
{ | ||
type Ok = (); | ||
type Error = Error; | ||
|
||
fn serialize_element<T>(&mut self, value: &T) -> Result<Self::Ok, Self::Error> | ||
where | ||
T: Serialize + ?Sized | ||
{ | ||
if helpers::is_wrapped(value) { | ||
value.serialize(&mut *self.parent) | ||
} else { | ||
Err(SerError::custom( | ||
"Tuples can't contain primitive types. Please wrap primitives in a newtype.", | ||
)) | ||
} | ||
} | ||
|
||
fn end(self) -> Result<Self::Ok, Self::Error> { | ||
Ok(()) | ||
} | ||
} |