4
4
5
5
# Summary
6
6
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.
20
14
21
15
# Motivation
22
16
@@ -46,8 +40,7 @@ originating from C.
46
40
47
41
# Detailed design
48
42
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:
51
44
52
45
1 . A Rust type wants to be passed into C.
53
46
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
57
50
once, somewhat conflating desires. Additionally, ` CVec ` provides a fairly
58
51
different interface than ` CString ` while providing similar functionality.
59
52
60
- ## A new ` std::c_string `
53
+ ## A new ` std::ffi `
61
54
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 ]
64
57
65
58
[ c_str ] : https://github.com/alexcrichton/rust/blob/cstr/src/librustrt/c_str.rs
66
59
67
60
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 ` :
69
62
70
63
``` rust
71
64
#[deriving(Clone , PartialEq , PartialOrd , Eq , Ord , Hash )]
@@ -85,8 +78,8 @@ impl CString {
85
78
impl Deref <[libc :: c_char ]> for CString { /* ... */ }
86
79
impl Show for CString { /* ... */ }
87
80
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 ] { /* ... */ }
90
83
```
91
84
92
85
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
134
127
following functions will also be deprecated:
135
128
136
129
* ` 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
138
131
` str::from_utf8_unchecked ` .
139
132
* ` String::from_raw_buf ` - similarly to ` from_c_str ` , each step should be
140
133
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() ` .
142
135
* ` String::from_raw_buf_len ` - this should be replaced the same way as
143
136
` String::from_raw_buf ` except that ` slice::from_raw_buf ` is used instead of
144
- ` c_str ` .
137
+ ` ffi ` .
145
138
146
139
## Removing ` c_vec `
147
140
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
149
142
the third use case is left unsolved so far. This is what the current ` c_vec `
150
143
module is attempting to solve, but it does so in a somewhat ad-hoc fashion. The
151
144
constructor for the type takes a ` proc ` destructor to invoke when the vector is
0 commit comments