Skip to content

std: Rename cstr_memory feature to use "raw" #27836

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 18, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions src/libstd/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,39 @@ impl CString {
/// using the pointer.
#[unstable(feature = "cstr_memory", reason = "recently added",
issue = "27769")]
// NB: may want to be called from_raw, needs to consider CStr::from_ptr,
// Box::from_raw (or whatever it's currently called), and
// slice::from_raw_parts
#[deprecated(since = "1.4.0", reason = "renamed to from_raw")]
pub unsafe fn from_ptr(ptr: *const libc::c_char) -> CString {
CString::from_raw(ptr)
}

/// Retakes ownership of a CString that was transferred to C.
///
/// The only appropriate argument is a pointer obtained by calling
/// `into_raw`. The length of the string will be recalculated
/// using the pointer.
#[unstable(feature = "cstr_memory", reason = "recently added",
issue = "27769")]
pub unsafe fn from_raw(ptr: *const libc::c_char) -> CString {
let len = libc::strlen(ptr) + 1; // Including the NUL byte
let slice = slice::from_raw_parts(ptr, len as usize);
CString { inner: mem::transmute(slice) }
}

/// Transfers ownership of the string to a C caller.
///
/// The pointer must be returned to Rust and reconstituted using
/// `from_raw` to be properly deallocated. Specifically, one
/// should *not* use the standard C `free` function to deallocate
/// this string.
///
/// Failure to call `from_raw` will lead to a memory leak.
#[unstable(feature = "cstr_memory", reason = "recently added",
issue = "27769")]
#[deprecated(since = "1.4.0", reason = "renamed to into_raw")]
pub fn into_ptr(self) -> *const libc::c_char {
self.into_raw()
}

/// Transfers ownership of the string to a C caller.
///
/// The pointer must be returned to Rust and reconstituted using
Expand All @@ -226,8 +250,7 @@ impl CString {
/// Failure to call `from_ptr` will lead to a memory leak.
#[unstable(feature = "cstr_memory", reason = "recently added",
issue = "27769")]
// NB: may want to be called into_raw, see comments on from_ptr
pub fn into_ptr(self) -> *const libc::c_char {
pub fn into_raw(self) -> *const libc::c_char {
// It is important that the bytes be sized to fit - we need
// the capacity to be determinable from the string length, and
// shrinking to fit is the only way to be sure.
Expand Down