Skip to content

Commit

Permalink
chore(tonic): Remove API when they only return None
Browse files Browse the repository at this point in the history
  • Loading branch information
tottoto committed Dec 9, 2023
1 parent 07e4ee1 commit c1443e2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 96 deletions.
120 changes: 37 additions & 83 deletions tonic/src/request.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use crate::metadata::{MetadataMap, MetadataValue};
#[cfg(feature = "transport")]
use crate::transport::server::TcpConnectInfo;
#[cfg(all(feature = "transport", feature = "tls"))]
use crate::transport::server::TlsConnectInfo;
#[cfg(feature = "transport")]
#[allow(deprecated)]
use crate::transport::Certificate;
#[cfg(feature = "tls")]
use crate::transport::{server::TlsConnectInfo, Certificate};
use crate::Extensions;
#[cfg(feature = "transport")]
use std::net::SocketAddr;
#[cfg(feature = "tls")]
use std::sync::Arc;
use std::{net::SocketAddr, time::Duration};
use std::time::Duration;
use tokio_stream::Stream;

/// A gRPC request and metadata from an RPC call.
Expand Down Expand Up @@ -210,39 +209,25 @@ impl<T> Request<T> {
/// This will return `None` if the `IO` type used
/// does not implement `Connected` or when using a unix domain socket.
/// This currently only works on the server side.
#[cfg_attr(
not(feature = "transport"),
deprecated(
since = "0.10.3",
note = "`remote_addr` only returns `None` without transport feature. This API will require transport feature.",
)
)]
#[cfg(feature = "transport")]
pub fn local_addr(&self) -> Option<SocketAddr> {
#[cfg(feature = "transport")]
#[cfg(feature = "tls")]
{
#[cfg(feature = "tls")]
{
self.extensions()
.get::<TcpConnectInfo>()
.and_then(|i| i.local_addr())
.or_else(|| {
self.extensions()
.get::<TlsConnectInfo<TcpConnectInfo>>()
.and_then(|i| i.get_ref().local_addr())
})
}

#[cfg(not(feature = "tls"))]
{
self.extensions()
.get::<TcpConnectInfo>()
.and_then(|i| i.local_addr())
}
self.extensions()
.get::<TcpConnectInfo>()
.and_then(|i| i.local_addr())
.or_else(|| {
self.extensions()
.get::<TlsConnectInfo<TcpConnectInfo>>()
.and_then(|i| i.get_ref().local_addr())
})
}

#[cfg(not(feature = "transport"))]
#[cfg(not(feature = "tls"))]
{
None
self.extensions()
.get::<TcpConnectInfo>()
.and_then(|i| i.local_addr())
}
}

Expand All @@ -251,39 +236,25 @@ impl<T> Request<T> {
/// This will return `None` if the `IO` type used
/// does not implement `Connected` or when using a unix domain socket.
/// This currently only works on the server side.
#[cfg_attr(
not(feature = "transport"),
deprecated(
since = "0.10.3",
note = "`remote_addr` only returns `None` without transport feature. This API will require transport feature.",
)
)]
#[cfg(feature = "transport")]
pub fn remote_addr(&self) -> Option<SocketAddr> {
#[cfg(feature = "transport")]
#[cfg(feature = "tls")]
{
#[cfg(feature = "tls")]
{
self.extensions()
.get::<TcpConnectInfo>()
.and_then(|i| i.remote_addr())
.or_else(|| {
self.extensions()
.get::<TlsConnectInfo<TcpConnectInfo>>()
.and_then(|i| i.get_ref().remote_addr())
})
}

#[cfg(not(feature = "tls"))]
{
self.extensions()
.get::<TcpConnectInfo>()
.and_then(|i| i.remote_addr())
}
self.extensions()
.get::<TcpConnectInfo>()
.and_then(|i| i.remote_addr())
.or_else(|| {
self.extensions()
.get::<TlsConnectInfo<TcpConnectInfo>>()
.and_then(|i| i.get_ref().remote_addr())
})
}

#[cfg(not(feature = "transport"))]
#[cfg(not(feature = "tls"))]
{
None
self.extensions()
.get::<TcpConnectInfo>()
.and_then(|i| i.remote_addr())
}
}

Expand All @@ -293,28 +264,11 @@ impl<T> Request<T> {
/// and is mostly used for mTLS. This currently only returns
/// `Some` on the server side of the `transport` server with
/// TLS enabled connections.
#[cfg(feature = "transport")]
#[cfg_attr(
all(feature = "transport", not(feature = "tls")),
deprecated(
since = "0.10.3",
note = "`peer_certs` only returns `None` without tls feature. This API will require tls feature.",
)
)]
#[cfg_attr(docsrs, doc(cfg(feature = "transport")))]
#[allow(deprecated)]
#[cfg(feature = "tls")]
pub fn peer_certs(&self) -> Option<Arc<Vec<Certificate>>> {
#[cfg(feature = "tls")]
{
self.extensions()
.get::<TlsConnectInfo<TcpConnectInfo>>()
.and_then(|i| i.peer_certs())
}

#[cfg(not(feature = "tls"))]
{
None
}
self.extensions()
.get::<TlsConnectInfo<TcpConnectInfo>>()
.and_then(|i| i.peer_certs())
}

/// Set the max duration the request is allowed to take.
Expand Down
5 changes: 4 additions & 1 deletion tonic/src/transport/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ pub mod server;

mod error;
mod service;
#[cfg(feature = "tls")]
#[cfg_attr(docsrs, doc(cfg(feature = "tls")))]
mod tls;

#[doc(inline)]
Expand All @@ -103,7 +105,8 @@ pub use self::error::Error;
pub use self::server::Server;
#[doc(inline)]
pub use self::service::grpc_timeout::TimeoutExpired;
#[allow(deprecated)]
#[cfg(feature = "tls")]
#[cfg_attr(docsrs, doc(cfg(feature = "tls")))]
pub use self::tls::Certificate;
#[doc(inline)]
/// A deprecated re-export. Please use `tonic::server::NamedService` directly.
Expand Down
12 changes: 0 additions & 12 deletions tonic/src/transport/tls.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
/// Represents a X509 certificate.
#[cfg_attr(
not(feature = "tls"),
deprecated(
since = "0.10.3",
note = "`Certificate` is used only by deprecated API without tls feature.",
)
)]
#[derive(Debug, Clone)]
pub struct Certificate {
pub(crate) pem: Vec<u8>,
}

/// Represents a private key and X509 certificate.
#[cfg(feature = "tls")]
#[cfg_attr(docsrs, doc(cfg(feature = "tls")))]
#[derive(Debug, Clone)]
pub struct Identity {
pub(crate) cert: Certificate,
pub(crate) key: Vec<u8>,
}

#[allow(deprecated)]
impl Certificate {
/// Parse a PEM encoded X509 Certificate.
///
Expand All @@ -46,14 +36,12 @@ impl Certificate {
}
}

#[allow(deprecated)]
impl AsRef<[u8]> for Certificate {
fn as_ref(&self) -> &[u8] {
self.pem.as_ref()
}
}

#[cfg(feature = "tls")]
impl Identity {
/// Parse a PEM encoded certificate and private key.
///
Expand Down

0 comments on commit c1443e2

Please sign in to comment.