@@ -21,38 +21,39 @@ Two kinds of item _declarations_ are allowed in external blocks: [functions] and
21
21
[ statics] . Calling functions or accessing statics that are declared in external
22
22
blocks is only allowed in an ` unsafe ` context.
23
23
24
- Starting in edition 2024, the ` unsafe ` keyword is required to appear before the
25
- ` extern ` keyword. In previous editions is accepted but not required.
24
+ ** Edition differences** : Starting in the 2024 edition, the ` unsafe ` keyword is
25
+ required to appear before the ` extern ` keyword on external blocks. In previous
26
+ editions, it is accepted but not required.
26
27
27
28
## Functions
28
29
29
30
Functions within external blocks are declared in the same way as other Rust
30
31
functions, with the exception that they must not have a body and are instead
31
32
terminated by a semicolon. Patterns are not allowed in parameters, only
32
- [ IDENTIFIER] or ` _ ` may be used. Only safety funcion qualifiers are allowed
33
- ( ` unsafe ` , ` safe ` ), other function qualifiers (` const ` , ` async ` , and ` extern ` )
34
- are not allowed .
33
+ [ IDENTIFIER] or ` _ ` may be used. The ` safe ` and ` unsafe ` function qualifiers are
34
+ allowed, but other function qualifiers (e.g. ` const ` , ` async ` , ` extern ` ) are
35
+ not.
35
36
36
37
Functions within external blocks may be called by Rust code, just like
37
38
functions defined in Rust. The Rust compiler automatically translates between
38
39
the Rust ABI and the foreign ABI.
39
40
40
- A function declared with an explicit safety qualifier (` unsafe ` , ` safe ` ) would
41
- take such safety qualification, if no qualifier is present is implicitly
42
- ` unsafe ` . When coerced to a function pointer, a function declared in an extern
43
- block has type ` unsafe extern "abi" for<'l1, ..., 'lm> fn(A1, ..., An) -> R ` ,
44
- where ` 'l1 ` , ... ` 'lm ` are its lifetime parameters, ` A1 ` , ..., ` An ` are the
45
- declared types of its parameters and ` R ` is the declared return type.
41
+ A function declared in an extern block is implicitly ` unsafe ` unless the ` safe `
42
+ function qualifier is present.
43
+
44
+ When coerced to a function pointer, a function declared in an extern block has
45
+ type ` extern "abi" for<'l1, ..., 'lm> fn(A1, ..., An) -> R ` , where ` 'l1 ` ,
46
+ ... ` 'lm ` are its lifetime parameters, ` A1 ` , ..., ` An ` are the declared types of
47
+ its parameters, ` R ` is the declared return type.
46
48
47
49
## Statics
48
50
49
51
Statics within external blocks are declared in the same way as [ statics] outside of external blocks,
50
52
except that they do not have an expression initializing their value.
51
- It is ` unsafe ` by default or if the item is declared as ` unsafe ` to access a static item declared in
52
- an extern block, unless the item was explicitly declared as ` safe ` .
53
- It does not matter if it's mutable, because there is nothing guaranteeing that the bit pattern at
54
- the static's memory is valid for the type it is declared with, since some arbitrary (e.g. C) code is
55
- in charge of initializing the static.
53
+ Unless a static item declared in an extern block is qualified as ` safe ` , it is ` unsafe ` to access that item, whether or
54
+ not it's mutable, because there is nothing guaranteeing that the bit pattern at the static's
55
+ memory is valid for the type it is declared with, since some arbitrary (e.g. C) code is in charge
56
+ of initializing the static.
56
57
57
58
Extern statics can be either immutable or mutable just like [ statics] outside of external blocks.
58
59
An immutable static * must* be initialized before any Rust code is executed. It is not enough for
@@ -105,9 +106,9 @@ identifier.
105
106
106
107
``` rust
107
108
unsafe extern " C" {
108
- fn foo (... );
109
- fn bar (x : i32 , ... );
110
- fn with_name (format : * const u8 , args : ... );
109
+ safe fn foo (... );
110
+ unsafe fn bar (x : i32 , ... );
111
+ unsafe fn with_name (format : * const u8 , args : ... );
111
112
}
112
113
```
113
114
@@ -278,7 +279,7 @@ uses the [_MetaNameValueStr_] syntax to specify the name of the symbol.
278
279
``` rust
279
280
unsafe extern {
280
281
#[link_name = " actual_symbol_name" ]
281
- fn name_in_rust ();
282
+ safe fn name_in_rust ();
282
283
}
283
284
```
284
285
@@ -307,7 +308,7 @@ it, and that assigned ordinal may change between builds of the binary.
307
308
#[link(name = "exporter", kind = "raw-dylib")]
308
309
unsafe extern "stdcall" {
309
310
#[link_ordinal(15)]
310
- fn imported_function_stdcall(i: i32);
311
+ safe fn imported_function_stdcall(i: i32);
311
312
}
312
313
```
313
314
0 commit comments