php_tokio/
borrow_unchecked.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Borrowed from https://github.com/jeremyBanks/you-can

// What's going on here? Unsafe borrows???
// NO: this is actually 100% safe, and here's why.
//
// This is needed because of https://github.com/danog/php-tokio/blob/master/src/event_loop.rs#L72
//
// Rust thinks we're Sending the Future to another thread (tokio's event loop),
// where it may be used even after its lifetime expires in the main (PHP) thread.
//
// In reality, the Future is only used by Tokio until the result is ready.
//
// Rust does not understand that when we suspend the current fiber in suspend_on,
// we basically keep alive the the entire stack,
// including the Rust stack and the Future on it, until the result of the future is ready.
//
// Once the result of the Future is ready, tokio doesn't need it anymore,
// the suspend_on function is resumed, and we safely drop the Future upon exiting.

use ext_php_rs::binary_slice::{BinarySlice, PackSlice};

#[inline(always)]
pub unsafe fn borrow_unchecked<
    'original,
    'unbounded,
    Ref: BorrowUnchecked<'original, 'unbounded>,
>(
    reference: Ref,
) -> Ref::Unbounded {
    unsafe { BorrowUnchecked::borrow_unchecked(reference) }
}

#[doc(hidden)]
pub unsafe trait BorrowUnchecked<'original, 'unbounded> {
    type Unbounded;

    unsafe fn borrow_unchecked(self) -> Self::Unbounded;
}

unsafe impl<'original, 'unbounded, T: 'unbounded> BorrowUnchecked<'original, 'unbounded>
    for &'original T
{
    type Unbounded = &'unbounded T;

    #[inline(always)]
    unsafe fn borrow_unchecked(self) -> Self::Unbounded {
        unsafe { ::core::mem::transmute(self) }
    }
}

unsafe impl<'original, 'unbounded, T: 'unbounded> BorrowUnchecked<'original, 'unbounded>
    for &'original mut T
{
    type Unbounded = &'unbounded mut T;

    #[inline(always)]
    unsafe fn borrow_unchecked(self) -> Self::Unbounded {
        unsafe { ::core::mem::transmute(self) }
    }
}

unsafe impl<'original, 'unbounded, T: 'unbounded + PackSlice> BorrowUnchecked<'original, 'unbounded>
    for BinarySlice<'original, T>
{
    type Unbounded = BinarySlice<'unbounded, T>;

    #[inline(always)]
    unsafe fn borrow_unchecked(self) -> Self::Unbounded {
        unsafe { ::core::mem::transmute(self) }
    }
}