| 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 |
//! Functions, types and expressions to handle FFI operations. |
| 6 |
//! |
| 7 |
//! This module leverages the various traits defined in `uniffi_core::ffi_converter_traits` to provide functionality to the rest of `uniffi_macros`. |
| 8 |
//! Keeping this layer separate makes it easier to handle changes to those traits. |
| 9 |
//! See `uniffi_core::ffi_converter_traits` for the meaning of these functions. |
| 10 |
|
| 11 |
use proc_macro2::TokenStream; |
| 12 |
use quote::{quote, ToTokens}; |
| 13 |
|
| 14 |
// Lower function |
| 15 |
pub fn lower(ty: impl ToTokens) -> TokenStream { |
| 16 |
quote! { |
| 17 |
<#ty as ::uniffi::Lower<crate::UniFfiTag>>::lower |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
// Lift type |
| 22 |
pub fn lift_type(ty: impl ToTokens) -> TokenStream { |
| 23 |
quote! { |
| 24 |
<#ty as ::uniffi::Lift<crate::UniFfiTag>>::FfiType |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
// Lift function |
| 29 |
pub fn try_lift(ty: impl ToTokens) -> TokenStream { |
| 30 |
quote! { |
| 31 |
<#ty as ::uniffi::Lift<crate::UniFfiTag>>::try_lift |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
/// Write function |
| 36 |
pub fn write(ty: impl ToTokens) -> TokenStream { |
| 37 |
quote! { |
| 38 |
<#ty as ::uniffi::Lower<crate::UniFfiTag>>::write |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
/// Read function |
| 43 |
pub fn try_read(ty: impl ToTokens) -> TokenStream { |
| 44 |
quote! { |
| 45 |
<#ty as ::uniffi::Lift<crate::UniFfiTag>>::try_read |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
/// Lower return type |
| 50 |
pub fn lower_return_type(ty: impl ToTokens) -> TokenStream { |
| 51 |
quote! { |
| 52 |
<#ty as ::uniffi::LowerReturn<crate::UniFfiTag>>::ReturnType |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/// Lower return function |
| 57 |
pub fn lower_return(ty: impl ToTokens) -> TokenStream { |
| 58 |
quote! { |
| 59 |
<#ty as ::uniffi::LowerReturn<crate::UniFfiTag>>::lower_return |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
/// Lower error function |
| 64 |
pub fn lower_error(ty: impl ToTokens) -> TokenStream { |
| 65 |
quote! { |
| 66 |
<#ty as ::uniffi::LowerError<crate::UniFfiTag>>::lower_error |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/// Lift return type |
| 71 |
pub fn lift_return_type(ty: impl ToTokens) -> TokenStream { |
| 72 |
quote! { |
| 73 |
<#ty as ::uniffi::LiftReturn<crate::UniFfiTag>>::ReturnType |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
/// Lift foreign return function |
| 78 |
pub fn lift_foreign_return(ty: impl ToTokens) -> TokenStream { |
| 79 |
quote! { |
| 80 |
<#ty as ::uniffi::LiftReturn<crate::UniFfiTag>>::lift_foreign_return |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
/// Handle failed lift function |
| 85 |
pub fn lower_return_handle_failed_lift(ty: impl ToTokens) -> TokenStream { |
| 86 |
quote! { |
| 87 |
<#ty as ::uniffi::LowerReturn<crate::UniFfiTag>>::handle_failed_lift |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
/// LiftRef type |
| 92 |
pub fn lift_ref_type(ty: impl ToTokens) -> TokenStream { |
| 93 |
quote! { <#ty as ::uniffi::LiftRef<crate::UniFfiTag>>::LiftType } |
| 94 |
} |
| 95 |
|
| 96 |
/// Lower into rust buffer function |
| 97 |
pub fn lower_into_rust_buffer(ty: impl ToTokens) -> TokenStream { |
| 98 |
quote! { |
| 99 |
<#ty as ::uniffi::Lower<crate::UniFfiTag>>::lower_into_rust_buffer |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
/// Lift from rust buffer function |
| 104 |
pub fn try_lift_from_rust_buffer(ty: impl ToTokens) -> TokenStream { |
| 105 |
quote! { |
| 106 |
<#ty as ::uniffi::Lift<crate::UniFfiTag>>::try_lift_from_rust_buffer |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/// Expression for the TYPE_ID_META value for a type |
| 111 |
pub fn type_id_meta(ty: impl ToTokens) -> TokenStream { |
| 112 |
quote! { |
| 113 |
<#ty as ::uniffi::TypeId<crate::UniFfiTag>>::TYPE_ID_META |
| 114 |
} |
| 115 |
} |