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
use std::marker::PhantomData;
use std::ptr;
use crate::with_offset::WithOffset;
/// 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> {
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`].
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`].
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() }
}
/// # Safety
///
/// `this` must have been returned from [`WithOffset::into_ffi_safe`].
pub unsafe fn from_with_offset(this: WithOffset<*const FFISafe<'a, T>>) -> WithOffset<&'a T> {
// SAFETY: We required that the caller created `this` using `into_ffi_safe`, which uses `FFISafe::new`.
this.map(|data| unsafe { FFISafe::get(data) })
}
}
impl<'a, T> WithOffset<&'a T> {
/// Convert `self` into an FFI-safe type.
///
/// LLVM is able to better optimize the resulting type than a `*const FFISafe<'a, WithOffset<...>>`.
pub fn into_ffi_safe(self) -> WithOffset<*const FFISafe<'a, T>> {
self.map(FFISafe::new)
}
}