Skip to content

Commit

Permalink
Add better example for Connected
Browse files Browse the repository at this point in the history
  • Loading branch information
davidpdrsn committed May 17, 2021
1 parent 85185ef commit 39cf177
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion tonic/src/transport/server/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,38 @@ use tokio_rustls::{rustls::Session, server::TlsStream};
/// custom IO types that can still provide the same connection
/// metadata.
///
/// The `ConnectInfo` returned will be accessible through [request extensions][ext].
/// # Example
///
/// The `ConnectInfo` returned will be accessible through [request extensions][ext]:
///
/// ```
/// use tonic::{Request, transport::server::Connected};
///
/// // A `Stream` that yields connections
/// struct MyConnector {}
///
/// // Return metadata about the connection as `MyConnectInfo`
/// impl Connected for MyConnector {
/// type ConnectInfo = MyConnectInfo;
///
/// fn connect_info(&self) -> Self::ConnectInfo {
/// MyConnectInfo {}
/// }
/// }
///
/// #[derive(Clone)]
/// struct MyConnectInfo {
/// // Metadata about your connection
/// }
///
/// // The connect info can be accessed through request extensions:
/// # fn foo(request: Request<()>) {
/// let connect_info: &MyConnectInfo = request
/// .extensions()
/// .get::<MyConnectInfo>()
/// .expect("bug in tonic");
/// # }
/// ```
///
/// [ext]: crate::Request::extensions
pub trait Connected {
Expand Down

0 comments on commit 39cf177

Please sign in to comment.