|
2 | 2 |
|
3 | 3 | > `swift-bridge` facilitates Rust and Swift interop.
|
4 | 4 |
|
5 |
| -`swift-bridge` is a library that lets you pass and share high-level types such as `Option<T>`, `String`, |
6 |
| -`Structs` and `Classes` between Rust and Swift. |
| 5 | +`swift-bridge` is a library that helps you pass and share high-level types such as `Option<T>`, `String`, |
| 6 | +`struct` and `Class` between Rust and Swift. |
7 | 7 |
|
8 |
| -It also lets you bridge higher level language features between Rust and Swift, such as async functions and generics. |
| 8 | +It also helps you bridge higher level language features between Rust and Swift, such as async functions and generics. |
9 | 9 |
|
10 | 10 | ## Installation
|
11 | 11 |
|
@@ -36,52 +36,50 @@ Here's a quick peek at how you might describe an FFI boundary between Swift and
|
36 | 36 |
|
37 | 37 | <!-- ANCHOR: bridge-module-example -->
|
38 | 38 | ```rust
|
39 |
| -// Use the `swift_bridge::bridge` macro to declare a bridge module that |
40 |
| -// `swift-bridge-build` will parse at build time in order to generate |
41 |
| -// the necessary Swift and C FFI glue code. |
| 39 | +// We use the `swift_bridge::bridge` macro to declare a bridge module. |
| 40 | +// Then at build time the `swift-bridge-build` crate is used to generate |
| 41 | +// the corresponding Swift and C FFI glue code. |
42 | 42 | #[swift_bridge::bridge]
|
43 | 43 | mod ffi {
|
44 |
| - // Create shared structs where both Rust and Swift can directly access the fields. |
| 44 | + // Create "transparent" structs where both Rust and Swift can directly access the fields. |
45 | 45 | struct AppConfig {
|
46 | 46 | file_manager: CustomFileManager,
|
47 | 47 | }
|
48 | 48 |
|
49 |
| - // Shared enums are also supported |
| 49 | + // Transparent enums are also supported. |
50 | 50 | enum UserLookup {
|
51 | 51 | ById(UserId),
|
52 | 52 | ByName(String),
|
53 | 53 | }
|
54 | 54 |
|
55 |
| - // Export Rust types, functions and methods for Swift to use. |
| 55 | + // Export opaque Rust types, functions and methods for Swift to use. |
56 | 56 | extern "Rust" {
|
57 | 57 | type RustApp;
|
58 | 58 |
|
59 | 59 | #[swift_bridge(init)]
|
60 |
| - fn new(config: AppConfig); |
| 60 | + fn new(config: AppConfig) -> RustApp; |
61 | 61 |
|
62 |
| - fn insert_user(&mut self, user_id: UserId, user: User); |
63 | 62 | fn get_user(&self, lookup: UserLookup) -> Option<&User>;
|
64 | 63 | }
|
65 | 64 |
|
66 | 65 | extern "Rust" {
|
67 | 66 | type User;
|
| 67 | + type MessageBoard; |
68 | 68 |
|
69 |
| - #[swift_bridge(Copy(4))] |
70 |
| - type UserId; |
71 |
| - |
72 |
| - #[swift_bridge(init)] |
73 |
| - fn new(user_id: UserId, name: String, email: Option<String>) -> User; |
| 69 | + #[swift_bridge(get(&nickname))] |
| 70 | + fn informal_name(self: &User) -> &str; |
74 | 71 | }
|
75 | 72 |
|
76 |
| - // Import Swift classes and functions for Rust to use. |
| 73 | + // Import opaque Swift classes and functions for Rust to use. |
77 | 74 | extern "Swift" {
|
78 | 75 | type CustomFileManager;
|
79 | 76 | fn save_file(&self, name: &str, contents: &[u8]);
|
80 | 77 | }
|
81 | 78 | }
|
82 | 79 |
|
83 |
| -#[derive(Copy)] |
84 |
| -struct UserId(u32); |
| 80 | +struct User { |
| 81 | + nickname: String |
| 82 | +} |
85 | 83 | ```
|
86 | 84 | <!-- ANCHOR_END: bridge-module-example -->
|
87 | 85 |
|
|
0 commit comments