@@ -563,7 +563,7 @@ of [attributes](#attributes) attached to it.
563
563
564
564
~~~~~~~~ {.ebnf .gram}
565
565
item : mod_item | fn_item | type_item | enum_item
566
- | res_item | iface_item | impl_item ;
566
+ | res_item | iface_item | impl_item | native_mod_item ;
567
567
~~~~~~~~
568
568
569
569
An _ item_ is a component of a crate; some module items can be defined in crate
@@ -1015,6 +1015,29 @@ Similarly, [interface](#interfaces) bounds can be specified for type
1015
1015
parameters to allow methods of that interface to be called on values
1016
1016
of that type.
1017
1017
1018
+ #### Crust functions
1019
+
1020
+ Crust functions are part of Rust's foreign function interface,
1021
+ providing the opposite functionality to [ native modules] ( #native-modules ) .
1022
+ Whereas native modules allow Rust code to call foreign
1023
+ code, crust functions allow foreign code to call Rust code. They are
1024
+ defined the same as any other Rust function, except that they are
1025
+ prepended with the ` crust ` keyword.
1026
+
1027
+ ~~~
1028
+ crust fn new_vec() -> [int] { [] }
1029
+ ~~~
1030
+
1031
+ Crust functions may not be called from Rust code, but their value
1032
+ may be taken as an unsafe ` u8 ` pointer.
1033
+
1034
+ ~~~
1035
+ let fptr: *u8 = new_vec;
1036
+ ~~~
1037
+
1038
+ The primary motivation of crust functions is to create callbacks
1039
+ for native functions that expect to receive function pointers.
1040
+
1018
1041
### Type definitions
1019
1042
1020
1043
A _ type definition_ defines a new name for an existing [ type] ( #types ) . Type
@@ -1222,6 +1245,61 @@ impl of seq<bool> for u32 {
1222
1245
}
1223
1246
~~~~
1224
1247
1248
+ ### Native modules
1249
+
1250
+ ~~~ {.ebnf .gram}
1251
+ native_mod_item : "native mod" ident '{' native_mod '} ;
1252
+ native_mod : [ native_fn ] * ;
1253
+ ~~~
1254
+
1255
+ Native modules form the basis for Rust's foreign function interface. A native
1256
+ module describes functions in external, non-Rust libraries. Functions within
1257
+ native modules are declared the same as other Rust functions, with the exception
1258
+ that they may not have a body and are instead terminated by a semi-colon.
1259
+
1260
+ ~~~
1261
+ native mod c {
1262
+ fn fopen(filename: *c_char, mod: *c_char) -> *FILE;
1263
+ }
1264
+ ~~~
1265
+
1266
+ Functions within native modules may be called by Rust code as it would any
1267
+ normal function and the Rust compiler will automatically translate between
1268
+ the Rust ABI and the native ABI.
1269
+
1270
+ The name of the native module has special meaning to the Rust compiler in
1271
+ that it will treat the module name as the name of a library to link to,
1272
+ performing the linking as appropriate for the target platform. The name
1273
+ given for the native module will be transformed in a platform-specific
1274
+ way to determine the name of the library. For example, on Linux the name
1275
+ of the native module is prefixed with 'lib' and suffixed with '.so', so
1276
+ the native mod 'rustrt' would be linked to a library named 'librustrt.so'.
1277
+
1278
+ A number of [ attributes] ( #attributes ) control the behavior of native mods.
1279
+
1280
+ By default native mods assume that the library they are calling use
1281
+ the standard C "cdecl" ABI. Other ABI's may be specified using the ` abi `
1282
+ attribute as in
1283
+
1284
+ ~~~
1285
+ // Interface to the Windows API
1286
+ #[abi = "stdcall"]
1287
+ native mod kernel32 { }
1288
+ ~~~
1289
+
1290
+ The ` link_name ` attribute allows the default library naming behavior to
1291
+ be overriden by explicitly specifying the name of the library.
1292
+
1293
+ ~~~
1294
+ #[link_name = "crypto"]
1295
+ native mod mycrypto { }
1296
+ ~~~
1297
+
1298
+ The ` nolink ` attribute tells the Rust compiler not to perform any linking
1299
+ for the native module. This is particularly useful for creating native
1300
+ mods for libc, which tends to not follow standard library naming conventions
1301
+ and is linked to all Rust programs anyway.
1302
+
1225
1303
## Attributes
1226
1304
1227
1305
~~~~~~~~ {.ebnf .gram}
0 commit comments