Skip to content

Commit

Permalink
rust: add binary_protocol::deserialize Bytes thrift entry point
Browse files Browse the repository at this point in the history
Summary:
Same as previous change to compact_protocol, but for binary_protocol

This change updates binary_protocol::deserialize in terms of a new type DeserializeSource. DeserializeSource has conversions that it can take ownership of Bytes and items that are Into<Bytes>, whilst still being able to copy data from types that are AsRef<[u8]>.

Reviewed By: markbt

Differential Revision: D26687829

fbshipit-source-id: fca1d4f9389f07971d54186d229e487e5173caee
  • Loading branch information
ahornby authored and facebook-github-bot committed Mar 3, 2021
1 parent 404d168 commit e211763
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/binary_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

use crate::binary_type::CopyFromBuf;
use crate::bufext::{BufExt, BufMutExt};
use crate::bufext::{BufExt, BufMutExt, DeserializeSource};
use crate::deserialize::Deserialize;
use crate::errors::ProtocolError;
use crate::framing::Framing;
Expand All @@ -27,7 +27,7 @@ use crate::Result;
use bufsize::SizeCounter;
use bytes::{Bytes, BytesMut};
use ghost::phantom;
use std::{convert::TryFrom, io::Cursor};
use std::convert::TryFrom;

pub const BINARY_VERSION_MASK: u32 = 0xffff_0000;
pub const BINARY_VERSION_1: u32 = 0x8001_0000;
Expand Down Expand Up @@ -482,12 +482,13 @@ where
}

/// Deserialize a Thrift blob using the binary protocol.
pub fn deserialize<T, B>(b: B) -> Result<T>
pub fn deserialize<T, B, C>(b: B) -> Result<T>
where
B: AsRef<[u8]>,
for<'a> T: Deserialize<BinaryProtocolDeserializer<Cursor<&'a [u8]>>>,
B: Into<DeserializeSource<C>>,
C: BufExt,
T: Deserialize<BinaryProtocolDeserializer<C>>,
{
let b = b.as_ref();
let mut deser = BinaryProtocolDeserializer::new(Cursor::new(b));
let source: DeserializeSource<C> = b.into();
let mut deser = BinaryProtocolDeserializer::new(source.0);
Ok(T::read(&mut deser)?)
}

0 comments on commit e211763

Please sign in to comment.