Skip to content

Commit 2fb3fb2

Browse files
committed
Auto merge of #27836 - alexcrichton:rename-cstring-raw, r=bluss
This commit renames the `CString::{into_ptr, from_ptr}` methods to `into_raw` and `from_raw` to mirror the corresponding methods on `Box` and the naming of "raw" for `from_raw_parts` on slices and vectors. cc #27769
2 parents 1d72d31 + 3cf9e10 commit 2fb3fb2

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

src/libstd/ffi/c_str.rs

+28-5
Original file line numberDiff line numberDiff line change
@@ -207,15 +207,39 @@ impl CString {
207207
/// using the pointer.
208208
#[unstable(feature = "cstr_memory", reason = "recently added",
209209
issue = "27769")]
210-
// NB: may want to be called from_raw, needs to consider CStr::from_ptr,
211-
// Box::from_raw (or whatever it's currently called), and
212-
// slice::from_raw_parts
210+
#[deprecated(since = "1.4.0", reason = "renamed to from_raw")]
213211
pub unsafe fn from_ptr(ptr: *const libc::c_char) -> CString {
212+
CString::from_raw(ptr)
213+
}
214+
215+
/// Retakes ownership of a CString that was transferred to C.
216+
///
217+
/// The only appropriate argument is a pointer obtained by calling
218+
/// `into_raw`. The length of the string will be recalculated
219+
/// using the pointer.
220+
#[unstable(feature = "cstr_memory", reason = "recently added",
221+
issue = "27769")]
222+
pub unsafe fn from_raw(ptr: *const libc::c_char) -> CString {
214223
let len = libc::strlen(ptr) + 1; // Including the NUL byte
215224
let slice = slice::from_raw_parts(ptr, len as usize);
216225
CString { inner: mem::transmute(slice) }
217226
}
218227

228+
/// Transfers ownership of the string to a C caller.
229+
///
230+
/// The pointer must be returned to Rust and reconstituted using
231+
/// `from_raw` to be properly deallocated. Specifically, one
232+
/// should *not* use the standard C `free` function to deallocate
233+
/// this string.
234+
///
235+
/// Failure to call `from_raw` will lead to a memory leak.
236+
#[unstable(feature = "cstr_memory", reason = "recently added",
237+
issue = "27769")]
238+
#[deprecated(since = "1.4.0", reason = "renamed to into_raw")]
239+
pub fn into_ptr(self) -> *const libc::c_char {
240+
self.into_raw()
241+
}
242+
219243
/// Transfers ownership of the string to a C caller.
220244
///
221245
/// The pointer must be returned to Rust and reconstituted using
@@ -226,8 +250,7 @@ impl CString {
226250
/// Failure to call `from_ptr` will lead to a memory leak.
227251
#[unstable(feature = "cstr_memory", reason = "recently added",
228252
issue = "27769")]
229-
// NB: may want to be called into_raw, see comments on from_ptr
230-
pub fn into_ptr(self) -> *const libc::c_char {
253+
pub fn into_raw(self) -> *const libc::c_char {
231254
// It is important that the bytes be sized to fit - we need
232255
// the capacity to be determinable from the string length, and
233256
// shrinking to fit is the only way to be sure.

0 commit comments

Comments
 (0)