Skip to content

Commit

Permalink
DX(fangs): alter FangProc::bite FangAction::{fore, back} interfac…
Browse files Browse the repository at this point in the history
…e to enable rust-analyzer's `async fn ...` completion (#371)
  • Loading branch information
kanarus authored Feb 14, 2025
1 parent 837d620 commit d8ed11e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
41 changes: 37 additions & 4 deletions ohkami/src/fang/middleware/util.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::super::{Fang, FangProc};
use super::super::bound::{SendSyncOnNative, SendOnNativeFuture};
use super::super::bound::SendSyncOnNative;
use crate::{Request, Response};
use std::future::Future;

#[cfg(feature="openapi")]
use crate::openapi;
Expand Down Expand Up @@ -67,21 +68,52 @@ use crate::openapi;
/// }
/// ```
pub trait FangAction: Clone + SendSyncOnNative + 'static {
// Here not using `-> impl SendOnNativeFuture` for
// rust-analyzer's completion.
// Currently rust-analyzer can complete `-> Future` methods
// as `async fn ...` **only when** it returns exactly one of:
//
// * `-> impl Future<Output = T>`
// * `-> impl Future<Output = T> + Send`
// * `-> impl Future<Output = T> + Send + 'lifetime`
//
// so `-> impl SendOnNativeFuture<T>` prevents his completion...

#[cfg(any(feature="rt_worker",))]
/// *fore fang*, that bites a request before a handler.
///
/// ### default
/// just return `Ok(())`
#[allow(unused_variables)]
fn fore<'a>(&'a self, req: &'a mut Request) -> impl SendOnNativeFuture<Result<(), Response>> {
fn fore<'a>(&'a self, req: &'a mut Request) -> impl Future<Output = Result<(), Response>> {
async {Ok(())}
}
#[cfg(not(any(feature="rt_worker",)))]
/// *fore fang*, that bites a request before a handler.
///
/// ### default
/// just return `Ok(())`
#[allow(unused_variables)]
fn fore<'a>(&'a self, req: &'a mut Request) -> impl Future<Output = Result<(), Response>> + Send {
async {Ok(())}
}

/// *back fang*, that bites a response after a handler.
#[cfg(any(feature="rt_worker",))]
/// *back fang*, that bites a generated response.
///
/// ### default
/// just return `()`
#[allow(unused_variables)]
fn back<'a>(&'a self, res: &'a mut Response) -> impl SendOnNativeFuture<()> {
fn back<'a>(&'a self, res: &'a mut Response) -> impl Future<Output = ()> {
async {}
}
#[cfg(not(any(feature="rt_worker",)))]
/// *back fang*, that bites a generated response.
///
/// ### default
/// just return `()`
#[allow(unused_variables)]
fn back<'a>(&'a self, res: &'a mut Response) -> impl Future<Output = ()> + Send {
async {}
}

Expand All @@ -90,6 +122,7 @@ pub trait FangAction: Clone + SendSyncOnNative + 'static {
operation
}
}

const _: () = {
impl<A: FangAction, I: FangProc> Fang<I> for A {
type Proc = FangActionProc<A, I>;
Expand Down
19 changes: 17 additions & 2 deletions ohkami/src/fang/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod bound;
use bound::*;

use crate::{Request, Response};
use std::{pin::Pin, ops::Deref};
use std::{pin::Pin, ops::Deref, future::Future};

#[cfg(feature="openapi")]
use crate::openapi;
Expand Down Expand Up @@ -65,7 +65,21 @@ pub trait Fang<Inner: FangProc> {
}

pub trait FangProc: SendSyncOnNative + 'static {
fn bite<'b>(&'b self, req: &'b mut Request) -> impl SendOnNativeFuture<Response>;
// Here not using `-> impl SendOnNativeFuture` for
// rust-analyzer's completion.
// Currently rust-analyzer can complete `-> Future` methods
// as `async fn ...` **only when** it returns exactly one of:
//
// * `-> impl Future<Output = T>`
// * `-> impl Future<Output = T> + Send`
// * `-> impl Future<Output = T> + Send + 'lifetime`
//
// so `-> impl SendOnNativeFuture<T>` prevents his completion...

#[cfg(any(feature="rt_worker",))]
fn bite<'b>(&'b self, req: &'b mut Request) -> impl Future<Output = Response>;
#[cfg(not(any(feature="rt_worker",)))]
fn bite<'b>(&'b self, req: &'b mut Request) -> impl Future<Output = Response> + Send;

/// Mainly used for override `bite` when itself returns `Pin<Box<dyn Future>>`.
///
Expand Down Expand Up @@ -114,6 +128,7 @@ const _: () = {
}

impl FangProc for BoxedFPC {
#[allow(refining_impl_trait)]
#[inline(always)]
fn bite<'b>(&'b self, req: &'b mut Request) -> impl SendOnNativeFuture<Response> {
self.0.call_bite(req)
Expand Down

0 comments on commit d8ed11e

Please sign in to comment.