Skip to content

Commit

Permalink
Added some safety documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
DelSkayn committed Jun 2, 2023
1 parent e1a33f0 commit 1f08348
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions core/src/context/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,17 @@ macro_rules! async_with{
let fut = Box::pin(async move {
$($t)*
});
/// SAFETY: While rquickjs objects have a 'js lifetime attached to them,
/// they actually life much longer an the lifetime is just for checking
/// if they belong to the correct context.
/// By requiring that everything is moved into the closure outside
/// environments still can't life shorter than the closure.
/// This allows use to recast the future to a higher lifetime without problems.
/// Second, the future will always aquire a lock before running. The closure
/// enforces that everything moved into the future is send, but non of the
/// rquickjs objects are send so the future will never be send.
/// Since we aquire a lock before running the future and nothing can escape the closure
/// and future it is safe to recast the future as send.
unsafe fn uplift<'a,'b,R>(f: std::pin::Pin<Box<dyn std::future::Future<Output = R> + 'a>>) -> std::pin::Pin<Box<dyn std::future::Future<Output = R> + 'b + Send>>{
std::mem::transmute(f)
}
Expand Down

0 comments on commit 1f08348

Please sign in to comment.