-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathtransparent_struct_codegen_tests.rs
704 lines (622 loc) · 20.6 KB
/
transparent_struct_codegen_tests.rs
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
use super::{CodegenTest, ExpectedCHeader, ExpectedRustTokens, ExpectedSwiftCode};
use proc_macro2::TokenStream;
use quote::quote;
/// Verify that we generate the functions to convert a shared struct that does not have fields
/// to and from its ffi representation.
mod generates_struct_to_and_from_ffi_conversions_no_fields {
use super::*;
fn bridge_module_tokens() -> TokenStream {
quote! {
#[swift_bridge::bridge]
mod ffi {
#[swift_bridge(swift_repr = "struct")]
struct SomeStruct();
}
}
}
fn expected_rust_tokens() -> ExpectedRustTokens {
ExpectedRustTokens::Contains(quote! {
pub struct SomeStruct();
#[repr(C)]
#[doc(hidden)]
pub struct __swift_bridge__SomeStruct {
_private: u8
}
impl swift_bridge::SharedStruct for SomeStruct {
type FfiRepr = __swift_bridge__SomeStruct;
}
impl SomeStruct {
#[doc(hidden)]
#[inline(always)]
pub fn into_ffi_repr(self) -> __swift_bridge__SomeStruct {
__swift_bridge__SomeStruct {
_private: 123
}
}
}
impl __swift_bridge__SomeStruct {
#[doc(hidden)]
#[inline(always)]
pub fn into_rust_repr(self) -> SomeStruct {
SomeStruct()
}
}
})
}
fn expected_swift_code() -> ExpectedSwiftCode {
ExpectedSwiftCode::ContainsAfterTrim(
r#"
public struct SomeStruct {
public init() {}
@inline(__always)
func intoFfiRepr() -> __swift_bridge__$SomeStruct {
__swift_bridge__$SomeStruct(_private: 123)
}
}
extension __swift_bridge__$SomeStruct {
@inline(__always)
func intoSwiftRepr() -> SomeStruct {
SomeStruct()
}
}
"#,
)
}
fn expected_c_header() -> ExpectedCHeader {
ExpectedCHeader::ContainsAfterTrim(
r#"
#include <stdint.h>
#include <stdbool.h>
typedef struct __swift_bridge__$SomeStruct { uint8_t _private; } __swift_bridge__$SomeStruct;
typedef struct __swift_bridge__$Option$SomeStruct { bool is_some; __swift_bridge__$SomeStruct val; } __swift_bridge__$Option$SomeStruct;
"#,
)
}
#[test]
fn generates_struct_to_and_from_ffi_conversions_with_fields() {
CodegenTest {
bridge_module: bridge_module_tokens().into(),
expected_rust_tokens: expected_rust_tokens(),
expected_swift_code: expected_swift_code(),
expected_c_header: expected_c_header(),
}
.test();
}
}
/// Verify that we generate the functions to convert a shared struct that has fields to and from
/// its ffi representation.
mod generates_struct_to_and_from_ffi_conversions_with_fields {
use super::*;
fn bridge_module_tokens() -> TokenStream {
quote! {
#[swift_bridge::bridge]
mod ffi {
#[swift_bridge(swift_repr = "struct")]
struct SomeStruct {
field: u8
}
}
}
}
fn expected_rust_tokens() -> ExpectedRustTokens {
ExpectedRustTokens::Contains(quote! {
pub struct SomeStruct {
pub field: u8
}
#[repr(C)]
#[doc(hidden)]
pub struct __swift_bridge__SomeStruct {
field: u8
}
impl swift_bridge::SharedStruct for SomeStruct {
type FfiRepr = __swift_bridge__SomeStruct;
}
impl SomeStruct {
#[doc(hidden)]
#[inline(always)]
pub fn into_ffi_repr(self) -> __swift_bridge__SomeStruct {
{ let val = self; __swift_bridge__SomeStruct{ field: val.field } }
}
}
impl __swift_bridge__SomeStruct {
#[doc(hidden)]
#[inline(always)]
pub fn into_rust_repr(self) -> SomeStruct {
{ let val = self; SomeStruct { field: val.field } }
}
}
})
}
fn expected_swift_code() -> ExpectedSwiftCode {
ExpectedSwiftCode::ContainsAfterTrim(
r#"
struct SomeStruct {
public var field: UInt8
public init(field: UInt8) {
self.field = field
}
@inline(__always)
func intoFfiRepr() -> __swift_bridge__$SomeStruct {
{ let val = self; return __swift_bridge__$SomeStruct(field: val.field); }()
}
}
extension __swift_bridge__$SomeStruct {
@inline(__always)
func intoSwiftRepr() -> SomeStruct {
{ let val = self; return SomeStruct(field: val.field); }()
}
}
"#,
)
}
fn expected_c_header() -> ExpectedCHeader {
ExpectedCHeader::ContainsAfterTrim(
r#"
typedef struct __swift_bridge__$SomeStruct { uint8_t field; } __swift_bridge__$SomeStruct;
"#,
)
}
#[test]
fn generates_struct_to_and_from_ffi_conversions_with_fields() {
CodegenTest {
bridge_module: bridge_module_tokens().into(),
expected_rust_tokens: expected_rust_tokens(),
expected_swift_code: expected_swift_code(),
expected_c_header: expected_c_header(),
}
.test();
}
}
/// Test code generation for a struct that has a primitive field and swift_repr = "struct".
mod struct_with_primitive_field {
use super::*;
fn bridge_module_tokens() -> TokenStream {
quote! {
#[swift_bridge::bridge]
mod ffi {
#[swift_bridge(swift_repr = "struct")]
struct SomeStruct {
field: u8
}
#[swift_bridge(swift_repr = "struct")]
pub struct AnotherStruct(u8);
}
}
}
fn expected_rust_tokens() -> ExpectedRustTokens {
ExpectedRustTokens::ContainsMany(vec![
quote! {
pub struct SomeStruct {
pub field: u8
}
#[repr(C)]
#[doc(hidden)]
pub struct __swift_bridge__SomeStruct {
field: u8
}
},
quote! {
pub struct AnotherStruct(pub u8);
#[repr(C)]
#[doc(hidden)]
pub struct __swift_bridge__AnotherStruct(u8);
},
])
}
fn expected_swift_code() -> ExpectedSwiftCode {
ExpectedSwiftCode::ContainsManyAfterTrim(vec![
r#"
struct SomeStruct {
public var field: UInt8
public init(field: UInt8) {
self.field = field
}
"#,
r#"
struct AnotherStruct {
public var _0: UInt8
public init(_0: UInt8) {
self._0 = _0
}
"#,
])
}
fn expected_c_header() -> ExpectedCHeader {
ExpectedCHeader::ContainsManyAfterTrim(vec![
r#"
typedef struct __swift_bridge__$SomeStruct { uint8_t field; } __swift_bridge__$SomeStruct;
"#,
r#"
typedef struct __swift_bridge__$AnotherStruct { uint8_t _0; } __swift_bridge__$AnotherStruct;
"#,
])
}
#[test]
fn struct_with_primitive_field() {
CodegenTest {
bridge_module: bridge_module_tokens().into(),
expected_rust_tokens: expected_rust_tokens(),
expected_swift_code: expected_swift_code(),
expected_c_header: expected_c_header(),
}
.test();
}
}
/// Test code generation for passing a `swift_repr = "struct"` as an argument to a
/// extern "Rust" fn.
mod extern_rust_fn_arg_swift_repr_struct {
use super::*;
fn bridge_module_tokens() -> TokenStream {
quote! {
#[swift_bridge::bridge]
mod ffi {
#[swift_bridge(swift_repr = "struct")]
pub struct SomeStruct {
pub field: u8
}
extern "Rust" {
fn some_function(arg: SomeStruct);
}
}
}
}
fn expected_rust_tokens() -> ExpectedRustTokens {
ExpectedRustTokens::Contains(quote! {
pub extern "C" fn __swift_bridge__some_function (arg: __swift_bridge__SomeStruct) {
super::some_function(arg.into_rust_repr())
}
})
}
fn expected_swift_code() -> ExpectedSwiftCode {
ExpectedSwiftCode::ContainsAfterTrim(
r#"
func some_function(_ arg: SomeStruct) {
__swift_bridge__$some_function(arg.intoFfiRepr())
}
"#,
)
}
fn expected_c_header() -> ExpectedCHeader {
ExpectedCHeader::ContainsAfterTrim(
r#"
void __swift_bridge__$some_function(struct __swift_bridge__$SomeStruct arg);
"#,
)
}
#[test]
fn extern_rust_fn_arg_swift_repr_struct() {
CodegenTest {
bridge_module: bridge_module_tokens().into(),
expected_rust_tokens: expected_rust_tokens(),
expected_swift_code: expected_swift_code(),
expected_c_header: expected_c_header(),
}
.test();
}
}
/// Test code generation for passing a `swift_repr = "struct"` as an argument to a
/// extern "Swift" fn.
mod extern_swift_fn_arg_swift_repr_struct {
use super::*;
fn bridge_module_tokens() -> TokenStream {
quote! {
#[swift_bridge::bridge]
mod ffi {
#[swift_bridge(swift_repr = "struct")]
pub struct SomeStruct {
pub field: u8
}
extern "Swift" {
fn some_function(arg: SomeStruct);
}
}
}
}
fn expected_rust_tokens() -> ExpectedRustTokens {
ExpectedRustTokens::Contains(quote! {
extern "C" {
#[link_name = "__swift_bridge__$some_function"]
fn __swift_bridge__some_function (arg : __swift_bridge__SomeStruct);
}
})
}
fn expected_swift_code() -> ExpectedSwiftCode {
ExpectedSwiftCode::ContainsAfterTrim(
r#"
@_cdecl("__swift_bridge__$some_function")
func __swift_bridge__some_function (_ arg: __swift_bridge__$SomeStruct) {
some_function(arg: arg.intoSwiftRepr())
}
"#,
)
}
fn expected_c_header() -> ExpectedCHeader {
ExpectedCHeader::SkipTest
}
#[test]
fn extern_swift_fn_arg_swift_repr_struct() {
CodegenTest {
bridge_module: bridge_module_tokens().into(),
expected_rust_tokens: expected_rust_tokens(),
expected_swift_code: expected_swift_code(),
expected_c_header: expected_c_header(),
}
.test();
}
}
/// Test code generation for passing a `swift_repr = "struct"` from extern "Rust".
mod extern_rust_return_swift_repr_struct {
use super::*;
fn bridge_module_tokens() -> TokenStream {
quote! {
#[swift_bridge::bridge]
mod ffi {
#[swift_bridge(swift_repr = "struct")]
struct SomeStruct {
field: u8
}
extern "Rust" {
fn some_function() -> SomeStruct;
}
}
}
}
fn expected_rust_tokens() -> ExpectedRustTokens {
ExpectedRustTokens::Contains(quote! {
pub extern "C" fn __swift_bridge__some_function () -> __swift_bridge__SomeStruct {
super::some_function().into_ffi_repr()
}
})
}
fn expected_swift_code() -> ExpectedSwiftCode {
ExpectedSwiftCode::ContainsAfterTrim(
r#"
func some_function() -> SomeStruct {
__swift_bridge__$some_function().intoSwiftRepr()
}
"#,
)
}
fn expected_c_header() -> ExpectedCHeader {
ExpectedCHeader::ContainsManyAfterTrim(vec![
r#"
typedef struct __swift_bridge__$SomeStruct { uint8_t field; } __swift_bridge__$SomeStruct;
"#,
r#"
struct __swift_bridge__$SomeStruct __swift_bridge__$some_function(void);
"#,
])
}
#[test]
fn extern_rust_return_swift_repr_struct() {
CodegenTest {
bridge_module: bridge_module_tokens().into(),
expected_rust_tokens: expected_rust_tokens(),
expected_swift_code: expected_swift_code(),
expected_c_header: expected_c_header(),
}
.test();
}
}
/// Test code generation for passing a `swift_repr = "struct"` from extern "Rust".
mod extern_swift_return_swift_repr_struct {
use super::*;
fn bridge_module_tokens() -> TokenStream {
quote! {
#[swift_bridge::bridge]
mod ffi {
#[swift_bridge(swift_repr = "struct")]
struct SomeStruct {
field: u8
}
extern "Swift" {
fn some_function() -> SomeStruct;
}
}
}
}
fn expected_rust_tokens() -> ExpectedRustTokens {
ExpectedRustTokens::Contains(quote! {
extern "C" {
#[link_name = "__swift_bridge__$some_function"]
fn __swift_bridge__some_function () -> __swift_bridge__SomeStruct;
}
})
}
fn expected_swift_code() -> ExpectedSwiftCode {
ExpectedSwiftCode::ContainsAfterTrim(
r#"
@_cdecl("__swift_bridge__$some_function")
func __swift_bridge__some_function () -> __swift_bridge__$SomeStruct {
some_function().intoFfiRepr()
}
"#,
)
}
fn expected_c_header() -> ExpectedCHeader {
ExpectedCHeader::ContainsAfterTrim(
r#"
typedef struct __swift_bridge__$SomeStruct { uint8_t field; } __swift_bridge__$SomeStruct;
"#,
)
}
#[test]
fn extern_swift_return_swift_repr_struct() {
CodegenTest {
bridge_module: bridge_module_tokens().into(),
expected_rust_tokens: expected_rust_tokens(),
expected_swift_code: expected_swift_code(),
expected_c_header: expected_c_header(),
}
.test();
}
}
/// Verify that the original name of the struct is not present in any of the generated Swift
/// code when we use the `swift_name` attribute..
/// Related: crates/swift-integration-tests/src/struct_attributes/swift_name.rs
mod shared_struct_swift_name_attribute {
use super::*;
fn bridge_module_tokens() -> TokenStream {
quote! {
#[swift_bridge::bridge]
mod ffi {
#[swift_bridge(swift_name = "StructRename1")]
struct StructName1;
#[swift_bridge(swift_name = "StructRename2", swift_repr = "struct")]
struct StructName2 {
field: u8,
}
#[swift_bridge(swift_name = "StructRename3", swift_repr = "struct")]
struct StructName3(u8);
extern "Rust" {
fn extern_rust_struct_rename_1(arg: StructName1) -> StructName1;
fn extern_rust_struct_rename_2(arg: StructName2) -> StructName2;
fn extern_rust_struct_rename_3(arg: StructName3) -> StructName3;
}
extern "Rust" {
fn extern_swift_struct_rename_1(arg: StructName1) -> StructName1;
fn extern_swift_struct_rename_2(arg: StructName2) -> StructName2;
fn extern_swift_struct_rename_3(arg: StructName3) -> StructName3;
}
}
}
}
fn expected_rust_tokens() -> ExpectedRustTokens {
ExpectedRustTokens::SkipTest
}
fn expected_swift_code() -> ExpectedSwiftCode {
ExpectedSwiftCode::DoesNotContainManyAfterTrim(vec![
"StructName1",
"StructName2",
"StructName3",
])
}
fn expected_c_header() -> ExpectedCHeader {
ExpectedCHeader::DoesNotContainManyAfterTrim(vec![
"StructName1",
"StructName2",
"StructName3",
])
}
#[test]
fn shared_struct_swift_name_attribute() {
CodegenTest {
bridge_module: bridge_module_tokens().into(),
expected_rust_tokens: expected_rust_tokens(),
expected_swift_code: expected_swift_code(),
expected_c_header: expected_c_header(),
}
.test();
}
}
/// Verify that we can use `Option<Struct>` as Rust function arg and return type.
mod extern_rust_option_struct {
use super::*;
fn bridge_module_tokens() -> TokenStream {
quote! {
#[swift_bridge::bridge]
mod ffi {
#[swift_bridge(swift_repr = "struct")]
struct SomeStruct {
field: u8
}
extern "Rust" {
fn some_function(arg: Option<SomeStruct>) -> Option<SomeStruct>;
}
}
}
}
fn expected_rust_tokens() -> ExpectedRustTokens {
ExpectedRustTokens::ContainsMany(vec![
quote! {
#[repr(C)]
#[doc(hidden)]
pub struct __swift_bridge__Option_SomeStruct {
is_some: bool,
val: std::mem::MaybeUninit<__swift_bridge__SomeStruct>,
}
impl __swift_bridge__Option_SomeStruct {
#[doc(hidden)]
#[inline(always)]
pub fn into_rust_repr(self) -> Option<SomeStruct> {
if self.is_some {
Some(unsafe { self.val.assume_init().into_rust_repr() })
} else {
None
}
}
#[doc(hidden)]
#[inline(always)]
pub fn from_rust_repr(val: Option<SomeStruct>) -> __swift_bridge__Option_SomeStruct {
if let Some(val) = val {
__swift_bridge__Option_SomeStruct {
is_some: true,
val: std::mem::MaybeUninit::new(val.into_ffi_repr())
}
} else {
__swift_bridge__Option_SomeStruct {
is_some: false,
val: std::mem::MaybeUninit::uninit()
}
}
}
}
},
quote! {
pub extern "C" fn __swift_bridge__some_function(arg: __swift_bridge__Option_SomeStruct) -> __swift_bridge__Option_SomeStruct {
__swift_bridge__Option_SomeStruct::from_rust_repr(super::some_function(arg.into_rust_repr()))
}
},
])
}
fn expected_swift_code() -> ExpectedSwiftCode {
ExpectedSwiftCode::ContainsManyAfterTrim(vec![
r#"
extension __swift_bridge__$Option$SomeStruct {
@inline(__always)
func intoSwiftRepr() -> Optional<SomeStruct> {
if self.is_some {
return self.val.intoSwiftRepr()
} else {
return nil
}
}
@inline(__always)
static func fromSwiftRepr(_ val: Optional<SomeStruct>) -> __swift_bridge__$Option$SomeStruct {
if let v = val {
return __swift_bridge__$Option$SomeStruct(is_some: true, val: v.intoFfiRepr())
} else {
return __swift_bridge__$Option$SomeStruct(is_some: false, val: __swift_bridge__$SomeStruct())
}
}
}
"#,
r#"
func some_function(_ arg: Optional<SomeStruct>) -> Optional<SomeStruct> {
__swift_bridge__$some_function(__swift_bridge__$Option$SomeStruct.fromSwiftRepr(arg)).intoSwiftRepr()
}
"#,
])
}
fn expected_c_header() -> ExpectedCHeader {
ExpectedCHeader::ContainsManyAfterTrim(vec![
r#"
#include <stdint.h>
#include <stdbool.h>
typedef struct __swift_bridge__$SomeStruct { uint8_t field; } __swift_bridge__$SomeStruct;
typedef struct __swift_bridge__$Option$SomeStruct { bool is_some; __swift_bridge__$SomeStruct val; } __swift_bridge__$Option$SomeStruct;
"#,
"struct __swift_bridge__$Option$SomeStruct __swift_bridge__$some_function(struct __swift_bridge__$Option$SomeStruct arg);",
])
}
#[test]
fn option_struct() {
CodegenTest {
bridge_module: bridge_module_tokens().into(),
expected_rust_tokens: expected_rust_tokens(),
expected_swift_code: expected_swift_code(),
expected_c_header: expected_c_header(),
}
.test();
}
}