tower_grpc/
response.rs

1use crate::metadata::MetadataMap;
2
3/// A gRPC response and metadata from an RPC call.
4#[derive(Debug)]
5pub struct Response<T> {
6    metadata: MetadataMap,
7    message: T,
8}
9
10impl<T> Response<T> {
11    /// Create a new gRPC response.
12    pub fn new(message: T) -> Self {
13        Response {
14            metadata: MetadataMap::new(),
15            message,
16        }
17    }
18
19    /// Get a reference to the message
20    pub fn get_ref(&self) -> &T {
21        &self.message
22    }
23
24    /// Get a mutable reference to the message
25    pub fn get_mut(&mut self) -> &mut T {
26        &mut self.message
27    }
28
29    /// Get a reference to the custom response metadata.
30    pub fn metadata(&self) -> &MetadataMap {
31        &self.metadata
32    }
33
34    /// Get a mutable reference to the response metadata.
35    pub fn metadata_mut(&mut self) -> &mut MetadataMap {
36        &mut self.metadata
37    }
38
39    /// Consumes `self`, returning the message
40    pub fn into_inner(self) -> T {
41        self.message
42    }
43
44    pub(crate) fn from_http(res: http::Response<T>) -> Self {
45        let (head, message) = res.into_parts();
46        Response {
47            metadata: MetadataMap::from_headers(head.headers),
48            message,
49        }
50    }
51
52    pub fn into_http(self) -> http::Response<T> {
53        let mut res = http::Response::new(self.message);
54
55        *res.version_mut() = http::Version::HTTP_2;
56        *res.headers_mut() = self.metadata.into_headers();
57
58        res
59    }
60
61    pub fn map<F, U>(self, f: F) -> Response<U>
62    where
63        F: FnOnce(T) -> U,
64    {
65        let message = f(self.message);
66        Response {
67            metadata: self.metadata,
68            message,
69        }
70    }
71
72    // pub fn metadata()
73    // pub fn metadata_bin()
74}