Debian Codegraph @

third_party/rust/uniffi_macros/src/remote.rs

raw
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 use proc_macro2::TokenStream;
6 use quote::quote;
7 use syn::{
8 parse::{Parse, ParseStream},
9 Ident, Token, Type,
10 };
11
12 pub struct RemoteTypeArgs {
13 pub implementing_crate: Ident,
14 pub sep: Token![::],
15 pub ty: Type,
16 }
17
18 impl Parse for RemoteTypeArgs {
19 fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
20 Ok(Self {
21 implementing_crate: input.parse()?,
22 sep: input.parse()?,
23 ty: input.parse()?,
24 })
25 }
26 }
27
28 pub fn expand_remote_type(args: RemoteTypeArgs) -> TokenStream {
29 let RemoteTypeArgs {
30 implementing_crate,
31 ty,
32 ..
33 } = args;
34 let existing_tag = quote! { #implementing_crate::UniFfiTag };
35
36 quote! {
37 unsafe impl ::uniffi::FfiConverter<crate::UniFfiTag> for #ty {
38 type FfiType = <#ty as ::uniffi::FfiConverter<#existing_tag>>::FfiType;
39
40 fn lower(obj: #ty) -> Self::FfiType {
41 <#ty as ::uniffi::FfiConverter<#existing_tag>>::lower(obj)
42 }
43
44 fn try_lift(v: Self::FfiType) -> ::uniffi::Result<#ty> {
45 <#ty as ::uniffi::FfiConverter<#existing_tag>>::try_lift(v)
46 }
47
48 fn write(obj: #ty, buf: &mut Vec<u8>) {
49 <#ty as ::uniffi::FfiConverter<#existing_tag>>::write(obj, buf)
50 }
51
52 fn try_read(buf: &mut &[u8]) -> ::uniffi::Result<#ty> {
53 <#ty as ::uniffi::FfiConverter<#existing_tag>>::try_read(buf)
54 }
55
56 const TYPE_ID_META: ::uniffi::MetadataBuffer =
57 <#ty as ::uniffi::FfiConverter<#existing_tag>>::TYPE_ID_META;
58 }
59
60 ::uniffi::derive_ffi_traits!(local #ty);
61 }
62 }