Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A higher-ranked lifetime error in route handler. #417

Open
importcjj opened this issue Oct 27, 2022 · 1 comment
Open

A higher-ranked lifetime error in route handler. #417

importcjj opened this issue Oct 27, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@importcjj
Copy link

Expected Behavior

Compile successfully

Actual Behavior

Failed, got:

higher-ranked lifetime error
could not prove 


for<'r, 's, 't0, 't1> std::pin::Pin<std::boxed::Box<impl std::future::Future<Output = std::result::Result<poem::Response, poem::Error>>>>: 
std::ops::CoerceUnsized<std::pin::Pin<std::boxed::Box<(dyn std::future::Future<Output = std::result::Result<poem::Response, poem::Error>> + std::marker::Send + 't1)>>>

Steps to Reproduce the Problem

use poem::{
    get, handler,
    listener::TcpListener,
    middleware::AddData,
    web::{Data, Json},
    EndpointExt, Route, Server,
};

#[derive(Clone)]
pub struct State {
    pub db: MyDb,
}

#[derive(Clone)]
pub struct MyDb;

impl<'e> Executor<'e> for &'e MyDb {
    type ExecuteResult = ();
}

pub trait Executor<'e>: Send + Sized {
    type ExecuteResult;

    fn execute(self) -> futures::future::BoxFuture<'e, Result<Self::ExecuteResult, anyhow::Error>> {
        unimplemented!()
    }
}

pub async fn save<'e, E>(conn: E) -> anyhow::Result<()>
where
    E: Executor<'e>,
{
    conn.execute().await?;
    Ok(())
}

#[handler]
async fn insert(state: Data<&State>) -> Json<serde_json::Value> {
    save(&state.db).await.unwrap();

    Json(serde_json::json!(1))
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {


    Ok(())
}

Specifications

  • Version: 1.3.48
  • Platform: stable-aarch64-apple-darwin
  • Subsystem:

Relevant issues:

dtolnay/async-trait#215
rust-lang/rust#64552

@importcjj importcjj added the bug Something isn't working label Oct 27, 2022
@Richardn2002
Copy link

Richardn2002 commented Mar 25, 2024

Also encountered higher-ranked lifetime error, in #[OpenApi] macro. Sorry that I cannot provide the exact code, but to summarize:

  • One API endpoint calls an async library function with parameter &[Something]. Everything fine.
  • For more flexibility, the parameter type is later changed into impl Iterator<Item = Something>. Then the lifetime error appeared.

Update: It is further located that creating a Peekable with .peek() out of the iterator will lead to the error, otherwise fine. Related. At this stage I am starting to assume that it is my code that has a lifetime problem, but after some macro magic the error somehow get into poem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants