-
Notifications
You must be signed in to change notification settings - Fork 54
/
README.md
267 lines (195 loc) · 4.84 KB
/
README.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# Opaque Types
... TODO OVERVIEW ...
## Exposing Opaque Rust Types
`extern "Rust` sections are used to expose Rust types and their associated methods and functions
so that they can be used from Swift code.
```rust
mod science;
use science::{ScienceLab, Hydrogen, Oxygen, make_water};
#[swift_bridge::bridge]
mod ffi {
extern "Rust" {
type Water;
#[swift_bridge(associated_to = Water)]
fn new() -> Water;
fn is_wet(&self) -> bool;
}
extern "Rust" {
type ScienceLab;
type Hydrogen;
type Oxygen;
fn make_water(
lab: &ScienceLab,
hydrogen: Hydrogen,
oxygen: Oxygen
) -> Water;
}
}
pub struct Water;
impl Water {
fn new () -> Self {
Water
}
fn is_wet(&self) -> bool {
unreachable!("Seriously...?")
}
}
```
## Owned, Ref and RefMut
When you define a type in an `extern "Rust"` block, three corresponding Swift classes get generated.
```swift
// Equivalent to `SomeType` in Rust
class SomeType: SomeTypeRefMut {
// ...
}
// Equivalent to `&mut SomeType` in Rust
class SomeTypeRefMut: SomeTypeRef {
// ...
}
// Equivalent to `&SomeType` in Rust
class SomeTypeRef {
// ...
}
```
Here's an example of how `&Type` and `&mut Type` are enforced:
```rust
// Rust
extern "Rust" {
type SomeType;
#[swift_bridge(init)]
fn new() -> SomeType;
// Callable by SomeType, SomeTypeRef and SomeTypeRefMut.
fn (&self) everyone();
// Callable by SomeType, and SomeTypeRefMut.
fn (&mut self) only_owned_and_ref_mut();
// Only callable by SomeType.
fn (self) only_owned();
}
extern "Rust" {
fn make_ref() -> &'static SomeType;
fn make_ref_mut() -> &'static mut SomeType;
}
```
```swift
// Swift
func methods() {
let someType: SomeType = SomeType()
let someTypeRef: SomeTypeRef = make_ref()
let someTypeRefMut: SomeTypeRefMut = make_ref_mut()
someType.everyone()
someType.only_owned_and_ref_mut()
someType.only_owned()
someTypeRefMut.everyone()
someTypeRefMut.only_owned_and_ref_mut()
someTypeRef.everyone()
}
func functions() {
let someType: SomeType = SomeType()
let someTypeRef: SomeTypeRef = make_ref()
let someTypeRefMut: SomeTypeRefMut = make_ref_mut()
takeReference(someType)
takeReference(someTypeRef)
takeReference(someTypeRefMut)
}
// Can be called with SomeType, SomeTypeRef and SomeTypeRefMut
func useSomeType(someType: SomeTypeRef) {
// ...
}
```
## Opaque Type Attributes
#### #[swift_bridge(already_declared)]
The `already_declared` attribute allows you to use the same type in multiple bridge modules.
```rust
use some_crate::App;
mod ffi {
extern "Rust" {
type App;
#[swift_bridge(init)]
fn new() -> App;
}
}
#[swift_bridge::bridge]
#[cfg(feature = "dev-utils")]
mod ffi_dev_utils {
extern "Rust" {
// We won't emit Swift and C type definitions for this type
// since we've already declared it elsewhere.
#[swift_bridge(already_declared)]
type App;
fn create_logged_in_user(&mut self, user_id: u8);
}
}
```
#### #[swift_bridge(Copy($SIZE))]
If you have an opaque Rust type that implements `Copy`, you will typically want to be
able to pass it between Swift and Rust by copying the bytes instead of allocating.
For example, let's say you have some new type wrappers for different kinds of IDs
within your system.
```
use uuid:Uuid;
#[derive(Copy)]
struct UserId(Uuid);
#[derive(Copy)]
struct OrganizationId(Uuid);
```
You can expose them using:
```rust
#[swift_bridge::bridge]
mod ffi {
extern "Rust" {
#[swift_bridge(Copy(16))]
type UserId;
#[swift_bridge(Copy(16))]
type OrganizationId;
}
}
```
The `16` indicates that a `UserId` has 16 bytes.
`swift-bridge` will add a compile time assertion that confirms that the given size is correct.
#### #[swift_bridge(Equatable)]
The `Equatable` attribute allows you to expose a Rust `PartialEq` implementation via Swift's
`Equatable` protocol.
```rust
#[swift_bridge::bridge]
mod ffi {
extern "Rust" {
#[swift_bridge(Equatable)]
type RustPartialEqType;
}
}
#[derive(PartialEq)]
struct RustPartialEqType(u32);
```
```swift
// In Swift
let val1 = RustPartialEqType(5)
let val2 = RustPartialEqType(10)
if val1 == val2 {
print("Equal")
} else {
print("Not equal")
}
```
#### #[swift_bridge(Hashable)]
The `Hashable` attribute allows you to expose a Rust `Hash` trait implementation via Swift's
`Hashable` protocol.
```rust
#[swift_bridge::bridge]
mod ffi {
extern "Rust" {
#[swift_bridge(Hashable)]
type RustHashType;
}
}
#[derive(Hash, PartialEq)]
struct RustHashType(u32);
```
```swift
// In Swift
let val = RustHashType(10);
let table: [RustHashType: String] = [:]
table[val] = "hello"
table[val] = "world"
//Should print "world"
print(table[val])
```