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
use std::marker::PhantomData;
use std::ptr;
/// A type that bypasses `#[warn(improper_ctypes)]` checks of FFI safe types.
/// This type is meant to roundtrip a reference to a type `T` with lifetime `'a`
/// through an `extern "C" fn` ptr from a Rust caller to Rust callee.
/// Non-Rust callees should not access this type.
#[repr(C)]
pub struct FFISafe<'a, T> {
phantom: PhantomData<&'a T>,
non_zst: bool,
}
impl<'a, T> FFISafe<'a, T> {
#[cfg(asm_fn_ptrs)]
pub fn new(this: &'a T) -> *const Self {
ptr::from_ref(this).cast()
}
pub fn _new_mut(this: &'a mut T) -> *mut Self {
ptr::from_mut(this).cast()
}
/// # Safety
///
/// `this` must have been returned from [`Self::new`].
#[cfg(asm_fn_ptrs)]
#[allow(unsafe_code)]
pub unsafe fn get(this: *const Self) -> &'a T {
// SAFETY: `this` originally was a `&'a T` in `Self::new`.
unsafe { &*this.cast() }
}
/// # Safety
///
/// `this` must have been returned from [`Self::new_mut`].
#[cfg(any(asm_fn_ptrs, feature = "c-ffi"))]
#[allow(unsafe_code)]
pub unsafe fn _get_mut(this: *mut Self) -> &'a mut T {
// SAFETY: `this` originally was a `&'a mut T` in `Self::new_mut`.
unsafe { &mut *this.cast() }
}
}