Skip to content

Commit 772028a

Browse files
committed
doc: Add some info about native mods and crust functions
1 parent db77c38 commit 772028a

File tree

1 file changed

+79
-1
lines changed

1 file changed

+79
-1
lines changed

doc/rust.md

+79-1
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ of [attributes](#attributes) attached to it.
563563

564564
~~~~~~~~ {.ebnf .gram}
565565
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 ;
567567
~~~~~~~~
568568

569569
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
10151015
parameters to allow methods of that interface to be called on values
10161016
of that type.
10171017

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+
10181041
### Type definitions
10191042

10201043
A _type definition_ defines a new name for an existing [type](#types). Type
@@ -1222,6 +1245,61 @@ impl of seq<bool> for u32 {
12221245
}
12231246
~~~~
12241247

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+
12251303
## Attributes
12261304

12271305
~~~~~~~~{.ebnf .gram}

0 commit comments

Comments
 (0)