1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
use serde::Deserialize; use rmpv::{self, ValueRef}; use Error; use protocol; /// Immutable header view. /// /// This struct represents a HPACK header received from a service. It is immutable, because the /// data it points on lays directly in the socket buffer, which makes deserialization so fast. #[derive(Debug, Deserialize, PartialEq)] pub struct HeaderRef<'a> { cached: bool, name: &'a [u8], data: &'a [u8], } impl<'a> HeaderRef<'a> { /// Returns a header name. #[inline] pub fn name(&self) -> &[u8] { self.name } /// Returns a header data. #[inline] pub fn data(&self) -> &[u8] { self.data } } /// Generic response type. #[derive(Debug)] pub struct Response<'a: 'b, 'b> { ty: u64, args: &'b ValueRef<'a>, meta: Vec<HeaderRef<'b>>, } impl<'a: 'b, 'b> Response<'a, 'b> { pub(crate) fn new(ty: u64, args: &'b ValueRef<'a>, _meta: &'b ValueRef<'a>) -> Result<Self, rmpv::ext::Error> { let resp = Self { ty: ty, args: args, meta: Vec::new(), //rmpv::ext::deserialize_from(meta)?, }; Ok(resp) } /// Returns a response type. #[inline] pub fn ty(&self) -> u64 { self.ty } /// Deserializes the response into the specified type. /// /// Note, that this method can also be used to deserialize a response into some borrowed type, /// like `&str`, `&[u8]` or into an user-defined type that contains them. In this case the /// deserialization is completely zero-copy. /// /// # Errors /// /// This method can fail if the underlying deserializer decides to fail. /// /// # Examples /// /// This example demonstrates how to use deserialization into a borrowed struct. /// /// ``` /// # #[macro_use] extern crate serde_derive; /// # extern crate cocaine; /// use cocaine::{Error, Response}; /// /// #[derive(Debug, Deserialize)] /// struct User<'a> { /// name: &'a str, /// age: u16, /// } /// /// fn handle(resp: &Response) -> Result<(), Error> { /// let user: User = resp.deserialize()?; /// println!("User: {:?}", user); /// Ok(()) /// } /// # fn main() {} /// ``` #[inline] pub fn deserialize<T: Deserialize<'b>>(&self) -> Result<T, Error> { protocol::deserialize(self.ty, self.args) } /// Returns response headers. #[inline] pub fn headers(&self) -> &[HeaderRef] { &self.meta } }