#swift #ios #ffi #mac #bindings

swift-bridge-macro

为swift-bridge模块代码生成提供动力

58个版本

0.1.57 2024年8月15日
0.1.55 2024年4月29日
0.1.53 2024年3月26日
0.1.52 2023年7月10日
0.1.5 2021年11月26日

#1884 in 进程宏

Download history 14670/week @ 2024-05-04 18542/week @ 2024-05-11 19720/week @ 2024-05-18 18937/week @ 2024-05-25 21003/week @ 2024-06-01 17252/week @ 2024-06-08 20148/week @ 2024-06-15 22509/week @ 2024-06-22 18921/week @ 2024-06-29 14052/week @ 2024-07-06 10828/week @ 2024-07-13 15314/week @ 2024-07-20 11204/week @ 2024-07-27 10228/week @ 2024-08-03 10359/week @ 2024-08-10 9857/week @ 2024-08-17

44,148 每月下载量
4 个crate中使用(通过 swift-bridge

Apache-2.0/MIT

1MB
25K SLoC

swift-bridge Actions状态 docs crates.io

swift-bridge 促进Rust和Swift的互操作性。

swift-bridge 使得在Rust和Swift之间传递和共享高级类型变得容易,例如 StringOption<T>Result<T, E>structclass 等更多类型。

它还帮助您连接更高层次的语言特性,如异步函数和泛型。

使用 swift-bridge 应该比手动管理Rust和Swift FFI更安全、更高效、更方便。

安装

# In your Cargo.toml

[build-dependencies]
swift-bridge-build = "0.1"

[dependencies]
swift-bridge = "0.1"

书籍

您可以在 swift-bridge 书籍》中找到有关使用Rust和Swift一起使用的信息。

快速预览

您通过在“桥接模块”中声明您想要导入和导出的类型和函数来使用 swift-bridge,然后使用 #[swift_bridge::bridge] 宏来注释该桥接模块。

然后,在构建时,您可以使用swift-bridge-build API或swift-bridge-cli CLI来解析您的注释桥接模块,并生成FFI层的SwiftC部分。

以下是使用桥接模块描述Swift和Rust之间FFI边界的快速预览。

// We use the `swift_bridge::bridge` macro to declare a bridge module.
// Then at build time the `swift-bridge-build` crate is used to generate
// the corresponding Swift and C FFI glue code.
#[swift_bridge::bridge]
mod ffi {
    // Create "transparent" structs where both Rust and Swift can directly access the fields.
    struct AppConfig {
        file_manager: CustomFileManager,
    }

    // Transparent enums are also supported.
    enum UserLookup {
        ById(UserId),
        ByName(String),
    }

    // Export opaque Rust types, functions and methods for Swift to use.
    extern "Rust" {
        type RustApp;

        #[swift_bridge(init)]
        fn new(config: AppConfig) -> RustApp;
        
        fn get_user(&self, lookup: UserLookup) -> Option<&User>;
    }

    extern "Rust" {
        type User;
        type MessageBoard;

        #[swift_bridge(get(&nickname))]
        fn informal_name(self: &User) -> &str;
    }

    // Import opaque Swift classes and functions for Rust to use.
    extern "Swift" {
        type CustomFileManager;
        fn save_file(&self, name: &str, contents: &[u8]);
    }
}

struct User {
    nickname: String
}

快速开始

swift-bridge存储库包含示例应用程序,您可以使用这些应用程序快速尝试库,或作为您自己的Swift + Rust应用程序的起点。

例如,以下是本地运行codegen-visualizer示例项目的方法。

git clone https://github.com/chinedufn/swift-bridge
cd swift-bridge/examples/codegen-visualizer

open CodegenVisualizer/CodegenVisualizer.xcodeproj
# *** Click the "Run" button at the top left of Xcode ***

您可以在 swift-bridge 书籍》中找到有关使用Rust和Swift一起使用的信息。

内置类型

除了允许您在Rust和Swift之间共享自定义结构体、枚举和类之外,swift-bridge还支持许多Rust和Swift标准库类型。

Rust中的名称 Swift中的名称 说明
u8, i8, u16, i16等 UInt8, Int8, UInt16, Int16等
bool Bool
String, &String, &mut String RustString, RustStringRef, RustStringRefMut
&str RustStr
Vec<T> RustVec<T>
SwiftArray<T> Array<T> 尚未实现
&[T] 尚未实现
&mut [T] 尚未实现
Box<T> 尚未实现
Box<dyn FnOnce(A,B,C) -> D> (A, B, C) -> D 从Rust传递到Swift是支持的,但从Swift到Rust尚未实现。
Box<dyn Fn(A,B,C) -> D> (A, B, C) -> D 尚未实现
Arc<T> 尚未实现
[T; N] 尚未实现
*const T UnsafePointer<T>
*mut T UnsafeMutablePointer<T>
Option<T> Optional<T>
fn x() -> Result<T, E> func x() throws -> T
fn x(arg: Result<T, E>) func x(arg: RustResult<T, E>)
(A, B, C, ...) (A, B, C, ...)
您有Rust标准库类型的想法吗?
打开一个问题!
您有Swift标准库类型的想法吗?
打开一个问题!

性能

swift-bridge旨在在性能关键环境中使用。

其生成的所有FFI代码都不使用对象序列化、克隆、同步或其他任何不必要的开销。

要测试

运行测试套件。

# Clone the repository
git clone [email protected]:chinedufn/swift-bridge.git
cd swift-bridge

# Run tests
cargo test --all && ./test-swift-rust-integration.sh && ./test-swift-packages.sh 

贡献

如果您想为swift-bridge做出贡献,请查看贡献者指南

在熟悉贡献过程后,尝试查看一些好的入门问题,看看是否有什么问题引起您的兴趣。

这些问题附带逐步说明,应有助于您实现第一个补丁。

致谢

  • cxx启发了使用桥接模块描述FFI边界的想法。

许可

许可协议为MITApache-2.0

依赖关系

~1.5MB
~36K SLoC