swift_bridge/
lib.rs

1//! Generate FFI glue between Swift and Rust code.
2
3#![deny(missing_docs)]
4
5pub use swift_bridge_macro::bridge;
6
7mod std_bridge;
8
9pub use self::std_bridge::{option, result, string};
10
11#[doc(hidden)]
12#[cfg(feature = "async")]
13pub mod async_support;
14
15#[doc(hidden)]
16pub mod boxed_fn_support;
17
18#[doc(hidden)]
19pub mod copy_support;
20
21#[doc(hidden)]
22#[repr(C)]
23pub struct FfiSlice<T> {
24    pub start: *const T,
25    pub len: usize,
26}
27
28// Unlike the Swift pointer wrapper types that we generate, this type does not implement drop.
29// So we can freely construct it and pass it over the FFI boundary without worrying about drop
30//
31// It has the same layout as the __private__PointerToSwift C struct, so when we pass this to
32// Swift it can receive it as a __private__PointerToSwift.
33#[doc(hidden)]
34#[repr(C)]
35pub struct PointerToSwiftType(pub *mut std::ffi::c_void);
36
37impl<T> FfiSlice<T> {
38    /// Create an FfiSlice from a slice.
39    pub fn from_slice(slice: &[T]) -> Self {
40        FfiSlice {
41            start: slice.as_ptr(),
42            len: slice.len(),
43        }
44    }
45
46    /// Get a reference to the slice that this FfiSlice points to.
47    pub fn as_slice(&self) -> &'static [T] {
48        unsafe { std::slice::from_raw_parts(self.start, self.len) }
49    }
50}
51
52// The code generation automatically implements this for all shared structs.
53// This trait is private and should not be used outside of swift-bridge.
54//
55// The main use case is for structs that use the `#[swift_bridge(already_declared)]`
56// attribute, where we use `<SomeStruct as SharedStruct::FfiRepr>` to get the
57// struct's FFI representation.
58#[doc(hidden)]
59pub trait SharedStruct {
60    /// The FFI friendly representation of this struct.
61    ///
62    /// ```
63    /// struct MyStruct {
64    ///     field: &'static str
65    /// }
66    /// // This is the auto generated ffi representation.
67    /// #[repr(C)]
68    /// struct __swift_bridge__MyStruct {
69    ///     field: swift_bridge::string::RustStr
70    /// }
71    /// ```
72    type FfiRepr;
73}
74
75// The code generation automatically implements this for all shared enum.
76// This trait is private and should not be used outside of swift-bridge.
77//
78// The main use case is for enums that use the `#[swift_bridge(already_declared)]`
79// attribute, where we use `<SomeEnum as SharedEnum::FfiRepr>` to get the
80// enum's FFI representation.
81#[doc(hidden)]
82pub trait SharedEnum {
83    /// The FFI friendly representation of this enum.
84    ///
85    /// ```
86    /// enum MyEnum {
87    ///     Variant1,
88    ///     Variant2,
89    /// }
90    /// // This is the auto generated ffi representation.
91    /// #[repr(C)]
92    /// enum __swift_bridge__MyEnum {
93    ///     Variant1,
94    ///     Variant2,
95    /// }
96    /// ```
97    type FfiRepr;
98}
99
100#[no_mangle]
101#[doc(hidden)]
102pub extern "C" fn __swift_bridge__null_pointer() -> *const std::ffi::c_void {
103    std::ptr::null()
104}