Skip to content

Commit d696e52

Browse files
committed
Update to add std::ffi
1 parent 392cb9f commit d696e52

File tree

1 file changed

+18
-25
lines changed

1 file changed

+18
-25
lines changed

text/0000-c_str-and-c_vec-stability.md

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,13 @@
44

55
# Summary
66

7-
Stabilize the `std::{c_str, c_vec}` modules by re-working their interfaces and
8-
refocusing each primitive for one particular task. The three broad categories of
9-
interoperating with C will work via:
10-
11-
1. If you have a Rust string/byte slice which needs to be given to C, then the
12-
`CString` type will be used to statically guarantee that a terminating nul
13-
character and no interior nuls exist.
14-
15-
2. If C hands you a string which you want to inspect, but not own, then a helper
16-
function will assist in converting the C string to a byte slice.
17-
18-
3. If C hands you a string which you want to inspect and own, then a helper type
19-
will consume ownership and will act as a `Box<[u8]>` in essence.
7+
* Remove the `std::c_vec` module
8+
* Move `std::c_str` under a new `std::ffi` module, not exporting the `c_str`
9+
module.
10+
* Focus `CString` on *Rust-owned* bytes, providing a static assertion that a
11+
pile of bytes has no interior nuls but has a trailing nul.
12+
* Provide convenience functions for translating *C-owned* types into slices in
13+
Rust.
2014

2115
# Motivation
2216

@@ -46,8 +40,7 @@ originating from C.
4640

4741
# Detailed design
4842

49-
In refactoring `c_str` and `c_vec`, all usage could be categorized into one of
50-
three categories:
43+
In refactoring all usage could be categorized into one of three categories:
5144

5245
1. A Rust type wants to be passed into C.
5346
2. A C type was handed to Rust, but Rust does not own it.
@@ -57,15 +50,15 @@ The current `CString` attempts to handle all three of these concerns all at
5750
once, somewhat conflating desires. Additionally, `CVec` provides a fairly
5851
different interface than `CString` while providing similar functionality.
5952

60-
## A new `std::c_string`
53+
## A new `std::ffi`
6154

62-
> **Note**: an implementation of the design below can be found [in a branch of
63-
> mine][c_str]
55+
> **Note**: an old implementation of the design below can be found [in a branch
56+
> of mine][c_str]
6457
6558
[c_str]: https://github.com/alexcrichton/rust/blob/cstr/src/librustrt/c_str.rs
6659

6760
The entire `c_str` module will be deleted as-is today and replaced with the
68-
following interface at the new location `std::c_string`:
61+
following interface at the new location `std::ffi`:
6962

7063
```rust
7164
#[deriving(Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
@@ -85,8 +78,8 @@ impl CString {
8578
impl Deref<[libc::c_char]> for CString { /* ... */ }
8679
impl Show for CString { /* ... */ }
8780

88-
pub unsafe fn from_raw_buf<'a>(raw: &'a *const libc::c_char) -> &'a [u8] { /* ... */ }
89-
pub unsafe fn from_raw_buf_with_nul<'a>(raw: &'a *const libc::c_char) -> &'a [u8] { /* ... */ }
81+
pub unsafe fn c_str_to_bytes<'a>(raw: &'a *const libc::c_char) -> &'a [u8] { /* ... */ }
82+
pub unsafe fn c_str_to_bytes_with_nul<'a>(raw: &'a *const libc::c_char) -> &'a [u8] { /* ... */ }
9083
```
9184

9285
The new `CString` API is focused solely on providing a static assertion that a
@@ -134,18 +127,18 @@ interpreted in Rust as a `u8` slice. With these two functions, all of the
134127
following functions will also be deprecated:
135128

136129
* `std::str::from_c_str` - this function should be replaced with
137-
`c_str::from_raw_buf` plus one of `str::from_utf8` or
130+
`ffi::c_str_to_bytes` plus one of `str::from_utf8` or
138131
`str::from_utf8_unchecked`.
139132
* `String::from_raw_buf` - similarly to `from_c_str`, each step should be
140133
composed individually to perform the required checks. This would involve using
141-
`c_str::from_raw_buf`, `str::from_utf8`, and `.to_string()`.
134+
`ffi::c_str_to_bytes`, `str::from_utf8`, and `.to_string()`.
142135
* `String::from_raw_buf_len` - this should be replaced the same way as
143136
`String::from_raw_buf` except that `slice::from_raw_buf` is used instead of
144-
`c_str`.
137+
`ffi`.
145138

146139
## Removing `c_vec`
147140

148-
The new `c_string` module serves as a solution to desires (1) and (2) above, but
141+
The new `ffi` module serves as a solution to desires (1) and (2) above, but
149142
the third use case is left unsolved so far. This is what the current `c_vec`
150143
module is attempting to solve, but it does so in a somewhat ad-hoc fashion. The
151144
constructor for the type takes a `proc` destructor to invoke when the vector is

0 commit comments

Comments
 (0)