swift_name.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #[export_name="rust_print_hello_world"]
  2. pub extern fn say_hello() {
  3. println!("Hello, World!");
  4. }
  5. #[repr(C)]
  6. pub struct SelfTypeTestStruct {
  7. times: u8,
  8. }
  9. impl SelfTypeTestStruct {
  10. #[export_name="SelfTypeTestStruct_should_exist_ref"]
  11. #[no_mangle]
  12. pub extern fn should_exist_ref(&self) {
  13. println!("should_exist_ref");
  14. }
  15. #[export_name="SelfTypeTestStruct_should_exist_ref_mut"]
  16. #[no_mangle]
  17. pub extern fn should_exist_ref_mut(&mut self) {
  18. println!("should_exist_ref_mut");
  19. }
  20. #[export_name="SelfTypeTestStruct_should_not_exist_box"]
  21. #[no_mangle]
  22. pub extern fn should_not_exist_box(self: Box<SelfTypeTestStruct>) {
  23. println!("should_not_exist_box");
  24. }
  25. #[export_name="SelfTypeTestStruct_should_not_exist_return_box"]
  26. #[no_mangle]
  27. pub extern fn should_not_exist_box() -> Box<Self> {
  28. println!("should_not_exist_box");
  29. }
  30. #[export_name="SelfTypeTestStruct_should_exist_annotated_self"]
  31. #[no_mangle]
  32. pub extern fn should_exist_annotated_self(self: Self) {
  33. println!("should_exist_annotated_self");
  34. }
  35. #[export_name="SelfTypeTestStruct_should_exist_annotated_mut_self"]
  36. #[no_mangle]
  37. #[allow(unused_mut)]
  38. pub extern fn should_exist_annotated_mut_self(mut self: Self) {
  39. println!("should_exist_annotated_mut_self");
  40. }
  41. #[export_name="SelfTypeTestStruct_should_exist_annotated_by_name"]
  42. #[no_mangle]
  43. pub extern fn should_exist_annotated_by_name(self: SelfTypeTestStruct) {
  44. println!("should_exist_annotated_by_name");
  45. }
  46. #[export_name="SelfTypeTestStruct_should_exist_annotated_mut_by_name"]
  47. #[no_mangle]
  48. #[allow(unused_mut)]
  49. pub extern fn should_exist_annotated_mut_by_name(mut self: SelfTypeTestStruct) {
  50. println!("should_exist_annotated_mut_by_name");
  51. }
  52. #[export_name="SelfTypeTestStruct_should_exist_unannotated"]
  53. #[no_mangle]
  54. pub extern fn should_exist_unannotated(self) {
  55. println!("should_exist_unannotated");
  56. }
  57. #[export_name="SelfTypeTestStruct_should_exist_mut_unannotated"]
  58. #[no_mangle]
  59. #[allow(unused_mut)]
  60. pub extern fn should_exist_mut_unannotated(mut self) {
  61. println!("should_exist_mut_unannotated");
  62. }
  63. }
  64. #[no_mangle]
  65. #[allow(unused_variables)]
  66. pub extern fn free_function_should_exist_ref(test_struct: &SelfTypeTestStruct) {
  67. println!("free_function_should_exist_ref");
  68. }
  69. #[no_mangle]
  70. #[allow(unused_variables)]
  71. pub extern fn free_function_should_exist_ref_mut(test_struct: &mut SelfTypeTestStruct) {
  72. println!("free_function_should_exist_ref_mut");
  73. }
  74. #[no_mangle]
  75. pub extern fn unnamed_argument(_: &mut SelfTypeTestStruct) {
  76. println!("unnamed_argument");
  77. }
  78. #[no_mangle]
  79. #[allow(unused_variables)]
  80. pub extern fn free_function_should_not_exist_box(boxed: Box<SelfTypeTestStruct>) {
  81. println!("free_function_should_not_exist_box");
  82. }
  83. #[no_mangle]
  84. #[allow(unused_variables)]
  85. pub extern fn free_function_should_exist_annotated_by_name(test_struct: SelfTypeTestStruct) {
  86. println!("free_function_should_exist_annotated_by_name");
  87. }
  88. #[no_mangle]
  89. #[allow(unused_mut)]
  90. #[allow(unused_variables)]
  91. pub extern fn free_function_should_exist_annotated_mut_by_name(mut test_struct: SelfTypeTestStruct) {
  92. println!("free_function_should_exist_annotated_mut_by_name");
  93. }
  94. struct Opaque {
  95. times: u8
  96. }
  97. #[repr(C)]
  98. pub struct PointerToOpaque { ptr: *mut Opaque }
  99. impl PointerToOpaque {
  100. #[export_name="PointerToOpaque_create"]
  101. pub extern fn create(times: u8) -> PointerToOpaque {
  102. PointerToOpaque { ptr: Box::into_raw(Box::new(Opaque { times })) }
  103. }
  104. #[export_name="PointerToOpaque_sayHello"]
  105. pub extern fn say_hello(self: PointerToOpaque) {
  106. if let Some(nonnull) = std::ptr::NonNull::new(self.ptr) {
  107. for _ in 0 .. unsafe { nonnull.as_ref().times } {
  108. println!("Hello!")
  109. }
  110. }
  111. }
  112. }