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