Skip to content

Commit

Permalink
use AFIT instead of async_trait 5
Browse files Browse the repository at this point in the history
  • Loading branch information
sunli829 committed Mar 30, 2024
1 parent accbb3e commit f6611ba
Show file tree
Hide file tree
Showing 39 changed files with 52 additions and 91 deletions.
1 change: 0 additions & 1 deletion examples/poem/custom-extractor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use poem::{
struct Token(String);

// Implements a token extractor
#[poem::async_trait]
impl<'a> FromRequest<'a> for Token {
async fn from_request(req: &'a Request, _body: &mut RequestBody) -> Result<Self> {
let token = req
Expand Down
1 change: 0 additions & 1 deletion poem-lambda/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ fn from_lambda_request(req: LambdaRequest) -> Request {
req
}

#[poem::async_trait]
impl<'a> FromRequest<'a> for &'a Context {
async fn from_request(req: &'a Request, _body: &mut RequestBody) -> Result<Self> {
let ctx = match req.extensions().get::<Context>() {
Expand Down
1 change: 0 additions & 1 deletion poem-openapi-derive/src/multipart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,6 @@ pub(crate) fn generate(args: DeriveInput) -> GeneratorResult<TokenStream> {
}
}

#[#crate_name::__private::poem::async_trait]
impl #extractor_impl_generics #crate_name::ApiExtractor<'__request> for #ident #ty_generics #where_clause {
const TYPES: &'static [#crate_name::ApiExtractorType] = &[#crate_name::ApiExtractorType::RequestObject];

Expand Down
1 change: 0 additions & 1 deletion poem-openapi-derive/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ pub(crate) fn generate(args: DeriveInput) -> GeneratorResult<TokenStream> {

let expanded = {
quote! {
#[#crate_name::__private::poem::async_trait]
impl #impl_generics #crate_name::ApiExtractor<'__request> for #ident #ty_generics #where_clause {
const TYPES: &'static [#crate_name::ApiExtractorType] = &[#crate_name::ApiExtractorType::RequestObject];

Expand Down
1 change: 0 additions & 1 deletion poem-openapi-derive/src/response_content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ pub(crate) fn generate(args: DeriveInput) -> GeneratorResult<TokenStream> {

let expanded = {
quote! {
#[#crate_name::__private::poem::async_trait]
impl #impl_generics #crate_name::ResponseContent for #ident #ty_generics #where_clause {
fn media_types() -> Vec<#crate_name::registry::MetaMediaType> {
::std::vec![#(#content),*]
Expand Down
2 changes: 0 additions & 2 deletions poem-openapi-derive/src/security_scheme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,6 @@ pub(crate) fn generate(args: DeriveInput) -> GeneratorResult<TokenStream> {
};

let expanded = quote! {
#[#crate_name::__private::poem::async_trait]
impl<'a> #crate_name::ApiExtractor<'a> for #ident {
const TYPES: &'static [#crate_name::ApiExtractorType] = &[#crate_name::ApiExtractorType::SecurityScheme];

Expand Down Expand Up @@ -545,7 +544,6 @@ pub(crate) fn generate(args: DeriveInput) -> GeneratorResult<TokenStream> {
};

let expanded = quote! {
#[#crate_name::__private::poem::async_trait]
impl<'a> #crate_name::ApiExtractor<'a> for #ident {
const TYPES: &'static [#crate_name::ApiExtractorType] = &[#crate_name::ApiExtractorType::SecurityScheme];

Expand Down
7 changes: 0 additions & 7 deletions poem-openapi-derive/src/webhook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,6 @@ pub(crate) fn generate(
for item in &mut trait_impl.items {
if let TraitItem::Fn(method) = item {
if let Some(operation_args) = parse_oai_attrs::<WebhookOperation>(&method.attrs)? {
if method.sig.asyncness.is_none() {
return Err(
Error::new_spanned(&method.sig.ident, "Must be asynchronous").into(),
);
}

generate_operation(&mut ctx, &crate_name, &args, operation_args, method)?;
remove_oai_attrs(&mut method.attrs);
}
Expand All @@ -99,7 +93,6 @@ pub(crate) fn generate(
let operations = operations.values();

let expanded = quote! {
#[#crate_name::__private::poem::async_trait]
#trait_impl

impl #crate_name::Webhook for &dyn #ident {
Expand Down
12 changes: 7 additions & 5 deletions poem-openapi/src/base.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::{
collections::HashMap,
fmt::{self, Debug, Display},
future::Future,
ops::Deref,
};

use futures_util::FutureExt;
use poem::{endpoint::BoxEndpoint, http::Method, Error, FromRequest, Request, RequestBody, Result};

use crate::{
Expand Down Expand Up @@ -152,7 +154,6 @@ impl<T> Default for ExtractParamOptions<T> {
/// - **T: poem::FromRequest**
///
/// Use Poem's extractor.
#[poem::async_trait]
#[allow(unused_variables)]
pub trait ApiExtractor<'a>: Sized {
/// The type of API extractor.
Expand Down Expand Up @@ -196,14 +197,13 @@ pub trait ApiExtractor<'a>: Sized {
}

/// Parse from the HTTP request.
async fn from_request(
fn from_request(
request: &'a Request,
body: &mut RequestBody,
param_opts: ExtractParamOptions<Self::ParamType>,
) -> Result<Self>;
) -> impl Future<Output = Result<Self>> + Send;
}

#[poem::async_trait]
impl<'a, T: FromRequest<'a>> ApiExtractor<'a> for T {
const TYPES: &'static [ApiExtractorType] = &[ApiExtractorType::PoemExtractor];

Expand All @@ -215,7 +215,9 @@ impl<'a, T: FromRequest<'a>> ApiExtractor<'a> for T {
body: &mut RequestBody,
_param_opts: ExtractParamOptions<Self::ParamType>,
) -> Result<Self> {
T::from_request(request, body).await
// FIXME: remove the unnecessary boxed
// https://github.com/rust-lang/rust/issues/100013
T::from_request(request, body).boxed().await
}
}

Expand Down
1 change: 0 additions & 1 deletion poem-openapi/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ macro_rules! impl_apirequest_for_payload {
};

($ty:ty, $($bounds:tt)*) => {
#[poem::async_trait]
impl<'a, $($bounds)*> $crate::ApiExtractor<'a> for $ty {
const TYPES: &'static [$crate::ApiExtractorType] = &[$crate::ApiExtractorType::RequestObject];

Expand Down
3 changes: 0 additions & 3 deletions poem-openapi/src/param/cookie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ impl<T> DerefMut for Cookie<T> {
}
}

#[poem::async_trait]
impl<'a, T: ParseFromParameter> ApiExtractor<'a> for Cookie<T> {
const TYPES: &'static [ApiExtractorType] = &[ApiExtractorType::Parameter];
const PARAM_IS_REQUIRED: bool = T::IS_REQUIRED;
Expand Down Expand Up @@ -95,7 +94,6 @@ impl<T> DerefMut for CookiePrivate<T> {
}
}

#[poem::async_trait]
impl<'a, T: ParseFromParameter> ApiExtractor<'a> for CookiePrivate<T> {
const TYPES: &'static [ApiExtractorType] = &[ApiExtractorType::Parameter];
const PARAM_IS_REQUIRED: bool = T::IS_REQUIRED;
Expand Down Expand Up @@ -165,7 +163,6 @@ impl<T> DerefMut for CookieSigned<T> {
}
}

#[poem::async_trait]
impl<'a, T: ParseFromParameter> ApiExtractor<'a> for CookieSigned<T> {
const TYPES: &'static [ApiExtractorType] = &[ApiExtractorType::Parameter];
const PARAM_IS_REQUIRED: bool = T::IS_REQUIRED;
Expand Down
1 change: 0 additions & 1 deletion poem-openapi/src/param/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ impl<T> DerefMut for Header<T> {
}
}

#[poem::async_trait]
impl<'a, T: ParseFromParameter> ApiExtractor<'a> for Header<T> {
const TYPES: &'static [ApiExtractorType] = &[ApiExtractorType::Parameter];
const PARAM_IS_REQUIRED: bool = T::IS_REQUIRED;
Expand Down
1 change: 0 additions & 1 deletion poem-openapi/src/param/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ impl<T> DerefMut for Path<T> {
}
}

#[poem::async_trait]
impl<'a, T: ParseFromParameter> ApiExtractor<'a> for Path<T> {
const TYPES: &'static [ApiExtractorType] = &[ApiExtractorType::Parameter];
const PARAM_IS_REQUIRED: bool = T::IS_REQUIRED;
Expand Down
1 change: 0 additions & 1 deletion poem-openapi/src/param/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ impl<T> DerefMut for Query<T> {
}
}

#[poem::async_trait]
impl<'a, T: ParseFromParameter> ApiExtractor<'a> for Query<T> {
const TYPES: &'static [ApiExtractorType] = &[ApiExtractorType::Parameter];
const PARAM_IS_REQUIRED: bool = T::IS_REQUIRED;
Expand Down
2 changes: 1 addition & 1 deletion poem-openapi/src/payload/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl<T: DeserializeOwned> ParsePayload for Form<T> {
const IS_REQUIRED: bool = true;

async fn from_request(req: &Request, body: &mut RequestBody) -> Result<Self> {
let data: Vec<u8> = FromRequest::from_request(req, body).await?;
let data = Vec::<u8>::from_request(req, body).await?;
Ok(Self(serde_urlencoded::from_bytes(&data).map_err(
|err| ParseRequestPayloadError {
reason: err.to_string(),
Expand Down
2 changes: 1 addition & 1 deletion poem-openapi/src/payload/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl<T: ParseFromJSON> ParsePayload for Json<T> {
const IS_REQUIRED: bool = true;

async fn from_request(request: &Request, body: &mut RequestBody) -> Result<Self> {
let data: Vec<u8> = FromRequest::from_request(request, body).await?;
let data = Vec::<u8>::from_request(request, body).await?;
let value = if data.is_empty() {
Value::Null
} else {
Expand Down
2 changes: 1 addition & 1 deletion poem-openapi/src/payload/xml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl<T: ParseFromXML> ParsePayload for Xml<T> {
const IS_REQUIRED: bool = true;

async fn from_request(request: &Request, body: &mut RequestBody) -> Result<Self> {
let data: Vec<u8> = FromRequest::from_request(request, body).await?;
let data = Vec::<u8>::from_request(request, body).await?;
let value = if data.is_empty() {
Value::Null
} else {
Expand Down
2 changes: 1 addition & 1 deletion poem-openapi/src/payload/yaml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl<T: ParseFromYAML> ParsePayload for Yaml<T> {
const IS_REQUIRED: bool = true;

async fn from_request(request: &Request, body: &mut RequestBody) -> Result<Self> {
let data: Vec<u8> = FromRequest::from_request(request, body).await?;
let data = Vec::<u8>::from_request(request, body).await?;
let value = if data.is_empty() {
Value::Null
} else {
Expand Down
30 changes: 15 additions & 15 deletions poem-openapi/tests/webhook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ async fn name() {
#[Webhook]
trait MyWebhooks: Sync {
#[oai(name = "a", method = "post")]
async fn test1(&self);
fn test1(&self);

#[oai(method = "trace")]
async fn test2(&self);
fn test2(&self);
}

assert_eq!(<&dyn MyWebhooks>::meta()[0].name, "a");
Expand All @@ -30,10 +30,10 @@ async fn method() {
#[Webhook]
trait MyWebhooks: Sync {
#[oai(method = "post")]
async fn test1(&self);
fn test1(&self);

#[oai(method = "trace")]
async fn test2(&self);
fn test2(&self);
}

assert_eq!(<&dyn MyWebhooks>::meta()[0].operation.method, Method::POST);
Expand All @@ -45,10 +45,10 @@ async fn deprecated() {
#[Webhook]
trait MyWebhooks: Sync {
#[oai(method = "post")]
async fn test1(&self);
fn test1(&self);

#[oai(method = "get", deprecated)]
async fn test2(&self);
fn test2(&self);
}

assert!(!<&dyn MyWebhooks>::meta()[0].operation.deprecated);
Expand All @@ -67,10 +67,10 @@ async fn tags() {
#[Webhook(tag = "MyTags::A")]
trait MyWebhooks: Sync {
#[oai(method = "post", tag = "MyTags::B", tag = "MyTags::C")]
async fn test1(&self);
fn test1(&self);

#[oai(method = "get", tag = "MyTags::B")]
async fn test2(&self);
fn test2(&self);
}

assert_eq!(
Expand All @@ -85,10 +85,10 @@ async fn operation_id() {
#[Webhook]
trait MyWebhooks: Sync {
#[oai(method = "post", operation_id = "a")]
async fn test1(&self);
fn test1(&self);

#[oai(method = "get", operation_id = "b")]
async fn test2(&self);
fn test2(&self);
}

assert_eq!(
Expand All @@ -106,7 +106,7 @@ async fn parameters() {
#[Webhook]
trait MyWebhooks: Sync {
#[oai(method = "post")]
async fn test(&self, a: Query<i32>, b: Path<String>);
fn test(&self, a: Query<i32>, b: Path<String>);
}

assert_eq!(
Expand Down Expand Up @@ -139,7 +139,7 @@ async fn request_body() {
#[Webhook]
trait MyWebhooks: Sync {
#[oai(method = "post")]
async fn test(&self, req: Json<i32>);
fn test(&self, req: Json<i32>);
}

assert_eq!(
Expand All @@ -160,7 +160,7 @@ async fn response() {
#[Webhook]
trait MyWebhooks: Sync {
#[oai(method = "post")]
async fn test(&self) -> Json<i32>;
fn test(&self) -> Json<i32>;
}

assert_eq!(
Expand All @@ -184,7 +184,7 @@ async fn create() {
#[Webhook]
trait MyWebhooks: Sync {
#[oai(method = "post")]
async fn test(&self) -> Json<i32>;
fn test(&self) -> Json<i32>;
}

let _ = OpenApiService::new((), "Test", "1.0").webhooks::<&dyn MyWebhooks>();
Expand All @@ -198,7 +198,7 @@ async fn external_docs() {
method = "post",
external_docs = "https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md"
)]
async fn test(&self);
fn test(&self);
}

assert_eq!(
Expand Down
1 change: 0 additions & 1 deletion poem/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ yaml = ["serde_yaml"]
[dependencies]
poem-derive.workspace = true

async-trait = "0.1.51"
bytes.workspace = true
futures-util = { workspace = true, features = ["sink"] }
http = "1.0.0"
Expand Down
1 change: 0 additions & 1 deletion poem/src/i18n/locale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ impl Locale {
}
}

#[async_trait::async_trait]
impl<'a> FromRequest<'a> for Locale {
async fn from_request(req: &'a Request, _body: &mut RequestBody) -> Result<Self> {
let resources = req
Expand Down
1 change: 0 additions & 1 deletion poem/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ mod route;
mod server;

pub use addr::Addr;
pub use async_trait::async_trait;
pub use body::Body;
pub use endpoint::{Endpoint, EndpointExt, IntoEndpoint};
pub use error::{Error, Result};
Expand Down
1 change: 0 additions & 1 deletion poem/src/session/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ impl Session {
}
}

#[async_trait::async_trait]
impl<'a> FromRequest<'a> for &'a Session {
async fn from_request(req: &'a Request, _body: &mut RequestBody) -> Result<Self> {
Ok(req
Expand Down
1 change: 0 additions & 1 deletion poem/src/web/accept.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ fn parse_accept(headers: &HeaderMap) -> Vec<Mime> {
items.into_iter().map(|(mime, _)| mime).collect()
}

#[async_trait::async_trait]
impl<'a> FromRequest<'a> for Accept {
async fn from_request(req: &'a Request, _body: &mut RequestBody) -> Result<Self> {
Ok(Self(parse_accept(req.headers())))
Expand Down
2 changes: 0 additions & 2 deletions poem/src/web/cookie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,6 @@ impl Cookie {
}
}

#[async_trait::async_trait]
impl<'a> FromRequest<'a> for Cookie {
async fn from_request(req: &'a Request, _body: &mut RequestBody) -> Result<Self> {
let value = req
Expand Down Expand Up @@ -501,7 +500,6 @@ impl FromStr for CookieJar {
}
}

#[async_trait::async_trait]
impl<'a> FromRequest<'a> for &'a CookieJar {
async fn from_request(req: &'a Request, _body: &mut RequestBody) -> Result<Self> {
Ok(req.cookie())
Expand Down
2 changes: 0 additions & 2 deletions poem/src/web/csrf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ impl Deref for CsrfToken {
}
}

#[async_trait::async_trait]
impl<'a> FromRequest<'a> for &'a CsrfToken {
async fn from_request(req: &'a Request, _body: &mut RequestBody) -> Result<Self> {
Ok(req
Expand Down Expand Up @@ -49,7 +48,6 @@ impl CsrfVerifier {
}
}

#[async_trait::async_trait]
impl<'a> FromRequest<'a> for &'a CsrfVerifier {
async fn from_request(req: &'a Request, _body: &mut RequestBody) -> Result<Self> {
Ok(req
Expand Down
Loading

0 comments on commit f6611ba

Please sign in to comment.