File tree Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change @@ -200,6 +200,28 @@ impl CString {
200
200
CString { inner : v }
201
201
}
202
202
203
+ /// Retakes ownership from a Rust string that has been returned to C.
204
+ pub unsafe fn from_raw_ptr ( ptr : * const libc:: c_char ) -> CString {
205
+ let len = libc:: strlen ( ptr) ;
206
+ let len_with_nul = len as usize + 1 ;
207
+ let vec = Vec :: from_raw_parts ( ptr as * mut u8 , len_with_nul, len_with_nul) ;
208
+ CString :: from_vec_unchecked ( vec)
209
+ }
210
+
211
+ /// Transfers ownership from Rust to C. Be warned that the string
212
+ /// must be returned to Rust and reconstituted via `from_raw_ptr`
213
+ /// to be properly deallocated.
214
+ pub fn into_raw_ptr ( self ) -> * const libc:: c_char {
215
+ // Resize the vector to fit - we need the capacity to be
216
+ // determinable from the string length, and shrinking to fit
217
+ // is the only way to be sure.
218
+ let mut vec = self . inner ;
219
+ vec. shrink_to_fit ( ) ;
220
+ let ptr = vec. as_ptr ( ) as * const libc:: c_char ;
221
+ mem:: forget ( vec) ;
222
+ ptr
223
+ }
224
+
203
225
/// Returns the contents of this `CString` as a slice of bytes.
204
226
///
205
227
/// The returned slice does **not** contain the trailing nul separator and
You can’t perform that action at this time.
0 commit comments