diff --git a/tonic/src/extensions.rs b/tonic/src/extensions.rs index 32b9ad021..bfeee0fc9 100644 --- a/tonic/src/extensions.rs +++ b/tonic/src/extensions.rs @@ -1,78 +1,3 @@ -use std::fmt; - -/// A type map of protocol extensions. -/// -/// `Extensions` can be used by [`Interceptor`] and [`Request`] to store extra data derived from -/// the underlying protocol. -/// -/// [`Interceptor`]: crate::service::Interceptor -/// [`Request`]: crate::Request -#[derive(Default)] -pub struct Extensions { - inner: http::Extensions, -} - -impl Extensions { - pub(crate) fn new() -> Self { - Self { - inner: http::Extensions::new(), - } - } - - /// Insert a type into this `Extensions`. - /// - /// If a extension of this type already existed, it will - /// be returned. - #[inline] - pub fn insert(&mut self, val: T) -> Option { - self.inner.insert(val) - } - - /// Get a reference to a type previously inserted on this `Extensions`. - #[inline] - pub fn get(&self) -> Option<&T> { - self.inner.get() - } - - /// Get a mutable reference to a type previously inserted on this `Extensions`. - #[inline] - pub fn get_mut(&mut self) -> Option<&mut T> { - self.inner.get_mut() - } - - /// Remove a type from this `Extensions`. - /// - /// If a extension of this type existed, it will be returned. - #[inline] - pub fn remove(&mut self) -> Option { - self.inner.remove() - } - - /// Clear the `Extensions` of all inserted extensions. - #[inline] - pub fn clear(&mut self) { - self.inner.clear() - } - - #[inline] - /// Convert from `http::Extensions` - pub fn from_http(http: http::Extensions) -> Self { - Self { inner: http } - } - - /// Convert to `http::Extensions` and consume self. - #[inline] - pub fn into_http(self) -> http::Extensions { - self.inner - } -} - -impl fmt::Debug for Extensions { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("Extensions").finish() - } -} - /// A gRPC Method info extension. #[derive(Debug, Clone)] pub struct GrpcMethod { diff --git a/tonic/src/lib.rs b/tonic/src/lib.rs index 325c6af47..29137536f 100644 --- a/tonic/src/lib.rs +++ b/tonic/src/lib.rs @@ -119,7 +119,8 @@ pub use async_trait::async_trait; #[doc(inline)] pub use codec::Streaming; -pub use extensions::{Extensions, GrpcMethod}; +pub use extensions::GrpcMethod; +pub use http::Extensions; pub use request::{IntoRequest, IntoStreamingRequest, Request}; pub use response::Response; pub use status::{Code, Status}; diff --git a/tonic/src/request.rs b/tonic/src/request.rs index f2cca7c74..e9fe610b8 100644 --- a/tonic/src/request.rs +++ b/tonic/src/request.rs @@ -3,7 +3,7 @@ use crate::metadata::{MetadataMap, MetadataValue}; use crate::transport::server::TcpConnectInfo; #[cfg(feature = "tls")] use crate::transport::server::TlsConnectInfo; -use crate::Extensions; +use http::Extensions; #[cfg(feature = "transport")] use std::net::SocketAddr; #[cfg(feature = "tls")] @@ -161,7 +161,7 @@ impl Request { Request { metadata: MetadataMap::from_headers(parts.headers), message, - extensions: Extensions::from_http(parts.extensions), + extensions: parts.extensions, } } @@ -187,7 +187,7 @@ impl Request { SanitizeHeaders::Yes => self.metadata.into_sanitized_headers(), SanitizeHeaders::No => self.metadata.into_headers(), }; - *request.extensions_mut() = self.extensions.into_http(); + *request.extensions_mut() = self.extensions; request } diff --git a/tonic/src/response.rs b/tonic/src/response.rs index ba9063419..976140f6f 100644 --- a/tonic/src/response.rs +++ b/tonic/src/response.rs @@ -1,4 +1,6 @@ -use crate::{metadata::MetadataMap, Extensions}; +use http::Extensions; + +use crate::metadata::MetadataMap; /// A gRPC response and metadata from an RPC call. #[derive(Debug)] @@ -73,7 +75,7 @@ impl Response { Response { metadata: MetadataMap::from_headers(head.headers), message, - extensions: Extensions::from_http(head.extensions), + extensions: head.extensions, } } @@ -82,7 +84,7 @@ impl Response { *res.version_mut() = http::Version::HTTP_2; *res.headers_mut() = self.metadata.into_sanitized_headers(); - *res.extensions_mut() = self.extensions.into_http(); + *res.extensions_mut() = self.extensions; res }