@@ -207,15 +207,39 @@ impl CString {
207
207
/// using the pointer.
208
208
#[ unstable( feature = "cstr_memory" , reason = "recently added" ,
209
209
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" ) ]
213
211
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 {
214
223
let len = libc:: strlen ( ptr) + 1 ; // Including the NUL byte
215
224
let slice = slice:: from_raw_parts ( ptr, len as usize ) ;
216
225
CString { inner : mem:: transmute ( slice) }
217
226
}
218
227
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
+
219
243
/// Transfers ownership of the string to a C caller.
220
244
///
221
245
/// The pointer must be returned to Rust and reconstituted using
@@ -226,8 +250,7 @@ impl CString {
226
250
/// Failure to call `from_ptr` will lead to a memory leak.
227
251
#[ unstable( feature = "cstr_memory" , reason = "recently added" ,
228
252
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 {
231
254
// It is important that the bytes be sized to fit - we need
232
255
// the capacity to be determinable from the string length, and
233
256
// shrinking to fit is the only way to be sure.
0 commit comments