@@ -788,7 +788,7 @@ extern mod complicated_mod = "some-file/in/the-rust/path";
788
788
##### Use declarations
789
789
790
790
~~~~~~~~ {.ebnf .gram}
791
- use_decl : "pub"? "use" ident [ '=' path
791
+ use_decl : "pub" ? "use" ident [ '=' path
792
792
| "::" path_glob ] ;
793
793
794
794
path_glob : ident [ "::" path_glob ] ?
@@ -1006,20 +1006,25 @@ code_. They are defined in the same way as any other Rust function,
1006
1006
except that they have the ` extern ` modifier.
1007
1007
1008
1008
~~~
1009
+ // Declares an extern fn, the ABI defaults to "C"
1009
1010
extern fn new_vec() -> ~[int] { ~[] }
1011
+
1012
+ // Declares an extern fn with "stdcall" ABI
1013
+ extern "stdcall" fn new_vec_stdcall() -> ~[int] { ~[] }
1010
1014
~~~
1011
1015
1012
- Extern functions may not be called from Rust code,
1013
- but Rust code may take their value as a raw ` u8 ` pointer.
1016
+ Unlike normal functions, extern fns have an ` extern "ABI" fn() ` .
1017
+ This is the same type as the functions declared in an extern
1018
+ block.
1014
1019
1015
1020
~~~
1016
1021
# extern fn new_vec() -> ~[int] { ~[] }
1017
- let fptr: *u8 = new_vec;
1022
+ let fptr: extern "C" fn() -> ~[int] = new_vec;
1018
1023
~~~
1019
1024
1020
- The primary motivation for extern functions is
1021
- to create callbacks for foreign functions that expect to receive function
1022
- pointers .
1025
+ Extern functions may be called from Rust code, but
1026
+ caution must be taken with respect to the size of the stack
1027
+ segment, just as when calling an extern function normally .
1023
1028
1024
1029
### Type definitions
1025
1030
@@ -1384,14 +1389,13 @@ between the Rust ABI and the foreign ABI.
1384
1389
A number of [ attributes] ( #attributes ) control the behavior of external
1385
1390
blocks.
1386
1391
1387
- By default external blocks assume
1388
- that the library they are calling uses the standard C "cdecl" ABI.
1389
- Other ABIs may be specified using the ` abi ` attribute as in
1392
+ By default external blocks assume that the library they are calling
1393
+ uses the standard C "cdecl" ABI. Other ABIs may be specified using
1394
+ an ` abi ` string, as shown here:
1390
1395
1391
1396
~~~ {.xfail-test}
1392
1397
// Interface to the Windows API
1393
- #[abi = "stdcall"]
1394
- extern { }
1398
+ extern "stdcall" { }
1395
1399
~~~
1396
1400
1397
1401
The ` link_name ` attribute allows the name of the library to be specified.
@@ -1407,6 +1411,12 @@ This is particularly useful for creating external blocks for libc,
1407
1411
which tends to not follow standard library naming conventions
1408
1412
and is linked to all Rust programs anyway.
1409
1413
1414
+ The type of a function
1415
+ declared in an extern block
1416
+ is ` extern "abi" fn(A1, ..., An) -> R ` ,
1417
+ where ` A1...An ` are the declared types of its arguments
1418
+ and ` R ` is the decalred return type.
1419
+
1410
1420
## Attributes
1411
1421
1412
1422
~~~~~~~~ {.ebnf .gram}
@@ -1920,7 +1930,7 @@ it is automatically dereferenced to make the field access possible.
1920
1930
### Vector expressions
1921
1931
1922
1932
~~~~~~~~ {.ebnf .gram}
1923
- vec_expr : '[' "mut"? vec_elems? ']'
1933
+ vec_expr : '[' "mut" ? vec_elems? ']'
1924
1934
1925
1935
vec_elems : [expr [',' expr]*] | [expr ',' ".." expr]
1926
1936
~~~~~~~~
@@ -2864,17 +2874,16 @@ the vtable pointer for the `T` implementation of `R`, and the pointer value of `
2864
2874
An example of an object type:
2865
2875
2866
2876
~~~~~~~~
2867
- # use std::int;
2868
2877
trait Printable {
2869
- fn to_str (&self) -> ~str;
2878
+ fn to_string (&self) -> ~str;
2870
2879
}
2871
2880
2872
2881
impl Printable for int {
2873
- fn to_str (&self) -> ~str { int:: to_str(*self ) }
2882
+ fn to_string (&self) -> ~str { self. to_str() }
2874
2883
}
2875
2884
2876
2885
fn print(a: @Printable) {
2877
- println(a.to_str ());
2886
+ println(a.to_string ());
2878
2887
}
2879
2888
2880
2889
fn main() {
0 commit comments