poem icon indicating copy to clipboard operation
poem copied to clipboard

A higher-ranked lifetime error in route handler.

Open importcjj opened this issue 2 years ago • 1 comments

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:

https://github.com/dtolnay/async-trait/issues/215 https://github.com/rust-lang/rust/issues/64552

importcjj avatar Oct 27 '22 10:10 importcjj

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.

Richardn2002 avatar Mar 25 '24 14:03 Richardn2002