pub struct Protobuf<T>(pub T);
Available on crate feature
protobuf
only.Expand description
A Protocol Buffer message extractor and response.
This can be used both as an extractor and as a response.
§As extractor
When used as an extractor, it can decode request bodies into some type that
implements prost::Message
. The request will be rejected (and a ProtobufRejection
will
be returned) if:
- The body couldn’t be decoded into the target Protocol Buffer message type.
- Buffering the request body fails.
See ProtobufRejection
for more details.
The extractor does not expect a Content-Type
header to be present in the request.
§Extractor example
use axum::{routing::post, Router};
use axum_extra::protobuf::Protobuf;
#[derive(prost::Message)]
struct CreateUser {
#[prost(string, tag="1")]
email: String,
#[prost(string, tag="2")]
password: String,
}
async fn create_user(Protobuf(payload): Protobuf<CreateUser>) {
// payload is `CreateUser`
}
let app = Router::new().route("/users", post(create_user));
§As response
When used as a response, it can encode any type that implements prost::Message
to
a newly allocated buffer.
If no Content-Type
header is set, the Content-Type: application/octet-stream
header
will be used automatically.
§Response example
use axum::{
extract::Path,
routing::get,
Router,
};
use axum_extra::protobuf::Protobuf;
#[derive(prost::Message)]
struct User {
#[prost(string, tag="1")]
username: String,
}
async fn get_user(Path(user_id) : Path<String>) -> Protobuf<User> {
let user = find_user(user_id).await;
Protobuf(user)
}
async fn find_user(user_id: String) -> User {
// ...
}
let app = Router::new().route("/users/:id", get(get_user));
Tuple Fields§
§0: T
Trait Implementations§
Source§impl<T, S> FromRequest<S> for Protobuf<T>
impl<T, S> FromRequest<S> for Protobuf<T>
Source§type Rejection = ProtobufRejection
type Rejection = ProtobufRejection
If the extractor fails it’ll use this “rejection” type. A rejection is
a kind of error that can be converted into a response.
Source§impl<T> IntoResponse for Protobuf<T>
impl<T> IntoResponse for Protobuf<T>
Source§fn into_response(self) -> Response
fn into_response(self) -> Response
Create a response.
impl<T: Copy> Copy for Protobuf<T>
Auto Trait Implementations§
impl<T> Freeze for Protobuf<T>where
T: Freeze,
impl<T> RefUnwindSafe for Protobuf<T>where
T: RefUnwindSafe,
impl<T> Send for Protobuf<T>where
T: Send,
impl<T> Sync for Protobuf<T>where
T: Sync,
impl<T> Unpin for Protobuf<T>where
T: Unpin,
impl<T> UnwindSafe for Protobuf<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T, S> Handler<IntoResponseHandler, S> for T
impl<T, S> Handler<IntoResponseHandler, S> for T
Source§fn call(
self,
_req: Request<Body>,
_state: S,
) -> <T as Handler<IntoResponseHandler, S>>::Future
fn call( self, _req: Request<Body>, _state: S, ) -> <T as Handler<IntoResponseHandler, S>>::Future
Call the handler with the given request.
Source§fn layer<L>(self, layer: L) -> Layered<L, Self, T, S>where
L: Layer<HandlerService<Self, T, S>> + Clone,
<L as Layer<HandlerService<Self, T, S>>>::Service: Service<Request<Body>>,
fn layer<L>(self, layer: L) -> Layered<L, Self, T, S>where
L: Layer<HandlerService<Self, T, S>> + Clone,
<L as Layer<HandlerService<Self, T, S>>>::Service: Service<Request<Body>>,
Apply a
tower::Layer
to the handler. Read moreSource§fn with_state(self, state: S) -> HandlerService<Self, T, S>
fn with_state(self, state: S) -> HandlerService<Self, T, S>
Convert the handler into a
Service
by providing the stateSource§impl<H, T> HandlerWithoutStateExt<T> for H
impl<H, T> HandlerWithoutStateExt<T> for H
Source§fn into_service(self) -> HandlerService<H, T, ()>
fn into_service(self) -> HandlerService<H, T, ()>
Convert the handler into a
Service
and no state.Source§fn into_make_service(self) -> IntoMakeService<HandlerService<H, T, ()>>
fn into_make_service(self) -> IntoMakeService<HandlerService<H, T, ()>>
Convert the handler into a
MakeService
and no state. Read more