tonic

Struct Request

Source
pub struct Request<T> { /* private fields */ }
Expand description

A gRPC request and metadata from an RPC call.

Implementations§

Source§

impl<T> Request<T>

Source

pub fn new(message: T) -> Self

Create a new gRPC request.

Request::new(HelloRequest {
   name: "Bob".into(),
});
Source

pub fn get_ref(&self) -> &T

Get a reference to the message

Source

pub fn get_mut(&mut self) -> &mut T

Get a mutable reference to the message

Source

pub fn metadata(&self) -> &MetadataMap

Get a reference to the custom request metadata.

Source

pub fn metadata_mut(&mut self) -> &mut MetadataMap

Get a mutable reference to the request metadata.

Source

pub fn into_inner(self) -> T

Consumes self, returning the message

Source

pub fn into_parts(self) -> (MetadataMap, Extensions, T)

Consumes self returning the parts of the request.

Source

pub fn from_parts( metadata: MetadataMap, extensions: Extensions, message: T, ) -> Self

Create a new gRPC request from metadata, extensions and message.

Source

pub fn from_http(http: Request<T>) -> Self

Convert an HTTP request to a gRPC request

Source

pub fn local_addr(&self) -> Option<SocketAddr>

Get the local address of this connection.

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.

Source

pub fn remote_addr(&self) -> Option<SocketAddr>

Get the remote address of this connection.

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.

Source

pub fn set_timeout(&mut self, deadline: Duration)

Set the max duration the request is allowed to take.

Requires the server to support the grpc-timeout metadata, which Tonic does.

The duration will be formatted according to the spec and use the most precise unit possible.

Example:

use std::time::Duration;
use tonic::Request;

let mut request = Request::new(());

request.set_timeout(Duration::from_secs(30));

let value = request.metadata().get("grpc-timeout").unwrap();

assert_eq!(
    value,
    // equivalent to 30 seconds
    "30000000u"
);
Source

pub fn extensions(&self) -> &Extensions

Returns a reference to the associated extensions.

Source

pub fn extensions_mut(&mut self) -> &mut Extensions

Returns a mutable reference to the associated extensions.

§Example

Extensions can be set in interceptors:

use tonic::{Request, service::interceptor};

#[derive(Clone)] // Extensions must be Clone
struct MyExtension {
    some_piece_of_data: String,
}

interceptor(|mut request: Request<()>| {
    request.extensions_mut().insert(MyExtension {
        some_piece_of_data: "foo".to_string(),
    });

    Ok(request)
});

And picked up by RPCs:

use tonic::{async_trait, Status, Request, Response};

#[async_trait]
impl TestService for MyService {
    async fn handler(&self, req: Request<Input>) -> Result<Response<Output>, Status> {
        let value: &MyExtension = req.extensions().get::<MyExtension>().unwrap();

        Ok(Response::new(Output {}))
    }
}

Trait Implementations§

Source§

impl<T: Debug> Debug for Request<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> IntoRequest<T> for Request<T>

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<T> IntoStreamingRequest for Request<T>
where T: Stream + Send + 'static,

Source§

type Stream = T

The RPC request stream type
Source§

type Message = <T as Stream>::Item

The RPC request type
Source§

fn into_streaming_request(self) -> Self

Wrap the stream of messages in a tonic::Request

Auto Trait Implementations§

§

impl<T> Freeze for Request<T>
where T: Freeze,

§

impl<T> !RefUnwindSafe for Request<T>

§

impl<T> Send for Request<T>
where T: Send,

§

impl<T> Sync for Request<T>
where T: Sync,

§

impl<T> Unpin for Request<T>
where T: Unpin,

§

impl<T> !UnwindSafe for Request<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more