1
0
Fork 0

Simplify spawn_unchecked lifetime transmute

This commit is contained in:
Robbert van der Helm 2022-02-02 12:39:23 +01:00
parent 58736f5cc8
commit 6cde9cea0e

View file

@ -50,12 +50,9 @@ impl ThreadSpawnUnchecked for std::thread::Builder {
F: FnOnce() -> R + Send,
R: 'static + Send,
{
let f: Box<dyn Send + FnOnce() -> R> = Box::new(f);
// ^ No need for `'_` inside function bodies
// v but this is more readable
let _: &Box<dyn '_ + Send + FnOnce() -> R> = &f;
// Safety: same-layout since only a lifetime difference
let f: Box<dyn 'static + Send + FnOnce() -> R> = std::mem::transmute(f);
let f: Box<dyn '_ + FnOnce() -> R + Send> = Box::new(f);
// SAFETY: same-layout since only a lifetime difference
let f: Box<dyn 'static + FnOnce() -> R + Send> = std::mem::transmute(f);
self.spawn(f)
}
}