-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
52 additions
and
2 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
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,41 @@ | ||
use crate::Uuid; | ||
use std::io::{Read, Result, Write}; | ||
use borsh::{BorshDeserialize, BorshSerialize}; | ||
|
||
impl BorshSerialize for Uuid { | ||
fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { | ||
BorshSerialize::serialize(&self.as_bytes(), writer) | ||
} | ||
} | ||
|
||
impl BorshDeserialize for Uuid { | ||
fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> { | ||
Ok(Self::from_bytes(BorshDeserialize::deserialize_reader( | ||
reader, | ||
)?)) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod borsh_tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_serialize() { | ||
let uuid_str = "f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4"; | ||
let uuid = Uuid::parse_str(uuid_str).unwrap(); | ||
let uuid_bytes = uuid.as_bytes().to_vec(); | ||
let borsh_bytes = uuid.try_to_vec().unwrap(); | ||
assert_eq!(uuid_bytes, borsh_bytes); | ||
} | ||
|
||
#[test] | ||
fn test_deserialize() { | ||
use std::string::ToString; | ||
let uuid_str = "f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4"; | ||
let uuid = Uuid::parse_str(uuid_str).unwrap(); | ||
let uuid_bytes = uuid.as_bytes().to_vec(); | ||
let deserialized = Uuid::try_from_slice(&uuid_bytes).unwrap().to_string(); | ||
assert_eq!(uuid_str, deserialized); | ||
} | ||
} |
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