-
Notifications
You must be signed in to change notification settings - Fork 193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Serialises UUID as [u8:16] #331
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7d1741c
Fix #329
Redrield 5d08a0e
Fix CI error from unsupported syntax
Redrield 94cbb49
Fix formatting for CI
Redrield 82a3049
Actually fix it
Redrield b122c3a
That should actually finally fix it.
Redrield 56978b3
Rewrite dense serialization using serde attributes
Redrield 635dd6e
Run cargo fmt to please CI gods
Redrield b1b2efe
Get rid of debug messages
Redrield File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
//! Module for use with `#[serde(with = "...")]` to serialize a [`Uuid`] | ||
//! as a `[u8; 16] | ||
//! | ||
//! [`Uuid`]: ../../struct.Uuid.html | ||
|
||
use serde::de::{self, Error, SeqAccess}; | ||
use serde::ser::SerializeTuple; | ||
use serde::{Deserializer, Serializer}; | ||
|
||
use prelude::*; | ||
|
||
use std::fmt; | ||
|
||
/// Serializer for a [`Uuid`] into a `[u8; 16]` | ||
/// | ||
/// [`Uuid`]: ../../struct.Uuid.html | ||
pub fn serialize<S: Serializer>( | ||
u: &Uuid, | ||
serializer: S, | ||
) -> Result<S::Ok, S::Error> { | ||
println!("sereeeeeeeeealize"); | ||
let mut seq = serializer.serialize_tuple(16)?; | ||
|
||
for byte in u.as_bytes() { | ||
seq.serialize_element(byte)?; | ||
} | ||
|
||
seq.end() | ||
} | ||
|
||
/// Deserializer from a `[u8; 16]` into a [`Uuid`] | ||
/// | ||
/// [`Uuid`]: ../../struct.Uuid.html | ||
pub fn deserialize<'de, D: Deserializer<'de>>( | ||
deserializer: D, | ||
) -> Result<Uuid, D::Error> { | ||
println!("desereeeeeeeeialize"); | ||
struct DenseUuidBytesVisitor; | ||
Dylan-DPC-zz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
impl<'vi> de::Visitor<'vi> for DenseUuidBytesVisitor { | ||
type Value = Uuid; | ||
|
||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | ||
write!(formatter, "tuple") | ||
} | ||
|
||
fn visit_seq<A: SeqAccess<'vi>>( | ||
self, | ||
mut seq: A, | ||
) -> Result<Self::Value, A::Error> { | ||
if seq.size_hint() == Some(16) { | ||
let mut buf = [0; 16]; | ||
for i in 0..16 { | ||
buf[i] = seq.next_element().unwrap().unwrap() | ||
} | ||
Ok(Uuid::from_bytes(buf)) | ||
} else { | ||
Err(Error::custom("Uuid must be 16 bytes long")) | ||
} | ||
} | ||
} | ||
|
||
deserializer.deserialize_tuple(16, DenseUuidBytesVisitor) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use serde_test; | ||
|
||
use prelude::*; | ||
|
||
#[derive(Serialize, Debug, Deserialize, PartialEq)] | ||
struct UuidContainer { | ||
#[serde(with = "super")] | ||
u: Uuid, | ||
} | ||
|
||
#[test] | ||
fn test_serialize_compact() { | ||
use serde_test::Configure; | ||
|
||
let uuid_bytes = b"F9168C5E-CEB2-4F"; | ||
let container = UuidContainer { | ||
u: Uuid::from_slice(uuid_bytes).unwrap(), | ||
}; | ||
|
||
// more complex because of the struct wrapping the actual UUID | ||
// serialization | ||
serde_test::assert_tokens( | ||
&container.compact(), | ||
&[ | ||
serde_test::Token::Struct { | ||
name: "UuidContainer", | ||
len: 1, | ||
}, | ||
serde_test::Token::Str("u"), | ||
serde_test::Token::Tuple { len: 16 }, | ||
serde_test::Token::U8(uuid_bytes[0]), | ||
serde_test::Token::U8(uuid_bytes[1]), | ||
serde_test::Token::U8(uuid_bytes[2]), | ||
serde_test::Token::U8(uuid_bytes[3]), | ||
serde_test::Token::U8(uuid_bytes[4]), | ||
serde_test::Token::U8(uuid_bytes[5]), | ||
serde_test::Token::U8(uuid_bytes[6]), | ||
serde_test::Token::U8(uuid_bytes[7]), | ||
serde_test::Token::U8(uuid_bytes[8]), | ||
serde_test::Token::U8(uuid_bytes[9]), | ||
serde_test::Token::U8(uuid_bytes[10]), | ||
serde_test::Token::U8(uuid_bytes[11]), | ||
serde_test::Token::U8(uuid_bytes[12]), | ||
serde_test::Token::U8(uuid_bytes[13]), | ||
serde_test::Token::U8(uuid_bytes[14]), | ||
serde_test::Token::U8(uuid_bytes[15]), | ||
serde_test::Token::TupleEnd, | ||
serde_test::Token::StructEnd, | ||
], | ||
) | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
uhmm?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Debug messages, i'll take those out, whoops!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a // TODO just to be sure :)
we have a bot that marks TODO comments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wdym? Removed them in b1b2efe