summaryrefslogtreecommitdiffstats
path: root/tests/rust/swift_name.rs
blob: 01cfdbe6b9c0c5e13495a0f2e7f717c9c93e1764 (plain)
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
#[export_name="rust_print_hello_world"]
pub extern fn say_hello() {
  println!("Hello, World!");
}

#[repr(C)]
pub struct SelfTypeTestStruct {
  times: u8,
}

impl SelfTypeTestStruct {
  #[export_name="SelfTypeTestStruct_should_exist_ref"]
  #[no_mangle]
  pub extern fn should_exist_ref(&self) {
    println!("should_exist_ref");
  }

  #[export_name="SelfTypeTestStruct_should_exist_ref_mut"]
  #[no_mangle]
  pub extern fn should_exist_ref_mut(&mut self) {
    println!("should_exist_ref_mut");
  }

  #[export_name="SelfTypeTestStruct_should_not_exist_box"]
  #[no_mangle]
  pub extern fn should_not_exist_box(self: Box<SelfTypeTestStruct>) {
    println!("should_not_exist_box");
  }

  #[export_name="SelfTypeTestStruct_should_not_exist_return_box"]
  #[no_mangle]
  pub extern fn should_not_exist_box() -> Box<Self> {
    println!("should_not_exist_box");
  }

  #[export_name="SelfTypeTestStruct_should_exist_annotated_self"]
  #[no_mangle]
  pub extern fn should_exist_annotated_self(self: Self) {
    println!("should_exist_annotated_self");
  }

  #[export_name="SelfTypeTestStruct_should_exist_annotated_mut_self"]
  #[no_mangle]
  #[allow(unused_mut)]
  pub extern fn should_exist_annotated_mut_self(mut self: Self) {
    println!("should_exist_annotated_mut_self");
  }

  #[export_name="SelfTypeTestStruct_should_exist_annotated_by_name"]
  #[no_mangle]
  pub extern fn should_exist_annotated_by_name(self: SelfTypeTestStruct) {
    println!("should_exist_annotated_by_name");
  }

  #[export_name="SelfTypeTestStruct_should_exist_annotated_mut_by_name"]
  #[no_mangle]
  #[allow(unused_mut)]
  pub extern fn should_exist_annotated_mut_by_name(mut self: SelfTypeTestStruct) {
    println!("should_exist_annotated_mut_by_name");
  }

  #[export_name="SelfTypeTestStruct_should_exist_unannotated"]
  #[no_mangle]
  pub extern fn should_exist_unannotated(self) {
    println!("should_exist_unannotated");
  }

  #[export_name="SelfTypeTestStruct_should_exist_mut_unannotated"]
  #[no_mangle]
  #[allow(unused_mut)]
  pub extern fn should_exist_mut_unannotated(mut self) {
    println!("should_exist_mut_unannotated");
  }
}

#[no_mangle]
#[allow(unused_variables)]
pub extern fn free_function_should_exist_ref(test_struct: &SelfTypeTestStruct) {
  println!("free_function_should_exist_ref");
}

#[no_mangle]
#[allow(unused_variables)]
pub extern fn free_function_should_exist_ref_mut(test_struct: &mut SelfTypeTestStruct) {
  println!("free_function_should_exist_ref_mut");
}

#[no_mangle]
pub extern fn unnamed_argument(_: &mut SelfTypeTestStruct) {
  println!("unnamed_argument");
}

#[no_mangle]
#[allow(unused_variables)]
pub extern fn free_function_should_not_exist_box(boxed: Box<SelfTypeTestStruct>) {
  println!("free_function_should_not_exist_box");
}

#[no_mangle]
#[allow(unused_variables)]
pub extern fn free_function_should_exist_annotated_by_name(test_struct: SelfTypeTestStruct) {
  println!("free_function_should_exist_annotated_by_name");
}

#[no_mangle]
#[allow(unused_mut)]
#[allow(unused_variables)]
pub extern fn free_function_should_exist_annotated_mut_by_name(mut test_struct: SelfTypeTestStruct) {
  println!("free_function_should_exist_annotated_mut_by_name");
}

struct Opaque {
  times: u8
}

#[repr(C)]
pub struct PointerToOpaque { ptr: *mut Opaque }

impl PointerToOpaque {
  #[export_name="PointerToOpaque_create"]
  pub extern fn create(times: u8) -> PointerToOpaque {
    PointerToOpaque { ptr: Box::into_raw(Box::new(Opaque { times })) }
  }

  #[export_name="PointerToOpaque_sayHello"]
  pub extern fn say_hello(self: PointerToOpaque) {
    if let Some(nonnull) = std::ptr::NonNull::new(self.ptr) {
      for _ in 0 .. unsafe { nonnull.as_ref().times } {
        println!("Hello!")
      }
    }
  }
}