uniffi_core/ffi_converter_traits.rs
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//! Traits that define how to transfer values via the FFI layer.
6//!
7//! These traits define how to pass values over the FFI in various ways: as arguments or as return
8//! values, from Rust to the foreign side and vice-versa. These traits are mainly used by the
9//! proc-macro generated code. The goal is to allow the proc-macros to go from a type name to the
10//! correct function for a given FFI operation.
11//!
12//! The main traits form a sort-of tree structure from general to specific:
13//! ```ignore
14//!
15//! [FfiConverter]
16//! |
17//! -----------------------------------------
18//! | |
19//! [Lower] [Lift]
20//! | |
21//! ----------------- --------------
22//! | | | |
23//! [LowerReturn] [LowerError] [LiftRef] [LiftReturn]
24//! ```
25//!
26//! There's also:
27//! - [TypeId], which is implemented for all types that implement any of the above traits.
28//! - [ConvertError], which is implement for errors that can be used in callback interfaces.
29//!
30//! The `derive_ffi_traits` macro can be used to derive the specific traits from the general ones.
31//! Here's the main ways we implement these traits:
32//!
33//! * For most types we implement [FfiConverter] and use [derive_ffi_traits] to implement the rest
34//! * If a type can only be lifted/lowered, then we implement [Lift] or [Lower] and use
35//! [derive_ffi_traits] to implement the rest
36//! * If a type needs special-case handling, like `Result<>` and `()`, we implement the traits
37//! directly.
38//!
39//! FfiConverter has a generic parameter, that's filled in with a type local to the UniFFI consumer crate.
40//! This allows us to work around the Rust orphan rules for remote types. See
41//! `https://mozilla.github.io/uniffi-rs/internals/lifting_and_lowering.html#code-generation-and-the-fficonverter-trait`
42//! for details.
43//!
44//! ## Safety
45//!
46//! Most traits are unsafe (implementing it requires `unsafe impl`) because we can't guarantee
47//! that it's safe to pass your type out to foreign-language code and back again. Buggy
48//! implementations of this trait might violate some assumptions made by the generated code,
49//! or might not match with the corresponding code in the generated foreign-language bindings.
50//! These traits should not be used directly, only in generated code, and the generated code should
51//! have fixture tests to test that everything works correctly together.
52
53use std::{borrow::Borrow, mem::ManuallyDrop, sync::Arc};
54
55use anyhow::bail;
56use bytes::Buf;
57
58use crate::{
59 FfiDefault, Handle, LiftArgsError, MetadataBuffer, Result, RustBuffer, RustCallError,
60 RustCallStatus, RustCallStatusCode, UnexpectedUniFFICallbackError,
61};
62
63/// Generalized FFI conversions
64///
65/// This trait is not used directly by the code generation, but implement this and calling
66/// [derive_ffi_traits] is a simple way to implement all the traits that are.
67///
68/// ## Safety
69///
70/// All traits are unsafe (implementing it requires `unsafe impl`) because we can't guarantee
71/// that it's safe to pass your type out to foreign-language code and back again. Buggy
72/// implementations of this trait might violate some assumptions made by the generated code,
73/// or might not match with the corresponding code in the generated foreign-language bindings.
74/// These traits should not be used directly, only in generated code, and the generated code should
75/// have fixture tests to test that everything works correctly together.
76pub unsafe trait FfiConverter<UT>: Sized {
77 /// The low-level type used for passing values of this type over the FFI.
78 ///
79 /// This must be a C-compatible type (e.g. a numeric primitive, a `#[repr(C)]` struct) into
80 /// which values of the target rust type can be converted.
81 ///
82 /// For complex data types, we currently recommend using `RustBuffer` and serializing
83 /// the data for transfer. In theory it could be possible to build a matching
84 /// `#[repr(C)]` struct for a complex data type and pass that instead, but explicit
85 /// serialization is simpler and safer as a starting point.
86 ///
87 /// If a type implements multiple FFI traits, `FfiType` must be the same for all of them.
88 type FfiType: FfiDefault;
89
90 /// Lower a rust value of the target type, into an FFI value of type Self::FfiType.
91 ///
92 /// This trait method is used for sending data from rust to the foreign language code,
93 /// by (hopefully cheaply!) converting it into something that can be passed over the FFI
94 /// and reconstructed on the other side.
95 ///
96 /// Note that this method takes an owned value; this allows it to transfer ownership in turn to
97 /// the foreign language code, e.g. by boxing the value and passing a pointer.
98 fn lower(obj: Self) -> Self::FfiType;
99
100 /// Lift a rust value of the target type, from an FFI value of type Self::FfiType.
101 ///
102 /// This trait method is used for receiving data from the foreign language code in rust,
103 /// by (hopefully cheaply!) converting it from a low-level FFI value of type Self::FfiType
104 /// into a high-level rust value of the target type.
105 ///
106 /// Since we cannot statically guarantee that the foreign-language code will send valid
107 /// values of type Self::FfiType, this method is fallible.
108 fn try_lift(v: Self::FfiType) -> Result<Self>;
109
110 /// Write a rust value into a buffer, to send over the FFI in serialized form.
111 ///
112 /// This trait method can be used for sending data from rust to the foreign language code,
113 /// in cases where we're not able to use a special-purpose FFI type and must fall back to
114 /// sending serialized bytes.
115 ///
116 /// Note that this method takes an owned value because it's transferring ownership
117 /// to the foreign language code via the RustBuffer.
118 fn write(obj: Self, buf: &mut Vec<u8>);
119
120 /// Read a rust value from a buffer, received over the FFI in serialized form.
121 ///
122 /// This trait method can be used for receiving data from the foreign language code in rust,
123 /// in cases where we're not able to use a special-purpose FFI type and must fall back to
124 /// receiving serialized bytes.
125 ///
126 /// Since we cannot statically guarantee that the foreign-language code will send valid
127 /// serialized bytes for the target type, this method is fallible.
128 ///
129 /// Note the slightly unusual type here - we want a mutable reference to a slice of bytes,
130 /// because we want to be able to advance the start of the slice after reading an item
131 /// from it (but will not mutate the actual contents of the slice).
132 fn try_read(buf: &mut &[u8]) -> Result<Self>;
133
134 /// Type ID metadata, serialized into a [MetadataBuffer].
135 const TYPE_ID_META: MetadataBuffer;
136}
137
138/// FfiConverter for Arc-types
139///
140/// This trait gets around the orphan rule limitations, which prevent library crates from
141/// implementing `FfiConverter` on an Arc. When this is implemented for T, we generate an
142/// `FfiConverter` impl for Arc<T>.
143///
144/// Note: There's no need for `FfiConverterBox`, since Box is a fundamental type.
145///
146/// ## Safety
147///
148/// All traits are unsafe (implementing it requires `unsafe impl`) because we can't guarantee
149/// that it's safe to pass your type out to foreign-language code and back again. Buggy
150/// implementations of this trait might violate some assumptions made by the generated code,
151/// or might not match with the corresponding code in the generated foreign-language bindings.
152/// These traits should not be used directly, only in generated code, and the generated code should
153/// have fixture tests to test that everything works correctly together.
154#[cfg(not(all(target_arch = "wasm32", feature = "wasm-unstable-single-threaded")))]
155pub unsafe trait FfiConverterArc<UT>: Send + Sync {
156 type FfiType: FfiDefault;
157
158 fn lower(obj: Arc<Self>) -> Self::FfiType;
159 fn try_lift(v: Self::FfiType) -> Result<Arc<Self>>;
160 fn write(obj: Arc<Self>, buf: &mut Vec<u8>);
161 fn try_read(buf: &mut &[u8]) -> Result<Arc<Self>>;
162
163 const TYPE_ID_META: MetadataBuffer;
164}
165
166/// ## Safety
167///
168/// The Safety notice is the same as for `FfiConverterArc`, but this is only used
169/// for WASM targets that are single-threaded. In this case, Send and Sync aren't
170/// necessary to check.
171#[cfg(all(target_arch = "wasm32", feature = "wasm-unstable-single-threaded"))]
172pub unsafe trait FfiConverterArc<UT> {
173 type FfiType: FfiDefault;
174
175 fn lower(obj: Arc<Self>) -> Self::FfiType;
176 fn try_lift(v: Self::FfiType) -> Result<Arc<Self>>;
177 fn write(obj: Arc<Self>, buf: &mut Vec<u8>);
178 fn try_read(buf: &mut &[u8]) -> Result<Arc<Self>>;
179
180 const TYPE_ID_META: MetadataBuffer;
181}
182
183unsafe impl<T, UT> FfiConverter<UT> for Arc<T>
184where
185 T: FfiConverterArc<UT> + ?Sized,
186{
187 type FfiType = T::FfiType;
188
189 fn lower(obj: Self) -> Self::FfiType {
190 T::lower(obj)
191 }
192
193 fn try_lift(v: Self::FfiType) -> Result<Self> {
194 T::try_lift(v)
195 }
196
197 fn write(obj: Self, buf: &mut Vec<u8>) {
198 T::write(obj, buf)
199 }
200
201 fn try_read(buf: &mut &[u8]) -> Result<Self> {
202 T::try_read(buf)
203 }
204
205 const TYPE_ID_META: MetadataBuffer = T::TYPE_ID_META;
206}
207
208/// Lift values passed by the foreign code over the FFI into Rust values
209///
210/// This is used by the code generation to handle arguments. It's usually derived from
211/// [FfiConverter], except for types that only support lifting but not lowering.
212///
213/// See [FfiConverter] for a discussion of the methods
214///
215/// ## Safety
216///
217/// All traits are unsafe (implementing it requires `unsafe impl`) because we can't guarantee
218/// that it's safe to pass your type out to foreign-language code and back again. Buggy
219/// implementations of this trait might violate some assumptions made by the generated code,
220/// or might not match with the corresponding code in the generated foreign-language bindings.
221/// These traits should not be used directly, only in generated code, and the generated code should
222/// have fixture tests to test that everything works correctly together.
223pub unsafe trait Lift<UT>: Sized {
224 type FfiType;
225
226 fn try_lift(v: Self::FfiType) -> Result<Self>;
227
228 fn try_read(buf: &mut &[u8]) -> Result<Self>;
229
230 /// Convenience method
231 fn try_lift_from_rust_buffer(v: RustBuffer) -> Result<Self> {
232 let vec = v.destroy_into_vec();
233 let mut buf = vec.as_slice();
234 let value = Self::try_read(&mut buf)?;
235 match Buf::remaining(&buf) {
236 0 => Ok(value),
237 n => bail!("junk data left in buffer after lifting (count: {n})",),
238 }
239 }
240}
241
242/// Lower Rust values to pass them to the foreign code
243///
244/// This is used to pass arguments to callback interfaces. It's usually derived from
245/// [FfiConverter], except for types that only support lowering but not lifting.
246///
247/// See [FfiConverter] for a discussion of the methods
248///
249/// ## Safety
250///
251/// All traits are unsafe (implementing it requires `unsafe impl`) because we can't guarantee
252/// that it's safe to pass your type out to foreign-language code and back again. Buggy
253/// implementations of this trait might violate some assumptions made by the generated code,
254/// or might not match with the corresponding code in the generated foreign-language bindings.
255/// These traits should not be used directly, only in generated code, and the generated code should
256/// have fixture tests to test that everything works correctly together.
257pub unsafe trait Lower<UT>: Sized {
258 type FfiType: FfiDefault;
259
260 fn lower(obj: Self) -> Self::FfiType;
261
262 fn write(obj: Self, buf: &mut Vec<u8>);
263
264 /// Convenience method
265 fn lower_into_rust_buffer(obj: Self) -> RustBuffer {
266 let mut buf = ::std::vec::Vec::new();
267 Self::write(obj, &mut buf);
268 RustBuffer::from_vec(buf)
269 }
270}
271
272/// Return Rust values to the foreign code
273///
274/// This is usually derived from [Lower], but we special case types like `Result<>` and `()`.
275///
276/// ## Safety
277///
278/// All traits are unsafe (implementing it requires `unsafe impl`) because we can't guarantee
279/// that it's safe to pass your type out to foreign-language code and back again. Buggy
280/// implementations of this trait might violate some assumptions made by the generated code,
281/// or might not match with the corresponding code in the generated foreign-language bindings.
282/// These traits should not be used directly, only in generated code, and the generated code should
283/// have fixture tests to test that everything works correctly together.
284pub unsafe trait LowerReturn<UT>: Sized {
285 /// The type that should be returned by scaffolding functions for this type.
286 ///
287 /// When derived, it's the same as `FfiType`.
288 type ReturnType: FfiDefault;
289
290 /// Lower the return value from an scaffolding call
291 ///
292 /// Returns values that [rust_call] expects:
293 ///
294 /// - Ok(v) for `Ok` returns and non-result returns, where v is the lowered return value
295 /// - `Err(RustCallError::Error(buf))` for `Err` returns where `buf` is serialized error value.
296 fn lower_return(v: Self) -> Result<Self::ReturnType, RustCallError>;
297
298 /// Lower the return value for failed argument lifts
299 ///
300 /// This is called when we fail to make a scaffolding call, because of an error lifting an
301 /// argument. It should return a value that [rust_call] expects:
302 ///
303 /// - By default, this is `Err(RustCallError::InternalError(msg))` where `msg` is message
304 /// describing the failed lift.
305 /// - For Result types, if we can downcast the error to the `Err` value, then return
306 /// `Err(RustCallError::Error(buf))`. This results in better exception throws on the foreign
307 /// side.
308 fn handle_failed_lift(error: LiftArgsError) -> Result<Self::ReturnType, RustCallError> {
309 Err(error.to_internal_error())
310 }
311}
312
313/// Return Rust error values
314///
315/// This is implemented for types that can be the `E` param in `Result<T, E>`.
316/// It's is usually derived from [Lower], but we sometimes special case it.
317///
318/// ## Safety
319///
320/// All traits are unsafe (implementing it requires `unsafe impl`) because we can't guarantee
321/// that it's safe to pass your type out to foreign-language code and back again. Buggy
322/// implementations of this trait might violate some assumptions made by the generated code,
323/// or might not match with the corresponding code in the generated foreign-language bindings.
324/// These traits should not be used directly, only in generated code, and the generated code should
325/// have fixture tests to test that everything works correctly together.
326pub unsafe trait LowerError<UT>: Sized {
327 /// Lower this value for scaffolding function return
328 ///
329 /// Lower the type into a RustBuffer. `RustCallStatus.error_buf` will be set to this.
330 fn lower_error(obj: Self) -> RustBuffer;
331}
332
333/// Return foreign values to Rust
334///
335/// This is usually derived from [Lower], but we special case types like `Result<>` and `()`.
336///
337/// ## Safety
338///
339/// All traits are unsafe (implementing it requires `unsafe impl`) because we can't guarantee
340/// that it's safe to pass your type out to foreign-language code and back again. Buggy
341/// implementations of this trait might violate some assumptions made by the generated code,
342/// or might not match with the corresponding code in the generated foreign-language bindings.
343/// These traits should not be used directly, only in generated code, and the generated code should
344/// have fixture tests to test that everything works correctly together.
345pub unsafe trait LiftReturn<UT>: Sized {
346 /// FFI return type for trait interfaces
347 type ReturnType;
348
349 /// Lift a successfully returned value from a trait interface
350 fn try_lift_successful_return(v: Self::ReturnType) -> Result<Self>;
351
352 /// Lift a foreign returned value from a trait interface
353 ///
354 /// When we call a foreign-implemented trait interface method, we pass a &mut RustCallStatus
355 /// and get [Self::ReturnType] returned. This method takes both of those and lifts `Self` from
356 /// it.
357 fn lift_foreign_return(ffi_return: Self::ReturnType, call_status: RustCallStatus) -> Self {
358 match call_status.code {
359 RustCallStatusCode::Success => Self::try_lift_successful_return(ffi_return)
360 .unwrap_or_else(|e| {
361 Self::handle_callback_unexpected_error(UnexpectedUniFFICallbackError::new(e))
362 }),
363 RustCallStatusCode::Error => {
364 Self::lift_error(ManuallyDrop::into_inner(call_status.error_buf))
365 }
366 _ => {
367 let e = <String as FfiConverter<crate::UniFfiTag>>::try_lift(
368 ManuallyDrop::into_inner(call_status.error_buf),
369 )
370 .unwrap_or_else(|e| format!("(Error lifting message: {e}"));
371 Self::handle_callback_unexpected_error(UnexpectedUniFFICallbackError::new(e))
372 }
373 }
374 }
375
376 /// Lift a Rust value for a callback interface method error result
377 ///
378 /// This is called for "expected errors" -- the callback method returns a Result<> type and the
379 /// foreign code throws an exception that corresponds to the error type.
380 fn lift_error(_buf: RustBuffer) -> Self {
381 panic!("Callback interface method returned unexpected error")
382 }
383
384 /// Lift a Rust value for an unexpected callback interface error
385 ///
386 /// The main reason this is called is when the callback interface throws an error type that
387 /// doesn't match the Rust trait definition. It's also called for corner cases, like when the
388 /// foreign code doesn't follow the FFI contract.
389 ///
390 /// The default implementation panics unconditionally. Errors used in callback interfaces
391 /// handle this using the `From<UnexpectedUniFFICallbackError>` impl that the library author
392 /// must provide.
393 fn handle_callback_unexpected_error(e: UnexpectedUniFFICallbackError) -> Self {
394 panic!("Callback interface failure: {e}")
395 }
396}
397
398/// Lift references
399///
400/// This is usually derived from [Lift] and also implemented for the inner `T` value of smart
401/// pointers. For example, if `Lift` is implemented for `Arc<T>`, then we implement this to lift
402///
403/// ## Safety
404///
405/// All traits are unsafe (implementing it requires `unsafe impl`) because we can't guarantee
406/// that it's safe to pass your type out to foreign-language code and back again. Buggy
407/// implementations of this trait might violate some assumptions made by the generated code,
408/// or might not match with the corresponding code in the generated foreign-language bindings.
409/// These traits should not be used directly, only in generated code, and the generated code should
410/// have fixture tests to test that everything works correctly together.
411/// `&T` using the Arc.
412pub unsafe trait LiftRef<UT> {
413 type LiftType: Lift<UT> + Borrow<Self>;
414}
415
416/// Type ID metadata
417///
418/// This is used to build up more complex metadata. For example, the `MetadataBuffer` for function
419/// signatures includes a copy of this metadata for each argument and return type.
420pub trait TypeId<UT> {
421 const TYPE_ID_META: MetadataBuffer;
422}
423
424pub trait ConvertError<UT>: Sized {
425 fn try_convert_unexpected_callback_error(e: UnexpectedUniFFICallbackError) -> Result<Self>;
426}
427
428/// Manage handles for `Arc<Self>` instances
429///
430/// Handles are used to manage objects that are passed across the FFI. They general usage is:
431///
432/// * Rust creates an `Arc<>`
433/// * Rust uses `new_handle` to create a handle that represents the Arc reference
434/// * Rust passes the handle to the foreign code as a `u64`
435/// * The foreign code passes the handle back to `Rust` to refer to the object:
436/// * Handle are usually passed as borrowed values. When an FFI function inputs a handle as an
437/// argument, the foreign code simply passes a copy of the `u64` to Rust, which calls `get_arc`
438/// to get a new `Arc<>` clone for it.
439/// * Handles are returned as owned values. When an FFI function returns a handle, the foreign
440/// code either stops using the handle after returning it or calls `clone_handle` and returns
441/// the clone.
442/// * Eventually the foreign code may destroy their handle by passing it into a "free" FFI
443/// function. This functions input an owned handle and consume it.
444///
445/// The foreign code also defines their own handles. These represent foreign objects that are
446/// passed to Rust. Using foreign handles is essentially the same as above, but in reverse.
447///
448/// Handles must always be `Send` and the objects they reference must always be `Sync`.
449/// This means that it must be safe to send handles to other threads and use them there.
450///
451/// Note: this only needs to be derived for unsized types, there's a blanket impl for `T: Sized`.
452///
453/// ## Safety
454///
455/// All traits are unsafe (implementing it requires `unsafe impl`) because we can't guarantee
456/// that it's safe to pass your type out to foreign-language code and back again. Buggy
457/// implementations of this trait might violate some assumptions made by the generated code,
458/// or might not match with the corresponding code in the generated foreign-language bindings.
459/// These traits should not be used directly, only in generated code, and the generated code should
460/// have fixture tests to test that everything works correctly together.
461/// `&T` using the Arc.
462pub unsafe trait HandleAlloc<UT>: Send + Sync {
463 /// Create a new handle for an Arc value
464 ///
465 /// Use this to lower an Arc into a handle value before passing it across the FFI.
466 /// The newly-created handle will have reference count = 1.
467 fn new_handle(value: Arc<Self>) -> Handle;
468
469 /// Clone a handle
470 ///
471 /// This creates a new handle from an existing one.
472 /// It's used when the foreign code wants to pass back an owned handle and still keep a copy
473 /// for themselves.
474 /// # Safety
475 /// The handle must be valid.
476 unsafe fn clone_handle(handle: Handle) -> Handle;
477
478 /// Get a clone of the `Arc<>` using a "borrowed" handle.
479 ///
480 /// # Safety
481 /// The handle must be valid. Take care that the handle can
482 /// not be destroyed between when it's passed and when
483 /// `get_arc()` is called. #1797 is a cautionary tale.
484 unsafe fn get_arc(handle: Handle) -> Arc<Self> {
485 Self::consume_handle(Self::clone_handle(handle))
486 }
487
488 /// Consume a handle, getting back the initial `Arc<>`
489 /// # Safety
490 /// The handle must be valid.
491 unsafe fn consume_handle(handle: Handle) -> Arc<Self>;
492}
493
494/// Derive FFI traits
495///
496/// This can be used to derive:
497/// * [Lower] and [Lift] from [FfiConverter]
498/// * [LowerReturn] from [Lower]
499/// * [LiftReturn] and [LiftRef] from [Lift]
500///
501/// Usage:
502/// ```ignore
503///
504/// // Derive everything from [FfiConverter] for all Uniffi tags
505/// ::uniffi::derive_ffi_traits!(blanket Foo)
506/// // Derive everything from [FfiConverter] for the local crate::UniFfiTag
507/// ::uniffi::derive_ffi_traits!(local Foo)
508/// // To derive a specific trait, write out the impl item minus the actual block
509/// ::uniffi::derive_ffi_traits!(impl<T, UT> LowerReturn<UT> for Option<T>)
510/// ```
511#[macro_export]
512#[allow(clippy::crate_in_macro_def)]
513macro_rules! derive_ffi_traits {
514 (blanket $ty:ty) => {
515 $crate::derive_ffi_traits!(impl<UT> Lower<UT> for $ty);
516 $crate::derive_ffi_traits!(impl<UT> Lift<UT> for $ty);
517 $crate::derive_ffi_traits!(impl<UT> LowerReturn<UT> for $ty);
518 $crate::derive_ffi_traits!(impl<UT> LowerError<UT> for $ty);
519 $crate::derive_ffi_traits!(impl<UT> LiftReturn<UT> for $ty);
520 $crate::derive_ffi_traits!(impl<UT> LiftRef<UT> for $ty);
521 $crate::derive_ffi_traits!(impl<UT> ConvertError<UT> for $ty);
522 $crate::derive_ffi_traits!(impl<UT> TypeId<UT> for $ty);
523 };
524
525 (local $ty:ty) => {
526 $crate::derive_ffi_traits!(impl Lower<crate::UniFfiTag> for $ty);
527 $crate::derive_ffi_traits!(impl Lift<crate::UniFfiTag> for $ty);
528 $crate::derive_ffi_traits!(impl LowerReturn<crate::UniFfiTag> for $ty);
529 $crate::derive_ffi_traits!(impl LowerError<crate::UniFfiTag> for $ty);
530 $crate::derive_ffi_traits!(impl LiftReturn<crate::UniFfiTag> for $ty);
531 $crate::derive_ffi_traits!(impl LiftRef<crate::UniFfiTag> for $ty);
532 $crate::derive_ffi_traits!(impl ConvertError<crate::UniFfiTag> for $ty);
533 $crate::derive_ffi_traits!(impl TypeId<crate::UniFfiTag> for $ty);
534 };
535
536 (impl $(<$($generic:ident),*>)? $(::uniffi::)? Lower<$ut:path> for $ty:ty $(where $($where:tt)*)?) => {
537 unsafe impl $(<$($generic),*>)* $crate::Lower<$ut> for $ty $(where $($where)*)*
538 {
539 type FfiType = <Self as $crate::FfiConverter<$ut>>::FfiType;
540
541 fn lower(obj: Self) -> Self::FfiType {
542 <Self as $crate::FfiConverter<$ut>>::lower(obj)
543 }
544
545 fn write(obj: Self, buf: &mut ::std::vec::Vec<u8>) {
546 <Self as $crate::FfiConverter<$ut>>::write(obj, buf)
547 }
548 }
549 };
550
551 (impl $(<$($generic:ident),*>)? $(::uniffi::)? Lift<$ut:path> for $ty:ty $(where $($where:tt)*)?) => {
552 unsafe impl $(<$($generic),*>)* $crate::Lift<$ut> for $ty $(where $($where)*)*
553 {
554 type FfiType = <Self as $crate::FfiConverter<$ut>>::FfiType;
555
556 fn try_lift(v: Self::FfiType) -> $crate::deps::anyhow::Result<Self> {
557 <Self as $crate::FfiConverter<$ut>>::try_lift(v)
558 }
559
560 fn try_read(buf: &mut &[u8]) -> $crate::deps::anyhow::Result<Self> {
561 <Self as $crate::FfiConverter<$ut>>::try_read(buf)
562 }
563 }
564 };
565
566 (impl $(<$($generic:ident),*>)? $(::uniffi::)? LowerReturn<$ut:path> for $ty:ty $(where $($where:tt)*)?) => {
567 unsafe impl $(<$($generic),*>)* $crate::LowerReturn<$ut> for $ty $(where $($where)*)*
568 {
569 type ReturnType = <Self as $crate::Lower<$ut>>::FfiType;
570
571 fn lower_return(v: Self) -> $crate::deps::anyhow::Result<Self::ReturnType, $crate::RustCallError> {
572 ::std::result::Result::Ok(<Self as $crate::Lower<$ut>>::lower(v))
573 }
574 }
575 };
576
577 (impl $(<$($generic:ident),*>)? $(::uniffi::)? LowerError<$ut:path> for $ty:ty $(where $($where:tt)*)?) => {
578 unsafe impl $(<$($generic),*>)* $crate::LowerError<$ut> for $ty $(where $($where)*)*
579 {
580 fn lower_error(obj: Self) -> $crate::RustBuffer {
581 <Self as $crate::Lower<$ut>>::lower_into_rust_buffer(obj)
582 }
583 }
584 };
585
586 (impl $(<$($generic:ident),*>)? $(::uniffi::)? LiftReturn<$ut:path> for $ty:ty $(where $($where:tt)*)?) => {
587 unsafe impl $(<$($generic),*>)* $crate::LiftReturn<$ut> for $ty $(where $($where)*)*
588 {
589 type ReturnType = <Self as $crate::Lift<$ut>>::FfiType;
590
591 fn try_lift_successful_return(v: Self::ReturnType) -> $crate::Result<Self> {
592 <Self as $crate::Lift<$ut>>::try_lift(v)
593 }
594 }
595 };
596
597 (impl $(<$($generic:ident),*>)? $(::uniffi::)? LiftRef<$ut:path> for $ty:ty $(where $($where:tt)*)?) => {
598 unsafe impl $(<$($generic),*>)* $crate::LiftRef<$ut> for $ty $(where $($where)*)*
599 {
600 type LiftType = Self;
601 }
602 };
603
604 (impl $(<$($generic:ident),*>)? $(::uniffi::)? ConvertError<$ut:path> for $ty:ty $(where $($where:tt)*)?) => {
605 impl $(<$($generic),*>)* $crate::ConvertError<$ut> for $ty $(where $($where)*)*
606 {
607 fn try_convert_unexpected_callback_error(e: $crate::UnexpectedUniFFICallbackError) -> $crate::deps::anyhow::Result<Self> {
608 $crate::convert_unexpected_error!(e, $ty)
609 }
610 }
611 };
612
613 (impl $(<$($generic:ident),*>)? $(::uniffi::)? HandleAlloc<$ut:path> for $ty:ty $(where $($where:tt)*)?) => {
614 // Derived HandleAlloc implementation.
615 //
616 // This is only needed for !Sized types like `dyn Trait`, below is a blanket implementation
617 // for any sized type.
618 unsafe impl $(<$($generic),*>)* $crate::HandleAlloc<$ut> for $ty $(where $($where)*)*
619 {
620 // To implement HandleAlloc for an unsized type, wrap it with a second Arc which
621 // converts the wide pointer into a normal pointer.
622
623 fn new_handle(value: ::std::sync::Arc<Self>) -> $crate::Handle {
624 $crate::Handle::from_pointer(::std::sync::Arc::into_raw(::std::sync::Arc::new(value)))
625 }
626
627 unsafe fn clone_handle(handle: $crate::Handle) -> $crate::Handle {
628 ::std::sync::Arc::<::std::sync::Arc<Self>>::increment_strong_count(handle.as_pointer::<::std::sync::Arc<Self>>());
629 handle
630 }
631
632 unsafe fn consume_handle(handle: $crate::Handle) -> ::std::sync::Arc<Self> {
633 ::std::sync::Arc::<Self>::clone(
634 &std::sync::Arc::<::std::sync::Arc::<Self>>::from_raw(handle.as_pointer::<::std::sync::Arc<Self>>())
635 )
636 }
637 }
638 };
639
640 (impl $(<$($generic:ident),*>)? $(::uniffi::)? TypeId<$ut:path> for $ty:ty $(where $($where:tt)*)?) => {
641 impl $(<$($generic),*>)* $crate::TypeId<$ut> for $ty $(where $($where)*)*
642 {
643 const TYPE_ID_META: $crate::MetadataBuffer = <Self as $crate::FfiConverter<$ut>>::TYPE_ID_META;
644 }
645 };
646}
647
648unsafe impl<T: Send + Sync, UT> HandleAlloc<UT> for T {
649 fn new_handle(value: Arc<Self>) -> Handle {
650 Handle::from_arc(value)
651 }
652
653 unsafe fn clone_handle(handle: Handle) -> Handle {
654 handle.clone_arc_handle::<T>()
655 }
656
657 unsafe fn consume_handle(handle: Handle) -> Arc<Self> {
658 handle.into_arc()
659 }
660}