@@ -11,14 +11,14 @@ it talks to itself. For this reason, **Rust makes it easy to communicate with C
11
11
APIs without overhead, and to leverage its ownership system to provide much
12
12
stronger safety guarantees for those APIs at the same time** .
13
13
14
- In more detail , Rust's * foreign function interface * (FFI) is the way that it
15
- communicated with other languages . Following Rust's design principles, the FFI
16
- provides a ** zero-cost abstraction** where function calls between Rust and C
17
- have identical performance to C function calls. FFI bindings can also leverage
18
- language features such as ownership and borrowing to provide a ** safe
19
- interface ** that enforces protocols around pointers and other resources. These
20
- protocols usually appear only in the documentation for C APIs -- at best -- but
21
- Rust makes them explicit.
14
+ To communicate with other languages , Rust provides a * foreign function
15
+ interface * (FFI) . Following Rust's design principles, the FFI provides a
16
+ ** zero-cost abstraction** where function calls between Rust and C have identical
17
+ performance to C function calls. FFI bindings can also leverage language
18
+ features such as ownership and borrowing to provide a ** safe interface ** that
19
+ enforces protocols around pointers and other resources. These protocols usually
20
+ appear only in the documentation for C APIs -- at best -- but Rust makes them
21
+ explicit.
22
22
23
23
In this post we'll explore how to encapsulate unsafe FFI calls to C in safe,
24
24
zero-cost abstractions. Working with C is, however, just an example; we'll also
@@ -157,16 +157,14 @@ impl Tarball {
157
157
```
158
158
159
159
Here the ` *mut tarball_t ` pointer is * owned by* a ` Tarball ` , which is
160
- responsible for any destruction and cleanup. So we already have rich knowledge
161
- about the lifetime of the resource: if you have access to a ` Tarball ` , you know
162
- that the pointer inside must still be valid. Additionally, the ` file ` method
160
+ responsible for any destruction and cleanup, so we already have rich knowledge
161
+ about the lifetime of the tarball's memory. Additionally, the ` file ` method
163
162
returns a ** borrowed slice** whose lifetime is implicitly connected to the
164
163
lifetime of the source tarball itself (the ` &self ` argument). This is Rust's way
165
164
of indicating that the returned slice can only be used within the lifetime of
166
- the tarball, which in turn means that the slice will always point to valid
167
- memory. Thus, Rust statically prevents dangling pointer bugs that are easy to
165
+ the tarball, statically preventing dangling pointer bugs that are easy to
168
166
make when working directly with C. (If you're not familiar with this kind of
169
- borrowing in Rust, have a look at Yehuda Katz's [ blog post] on ownership.)
167
+ borrowing in Rust, have a look at Yehuda Katz's [ blog post on ownership] .)
170
168
171
169
[ blog post ] : http://blog.skylight.io/rust-means-never-having-to-close-a-socket/
172
170
0 commit comments