-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #312 from Mingun/refactoring
Minor improvement in code quality, optimisations and `deserialize_unit` fix
- Loading branch information
Showing
9 changed files
with
526 additions
and
355 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
//! Helper types for tests | ||
|
||
use serde::de::{self, Deserialize, Deserializer, Error}; | ||
use std::fmt; | ||
|
||
/// Wrapper around `Vec<u8>` that deserialized using `deserialize_byte_buf` | ||
/// instead of vector's generic `deserialize_seq` | ||
#[derive(Debug, PartialEq)] | ||
pub struct ByteBuf(pub Vec<u8>); | ||
|
||
impl<'de> Deserialize<'de> for ByteBuf { | ||
fn deserialize<D>(d: D) -> Result<Self, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
struct Visitor; | ||
|
||
impl<'de> de::Visitor<'de> for Visitor { | ||
type Value = ByteBuf; | ||
|
||
fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result { | ||
fmt.write_str("byte data") | ||
} | ||
|
||
fn visit_bytes<E: Error>(self, v: &[u8]) -> Result<Self::Value, E> { | ||
Ok(ByteBuf(v.to_vec())) | ||
} | ||
|
||
fn visit_byte_buf<E: Error>(self, v: Vec<u8>) -> Result<Self::Value, E> { | ||
Ok(ByteBuf(v)) | ||
} | ||
} | ||
|
||
Ok(d.deserialize_byte_buf(Visitor)?) | ||
} | ||
} | ||
|
||
/// Wrapper around `&[u8]` that deserialized using `deserialize_bytes` | ||
/// instead of vector's generic `deserialize_seq` | ||
#[derive(Debug, PartialEq)] | ||
pub struct Bytes<'de>(pub &'de [u8]); | ||
|
||
impl<'de> Deserialize<'de> for Bytes<'de> { | ||
fn deserialize<D>(d: D) -> Result<Self, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
struct Visitor; | ||
|
||
impl<'de> de::Visitor<'de> for Visitor { | ||
type Value = Bytes<'de>; | ||
|
||
fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result { | ||
fmt.write_str("borrowed bytes") | ||
} | ||
|
||
fn visit_borrowed_bytes<E: Error>(self, v: &'de [u8]) -> Result<Self::Value, E> { | ||
Ok(Bytes(v)) | ||
} | ||
} | ||
|
||
Ok(d.deserialize_bytes(Visitor)?) | ||
} | ||
} |
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
Oops, something went wrong.