Skip to content

Commit 44e6573

Browse files
committed
Auto merge of #28198 - alexcrichton:from-raw-mut, r=aturon
Conventionally in C `*mut T` is a transfer of ownership where `*const T` is a loan, so `*mut T` is likely the more appropriate return type for these functions. Additionally, this more closely mirrors the APIs on `Box` for this sort of functionality. cc #27769
2 parents 24e54ae + 6bb2c5d commit 44e6573

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

src/libstd/ffi/c_str.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ impl CString {
209209
issue = "27769")]
210210
#[deprecated(since = "1.4.0", reason = "renamed to from_raw")]
211211
pub unsafe fn from_ptr(ptr: *const libc::c_char) -> CString {
212-
CString::from_raw(ptr)
212+
CString::from_raw(ptr as *mut _)
213213
}
214214

215215
/// Retakes ownership of a CString that was transferred to C.
@@ -219,7 +219,7 @@ impl CString {
219219
/// using the pointer.
220220
#[unstable(feature = "cstr_memory", reason = "recently added",
221221
issue = "27769")]
222-
pub unsafe fn from_raw(ptr: *const libc::c_char) -> CString {
222+
pub unsafe fn from_raw(ptr: *mut libc::c_char) -> CString {
223223
let len = libc::strlen(ptr) + 1; // Including the NUL byte
224224
let slice = slice::from_raw_parts(ptr, len as usize);
225225
CString { inner: mem::transmute(slice) }
@@ -237,7 +237,7 @@ impl CString {
237237
issue = "27769")]
238238
#[deprecated(since = "1.4.0", reason = "renamed to into_raw")]
239239
pub fn into_ptr(self) -> *const libc::c_char {
240-
self.into_raw()
240+
self.into_raw() as *const _
241241
}
242242

243243
/// Transfers ownership of the string to a C caller.
@@ -250,11 +250,8 @@ impl CString {
250250
/// Failure to call `from_ptr` will lead to a memory leak.
251251
#[unstable(feature = "cstr_memory", reason = "recently added",
252252
issue = "27769")]
253-
pub fn into_raw(self) -> *const libc::c_char {
254-
// It is important that the bytes be sized to fit - we need
255-
// the capacity to be determinable from the string length, and
256-
// shrinking to fit is the only way to be sure.
257-
Box::into_raw(self.inner) as *const libc::c_char
253+
pub fn into_raw(self) -> *mut libc::c_char {
254+
Box::into_raw(self.inner) as *mut libc::c_char
258255
}
259256

260257
/// Returns the contents of this `CString` as a slice of bytes.

0 commit comments

Comments
 (0)