Skip to content

Commit d9eb4c3

Browse files
bors[bot]abt8601
andauthored
Merge #351
351: Update Interoperability section r=eldruin a=abt8601 Following the discussion on #348, this PR proposes the following changes to the Interoperability section: - Update descriptions regarding the availability of `core::ffi`. - Remove mentions of `std::os::raw`. I intentionally kept mentions of the `cty` crate and the `cstr_core` crate since the next section uses the former. We may also consider updating the following section to use `core::ffi` instead of the `cty` crate. This PR also introduces two additional changes. First, it changes the descriptions regarding the implicit conversion between C and Rust types. The book says that the compiler implicitly converts between the two types, but it doesn't sound right to say that the example (reproduced below) works because of the conversion. It works simply because, on platforms where `unsigned int` is 32-bit long, `c_uint` and `u32` are the same type because the former is a type alias of the latter. This PR also removes the `unsafe` from the example since I believe it plays no role. ```rust unsafe fn foo(num: u32) { let c_num: c_uint = num; let r_num: u32 = c_num; } ``` Second, it changes the text formatting in the type correspondence table. The types in the table now use code formatting. Also, the presentation of C pointer types is updated (e.g. `*char` → `char *`). Closes #348. Co-authored-by: Po-Yi Tsai <abt8601@protonmail.ch>
2 parents f27632c + 7318666 commit d9eb4c3

File tree

1 file changed

+26
-25
lines changed

1 file changed

+26
-25
lines changed

src/interoperability/index.md

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,42 @@
22

33
Interoperability between Rust and C code is always dependent
44
on transforming data between the two languages.
5-
For this purposes there are two dedicated modules
5+
For this purpose, there is a dedicated module
66
in the `stdlib` called
7-
[`std::ffi`](https://doc.rust-lang.org/std/ffi/index.html) and
8-
[`std::os::raw`](https://doc.rust-lang.org/std/os/raw/index.html).
7+
[`std::ffi`](https://doc.rust-lang.org/std/ffi/index.html).
98

10-
`std::os::raw` deals with low-level primitive types that can
11-
be converted implicitly by the compiler
12-
because the memory layout between Rust and C
13-
is similar enough or the same.
9+
`std::ffi` provides type definitions for C primitive types,
10+
such as `char`, `int`, and `long`.
11+
It also provides some utility for converting more complex
12+
types such as strings, mapping both `&str` and `String`
13+
to C types that are easier and safer to handle.
1414

15-
`std::ffi` provides some utility for converting more complex
16-
types such as Strings, mapping both `&str` and `String`
17-
to C-types that are easier and safer to handle.
18-
19-
Neither of these modules are available in `core`, but you can find a `#![no_std]`
20-
compatible version of `std::ffi::{CStr,CString}` in the [`cstr_core`] crate, and
21-
most of the `std::os::raw` types in the [`cty`] crate.
15+
As of Rust 1.30,
16+
functionalities of `std::ffi` are available
17+
in either `core::ffi` or `alloc::ffi`
18+
depending on whether or not memory allocation is involved.
19+
The [`cty`] crate and the [`cstr_core`] crate
20+
also offer similar functionalities.
2221

2322
[`cstr_core`]: https://crates.io/crates/cstr_core
2423
[`cty`]: https://crates.io/crates/cty
2524

26-
| Rust type | Intermediate | C type |
27-
|------------|--------------|--------------|
28-
| String | CString | *char |
29-
| &str | CStr | *const char |
30-
| () | c_void | void |
31-
| u32 or u64 | c_uint | unsigned int |
32-
| etc | ... | ... |
25+
| Rust type | Intermediate | C type |
26+
|----------------|--------------|----------------|
27+
| `String` | `CString` | `char *` |
28+
| `&str` | `CStr` | `const char *` |
29+
| `()` | `c_void` | `void` |
30+
| `u32` or `u64` | `c_uint` | `unsigned int` |
31+
| etc | ... | ... |
3332

34-
As mentioned above, primitive types can be converted
35-
by the compiler implicitly.
33+
A value of a C primitive type can be used
34+
as one of the corresponding Rust type and vice versa,
35+
since the former is simply a type alias of the latter.
36+
For example, the following code compiles on platforms
37+
where `unsigned int` is 32-bit long.
3638

3739
```rust,ignore
38-
unsafe fn foo(num: u32) {
40+
fn foo(num: u32) {
3941
let c_num: c_uint = num;
4042
let r_num: u32 = c_num;
4143
}
@@ -51,7 +53,6 @@ We are collecting examples and use cases for this on our issue tracker in
5153

5254
[issue #61]: https://github.com/rust-embedded/book/issues/61
5355

54-
5556
## Interoperability with RTOSs
5657

5758
Integrating Rust with an RTOS such as FreeRTOS or ChibiOS is still a work in

0 commit comments

Comments
 (0)