| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
//! FfiDefault trait |
| 6 |
//! |
| 7 |
//! When we make a FFI call into Rust we always need to return a value, even if that value will be |
| 8 |
//! ignored because we're flagging an exception. This trait defines what that value is for our |
| 9 |
//! supported FFI types. |
| 10 |
|
| 11 |
pub trait FfiDefault { |
| 12 |
fn ffi_default() -> Self; |
| 13 |
} |
| 14 |
|
| 15 |
// Most types can be handled by delegating to Default |
| 16 |
macro_rules! impl_ffi_default_with_default { |
| 17 |
($($T:ty,)+) => { impl_ffi_default_with_default!($($T),+); }; |
| 18 |
($($T:ty),*) => { |
| 19 |
$( |
| 20 |
impl FfiDefault for $T { |
| 21 |
fn ffi_default() -> Self { |
| 22 |
<$T>::default() |
| 23 |
} |
| 24 |
} |
| 25 |
)* |
| 26 |
}; |
| 27 |
} |
| 28 |
|
| 29 |
impl_ffi_default_with_default! { |
| 30 |
bool, i8, u8, i16, u16, i32, u32, i64, u64, f32, f64 |
| 31 |
} |
| 32 |
|
| 33 |
// Implement FfiDefault for the remaining types |
| 34 |
impl FfiDefault for () { |
| 35 |
fn ffi_default() {} |
| 36 |
} |
| 37 |
|
| 38 |
impl FfiDefault for crate::Handle { |
| 39 |
fn ffi_default() -> Self { |
| 40 |
Self::default() |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
impl FfiDefault for *const std::ffi::c_void { |
| 45 |
fn ffi_default() -> Self { |
| 46 |
std::ptr::null() |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
impl FfiDefault for crate::RustBuffer { |
| 51 |
fn ffi_default() -> Self { |
| 52 |
unsafe { Self::from_raw_parts(std::ptr::null_mut(), 0, 0) } |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
impl FfiDefault for crate::ForeignFuture { |
| 57 |
fn ffi_default() -> Self { |
| 58 |
extern "C" fn free(_handle: u64) {} |
| 59 |
crate::ForeignFuture { handle: 0, free } |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
impl<T> FfiDefault for Option<T> { |
| 64 |
fn ffi_default() -> Self { |
| 65 |
None |
| 66 |
} |
| 67 |
} |