You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
letx=b'A'asu32; // ascii character indecies are the same as Unicode character indecies
57
+
lety=f(x);
58
+
assert_eq!(y, 'A');
59
+
}
60
+
```
61
+
31
62
r[abi.compatibility.pointer]
32
63
Two [pointer types], `*mut T` and `*const U`, are *abi compatible* if the *metadata type*s of `T` and `U` are the same type.
33
64
@@ -37,12 +68,40 @@ Two [pointer types], `*mut T` and `*const U`, are *abi compatible* if the *metad
37
68
> [!NOTE]
38
69
> With transitivity, this applies regardless of the mutability of either pointer type
39
70
71
+
```rust
72
+
unsafefnfoo(x:*muti32){
73
+
unsafe{x.write(5);}
74
+
}
75
+
76
+
fnmain(){
77
+
letf:unsafefn(*mut ()) =unsafe{core::mem::transmute(fooasunsafefn(_))}; // Type Erase the function
78
+
letmutval=0;
79
+
letptr=core::ptr::addr_of_mut!(val).cast::<()>(); // Get Opaque Userdata from somewhere
80
+
unsafe{f(ptr);}
81
+
assert_eq!(val, 5);
82
+
}
83
+
```
84
+
40
85
r[abi.compatibility.reference-box]
41
86
The types [`&T`], [`&mut T`], [`alloc::boxed::Box<T>`], and [`core::ptr::NonNull<T>`], are *abi compatible* with `*const T`
42
87
43
88
> [!NOTE]
44
89
> With transitivity, they are also *abi compatible* with each other, and with `*mut T`, as well as references/`Box` to different types that have the same *metadata type*.
45
90
91
+
```rust
92
+
fnfoo(x:&muti32){
93
+
*x=5;
94
+
}
95
+
96
+
fnmain(){
97
+
letf:unsafefn(*mut ()) =unsafe{core::mem::transmute(fooasfn(_))}; // Type Erase the function
98
+
letmutval=0;
99
+
letptr=core::ptr::addr_of_mut!(val).cast::<()>(); // Get Opaque Userdata from somewhere
100
+
unsafe{f(ptr);}
101
+
assert_eq!(val, 5);
102
+
}
103
+
```
104
+
46
105
r[abi.compatibility.core]
47
106
The types [`core::mem::MaybeUninit<T>`], [`core::cell::UnsafeCell<T>`], and [`core::num::NonZero<T>`], are *abi compatible* with `T`.
48
107
@@ -55,6 +114,7 @@ Two types, `T` and `U`, are *abi compatible* if both have size 0 and alignment 1
55
114
r[abi.compatibility.option]
56
115
If `T` is a type listed in [layout.enum.option](https://doc.rust-lang.org/stable/core/option/index.html#representation), then given `S` is a type with size 0 and alignment 1, `T` is *abi compatible* with the types [`core::option::Option<T>`], [`core::result::Result<T,S>`], and [`core::result::Result<S,T>`].
57
116
117
+
58
118
r[abi.compatibility.fn-ptr]
59
119
An [`fn`-ptr type]`T` is compatible with an [`fn`-ptr type]`U` if `T` and `U` have *abi compatible* tags.
60
120
@@ -97,6 +157,9 @@ A call to a function `f` via a function item or function pointer with a given si
97
157
98
158
The behaviour a call that is not valid is undefined.
99
159
160
+
> [!NOTE]
161
+
> When parameter/return types do not exactly match, they are converted as though by calling [`core::mem::transmute`]. The representation and validity requirements of the type in the definition/return site still apply, for example, passing `0` to a function pointer `fn(u32)` that points to a function declared as `fn foo(x: NonZeroU32)` is undefined behaviour.
162
+
100
163
> [!NOTE]
101
164
> the ABI tag `extern "Rust"` is the default when the `extern` keyword is not used (either to declare the function within an [`extern` block], or as a [function qualifier][extern functions]). Thus it is safe to call most functions that use simd types.
102
165
@@ -129,6 +192,11 @@ The *`used` attribute* may be specified as a built-in attribute, using the [_Met
129
192
r[abi.used.restriction]
130
193
The `used` attribute shall only be applied to a `static` item. It shall not be applied to a `static` item declared within an [`extern` block].
131
194
195
+
```rust
196
+
#[used]
197
+
staticFOO:u32=0;
198
+
```
199
+
132
200
r[abi.used.application]
133
201
A `static` item with the `used` attribute is an *exported item*.
The *`no_mangle` attribute* and the *`export_name` attribute* shall only be applied to a `static` or `fn` item. The *`export_name` attribute* shall not be applied to an item declared within an [`extern` block].
191
259
260
+
```rust
261
+
#[no_mangle]
262
+
extern"C"fnfoo(x:i32) ->i32 {
263
+
x+1
264
+
}
265
+
266
+
#[export_name ="bar"]
267
+
extern"C"fnbaz(x:i32) ->i32 {
268
+
x+2
269
+
}
270
+
```
271
+
272
+
```rust,compile_fail
273
+
extern "C" {
274
+
#[export_name = "foo"]
275
+
fn __foo(x: i32) -> i32;
276
+
}
277
+
```
278
+
192
279
> [!NOTE]
193
280
> They may be applied to an associated `fn` of an `impl` block.
194
281
282
+
195
283
r[abi.symbol-name.exported]
196
284
An item with either the *`no_mangle` attrbute* or the *`export_name` attribute* is an *exported item*.
197
285
198
286
r[abi.symbol-name.no_mangle]
199
287
The *`no_mangle` attribute* may be specified as a built-in attribute, using the [_MetaWord_] syntax. The *export name* of an item with the *`no_mangle` attribute* is the declaration name of the item.
200
288
289
+
```rust
290
+
extern"C" {
291
+
fnbar() ->i32;
292
+
}
293
+
modinner{
294
+
#[no_mangle]
295
+
extern"C"fnbar() ->i32 {
296
+
0
297
+
}
298
+
}
299
+
300
+
fnmain() {
301
+
lety=unsafe {bar()};
302
+
assert_eq!(y,0);
303
+
}
304
+
```
305
+
201
306
r[abi.symbol-name.export_name]
202
307
The *`export_name` attribute* may be specified as a built-in attribute, using the [_MetaNameValueStr_] syntax. The *export name* of an item with the *`no_mangle` attribute* is the content of `STRING_LITERAL`.
203
308
309
+
```rust
310
+
extern"C" {
311
+
fnbar() ->i32;
312
+
}
313
+
modinner{
314
+
#[export_name ="bar"]
315
+
extern"C"fn__some_other_item_name() ->i32 {
316
+
0
317
+
}
318
+
}
319
+
320
+
fnmain(){
321
+
lety=unsafe {bar()};
322
+
assert_eq!(y,0);
323
+
}
324
+
```
325
+
204
326
## The `link_section` attribute
205
327
206
328
r[abi.link_section]
@@ -215,6 +337,13 @@ The *`link_section` attribute* may be specified as a built-in attribute, using t
215
337
r[abi.link_section.restriction]
216
338
The *`link_section` attribute* shall be aplied to a `static` or `fn` item.
217
339
340
+
<!-- no_run: don't link. The format of the section name is platform-specific. -->
341
+
```rust,no_run
342
+
#[no_mangle]
343
+
#[link_section = ".example_section"]
344
+
pub static VAR1: u32 = 1;
345
+
```
346
+
218
347
r[abi.link_section.def]
219
348
An item with the *`link_section` attribute* is placed in the specified section when linking. The section specified shall not violate the constraints on section names on the target, and shall not be invalid for the item type, no diagnostic is required.
220
349
@@ -224,12 +353,6 @@ An item with the *`link_section` attribute* is placed in the specified section w
224
353
>
225
354
> The result of using an invalid section name may be that the section is placed into the section but cannot be used as applicable, or that the section is given additional attributes that may be incompatible when linking.
226
355
227
-
<!-- no_run: don't link. The format of the section name is platform-specific. -->
228
-
```rust,no_run
229
-
#[no_mangle]
230
-
#[link_section = ".example_section"]
231
-
pub static VAR1: u32 = 1;
232
-
```
233
356
234
357
> [!TARGET-SPECIFIC]
235
358
> On ELF Platforms, the standard section names, and their attributes are:
@@ -242,6 +365,7 @@ pub static VAR1: u32 = 1;
242
365
>
243
366
> This is not an exhaustive list, and generally extended versions of these section names such as `.text.foo`, are also defined with the same properties as the base section.
0 commit comments