What to use as a minimal meaningless return type for a hook? #275
-
With my World I am creating some temporary files and directories (tempfile crate) and in some cases these files are kept and persisted. I don't want to wait for my OS's temporary file cleaner to delete them. I would like to clean these up manually with the I wrote some code which works and does what I want, but I don't know exactly what to return, so I put something like a mirosleep there because I saw it in the documentation. But if possible, I would like to construct a minimal meaningless Pin-Box-Future-Output and return that, kind of like we return #[tokio::main]
async fn main() {
World::cucumber()
.after(|_feature, _rule, _scenario, _event, world| {
/* my code cleaning the persistent temp files omitted here */
// fn after() must return a Pin<Box<dyn futures::Future<Output = ()>>>
// and I don't know how to construct such a thing.
// Instead I use a very short sleep, in a way I found in cucumber documentation
time::sleep(Duration::from_micros(1)).boxed_local()
})
.run("tests/features/file_checks.feature")
.await;
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
#[tokio::main]
async fn main() {
World::cucumber()
.after(|_feature, _rule, _scenario, _event, world| async {
// ^^^^^
/* my code cleaning the persistent temp files omitted here */
}.boxed_local())
// ^^^^^^^^^^
.run("tests/features/file_checks.feature")
.await;
}
#[tokio::main]
async fn main() {
World::cucumber()
.after(|_feature, _rule, _scenario, _event, world| {
/* my code cleaning the persistent temp files omitted here */
// fn after() must return a Pin<Box<dyn futures::Future<Output = ()>>>
// and I don't know how to construct such a thing.
future::ready(()).boxed_local()
})
.run("tests/features/file_checks.feature")
.await;
} |
Beta Was this translation helpful? Give feedback.
Cucumber::after
hook receives a closure that has return type ofPin<Box<dyn Future + 'a>>
, which basically means anyFuture
allocated on the heap. This is done to support returnedFuture
s of any stack size. From what I can see in your example, all code in the hook is synchronous (doesn't have.await
s) and you are trying to bridge synchronous and asynchronous code. I suggest doing it in 1 of 2 ways:async { ... }
block and add.boxed_local()
at the end: