Skip to content

Commit ed95d27

Browse files
authoredJun 12, 2024··
feat: Use http::Extensions directly (#1710)
1 parent 9c1f2f9 commit ed95d27

File tree

4 files changed

+10
-82
lines changed

4 files changed

+10
-82
lines changed
 

‎tonic/src/extensions.rs

-75
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,3 @@
1-
use std::fmt;
2-
3-
/// A type map of protocol extensions.
4-
///
5-
/// `Extensions` can be used by [`Interceptor`] and [`Request`] to store extra data derived from
6-
/// the underlying protocol.
7-
///
8-
/// [`Interceptor`]: crate::service::Interceptor
9-
/// [`Request`]: crate::Request
10-
#[derive(Default)]
11-
pub struct Extensions {
12-
inner: http::Extensions,
13-
}
14-
15-
impl Extensions {
16-
pub(crate) fn new() -> Self {
17-
Self {
18-
inner: http::Extensions::new(),
19-
}
20-
}
21-
22-
/// Insert a type into this `Extensions`.
23-
///
24-
/// If a extension of this type already existed, it will
25-
/// be returned.
26-
#[inline]
27-
pub fn insert<T: Clone + Send + Sync + 'static>(&mut self, val: T) -> Option<T> {
28-
self.inner.insert(val)
29-
}
30-
31-
/// Get a reference to a type previously inserted on this `Extensions`.
32-
#[inline]
33-
pub fn get<T: Send + Sync + 'static>(&self) -> Option<&T> {
34-
self.inner.get()
35-
}
36-
37-
/// Get a mutable reference to a type previously inserted on this `Extensions`.
38-
#[inline]
39-
pub fn get_mut<T: Send + Sync + 'static>(&mut self) -> Option<&mut T> {
40-
self.inner.get_mut()
41-
}
42-
43-
/// Remove a type from this `Extensions`.
44-
///
45-
/// If a extension of this type existed, it will be returned.
46-
#[inline]
47-
pub fn remove<T: Send + Sync + 'static>(&mut self) -> Option<T> {
48-
self.inner.remove()
49-
}
50-
51-
/// Clear the `Extensions` of all inserted extensions.
52-
#[inline]
53-
pub fn clear(&mut self) {
54-
self.inner.clear()
55-
}
56-
57-
#[inline]
58-
/// Convert from `http::Extensions`
59-
pub fn from_http(http: http::Extensions) -> Self {
60-
Self { inner: http }
61-
}
62-
63-
/// Convert to `http::Extensions` and consume self.
64-
#[inline]
65-
pub fn into_http(self) -> http::Extensions {
66-
self.inner
67-
}
68-
}
69-
70-
impl fmt::Debug for Extensions {
71-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72-
f.debug_struct("Extensions").finish()
73-
}
74-
}
75-
761
/// A gRPC Method info extension.
772
#[derive(Debug, Clone)]
783
pub struct GrpcMethod {

‎tonic/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ pub use async_trait::async_trait;
119119

120120
#[doc(inline)]
121121
pub use codec::Streaming;
122-
pub use extensions::{Extensions, GrpcMethod};
122+
pub use extensions::GrpcMethod;
123+
pub use http::Extensions;
123124
pub use request::{IntoRequest, IntoStreamingRequest, Request};
124125
pub use response::Response;
125126
pub use status::{Code, Status};

‎tonic/src/request.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::metadata::{MetadataMap, MetadataValue};
33
use crate::transport::server::TcpConnectInfo;
44
#[cfg(feature = "tls")]
55
use crate::transport::server::TlsConnectInfo;
6-
use crate::Extensions;
6+
use http::Extensions;
77
#[cfg(feature = "transport")]
88
use std::net::SocketAddr;
99
#[cfg(feature = "tls")]
@@ -161,7 +161,7 @@ impl<T> Request<T> {
161161
Request {
162162
metadata: MetadataMap::from_headers(parts.headers),
163163
message,
164-
extensions: Extensions::from_http(parts.extensions),
164+
extensions: parts.extensions,
165165
}
166166
}
167167

@@ -187,7 +187,7 @@ impl<T> Request<T> {
187187
SanitizeHeaders::Yes => self.metadata.into_sanitized_headers(),
188188
SanitizeHeaders::No => self.metadata.into_headers(),
189189
};
190-
*request.extensions_mut() = self.extensions.into_http();
190+
*request.extensions_mut() = self.extensions;
191191

192192
request
193193
}

‎tonic/src/response.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::{metadata::MetadataMap, Extensions};
1+
use http::Extensions;
2+
3+
use crate::metadata::MetadataMap;
24

35
/// A gRPC response and metadata from an RPC call.
46
#[derive(Debug)]
@@ -73,7 +75,7 @@ impl<T> Response<T> {
7375
Response {
7476
metadata: MetadataMap::from_headers(head.headers),
7577
message,
76-
extensions: Extensions::from_http(head.extensions),
78+
extensions: head.extensions,
7779
}
7880
}
7981

@@ -82,7 +84,7 @@ impl<T> Response<T> {
8284

8385
*res.version_mut() = http::Version::HTTP_2;
8486
*res.headers_mut() = self.metadata.into_sanitized_headers();
85-
*res.extensions_mut() = self.extensions.into_http();
87+
*res.extensions_mut() = self.extensions;
8688

8789
res
8890
}

0 commit comments

Comments
 (0)
Please sign in to comment.