Would love some advice/mentorship!

Hello,

I've been working on an open source project, thruster, for a bit over a year now. It's been fun and informative, for the most part, but I've reached a problem that I haven't been able to solve for weeks.

I'm trying to provide a way for developers to use the new async/await functionality, as I believe it will really clean up the code and make the library more useable. I'm having a LOT of trouble with adopting the new futures to my old code though. I'd love some help in where to go on this PR that fails to compile.

The TLDR of it is;

Old code used to look like this:

fn log(context: BasicContext, next: impl Fn(BasicContext) -> MiddlewareReturnValue<BasicContext>  + Send + Sync) -> MiddlewareReturnValue<BasicContext> {
  println!("start request");

  next(context)
    .then(|context| {
      println!("end request");
      future::ok(context)
    })
}

fn plaintext(mut context: Ctx, next: impl Fn(Ctx) -> MiddlewareReturnValue<Ctx>  + Send + Sync) -> MiddlewareReturnValue<Ctx> {
  let val = "Hello, World!";
  context.body(val);

  Box::new(future::ok(context))
}

fn main() {
  println!("Starting server...");

  let mut app = App::<Request, Ctx>::new_basic();

  app.get("/plaintext", middleware![BasicContext => log, Ctx => plaintext]);

  let server = Server::new(app);
  server.start("0.0.0.0", 4321);
}

New code would look like this:

async fn log(context: Ctx, next: impl Fn(Ctx) -> MiddlewareReturnValue<Ctx>  + Send + Sync) -> Ctx {
  println!("start request");

  let context = await!(next(context));

  println!("end request");
  context
}

async fn plaintext(mut context: Ctx, next: impl Fn(Ctx) -> MiddlewareReturnValue<Ctx>  + Send + Sync) -> Ctx {
  let val = "Hello, World!";
  context.body(val);

  context
}

fn main() {
  println!("Starting server...");

  let mut app = App::<Request, Ctx>::new_basic();

  app.get("/plaintext", async_middleware!([log, plaintext]));

  let server = Server::new(app);
  server.start("0.0.0.0", 4321);
}

I believe the issue I'm having is with lifetimes captured in next functions, but I'd really love some help/guidance on how I can resolve these issues.

Thanks!!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.