• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 use paste::paste;
12 
13 pub trait FfiDefault {
ffi_default() -> Self14     fn ffi_default() -> Self;
15 }
16 
17 // Most types can be handled by delegating to Default
18 macro_rules! impl_ffi_default_with_default {
19     ($($T:ty,)+) => { impl_ffi_default_with_default!($($T),+); };
20     ($($T:ty),*) => {
21             $(
22                 paste! {
23                     impl FfiDefault for $T {
24                         fn ffi_default() -> Self {
25                             $T::default()
26                         }
27                     }
28                 }
29             )*
30     };
31 }
32 
33 impl_ffi_default_with_default! {
34     bool, i8, u8, i16, u16, i32, u32, i64, u64, f32, f64
35 }
36 
37 // Implement FfiDefault for the remaining types
38 impl FfiDefault for () {
ffi_default()39     fn ffi_default() {}
40 }
41 
42 impl FfiDefault for crate::Handle {
ffi_default() -> Self43     fn ffi_default() -> Self {
44         Self::default()
45     }
46 }
47 
48 impl FfiDefault for *const std::ffi::c_void {
ffi_default() -> Self49     fn ffi_default() -> Self {
50         std::ptr::null()
51     }
52 }
53 
54 impl FfiDefault for crate::RustBuffer {
ffi_default() -> Self55     fn ffi_default() -> Self {
56         unsafe { Self::from_raw_parts(std::ptr::null_mut(), 0, 0) }
57     }
58 }
59 
60 impl FfiDefault for crate::ForeignFuture {
ffi_default() -> Self61     fn ffi_default() -> Self {
62         extern "C" fn free(_handle: u64) {}
63         crate::ForeignFuture { handle: 0, free }
64     }
65 }
66 
67 impl<T> FfiDefault for Option<T> {
ffi_default() -> Self68     fn ffi_default() -> Self {
69         None
70     }
71 }
72