about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2015-08-18 21:47:06 +0000
committerbors <bors@rust-lang.org>2015-08-18 21:47:06 +0000
commit2fb3fb24a395dfb21142f4572440214fbf551e4a (patch)
tree00283597127a7beefcfed3d9d2d0bb3fa42ccd57 /src/libstd
parent1d72d314480d3437345129ddf8b299c36e048f18 (diff)
parent3cf9e1086ef49cacd020f30947d834ba9c5ec375 (diff)
downloadrust-2fb3fb24a395dfb21142f4572440214fbf551e4a.tar.gz
rust-2fb3fb24a395dfb21142f4572440214fbf551e4a.zip
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
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/ffi/c_str.rs33
1 files changed, 28 insertions, 5 deletions
diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs
index b973dcef651..23daa87401a 100644
--- a/src/libstd/ffi/c_str.rs
+++ b/src/libstd/ffi/c_str.rs
@@ -207,10 +207,19 @@ 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) }
@@ -219,6 +228,21 @@ impl CString {
     /// 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
     /// `from_ptr` to be properly deallocated. Specifically, one
     /// should *not* use the standard C `free` function to deallocate
     /// this string.
@@ -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.