Skip to content

Commit

Permalink
Add methods for handing CStrings back and forth to C
Browse files Browse the repository at this point in the history
  • Loading branch information
shepmaster committed Jun 3, 2015
1 parent 2ebc3e3 commit a425a42
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/libstd/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#![unstable(feature = "std_misc")]

use borrow::{Cow, ToOwned};
use boxed::Box;
use boxed::{self, Box};
use clone::Clone;
use convert::{Into, From};
use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering};
Expand Down Expand Up @@ -202,6 +202,34 @@ impl CString {
CString { inner: v.into_boxed_slice() }
}

/// Retakes ownership of a CString that was transferred to C.
///
/// The only appropriate argument is a pointer obtained by calling
/// `into_ptr`.
#[unstable(feature = "c_str_memory", reason = "recently added")]
pub unsafe fn from_ptr(ptr: *const libc::c_char) -> CString {
let len = libc::strlen(ptr);
let len_with_nul = len as usize + 1;
let vec = Vec::from_raw_parts(ptr as *mut u8, len_with_nul, len_with_nul);
CString { inner: vec.into_boxed_slice() }
}

/// 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.
///
/// Failure to call `from_ptr` will lead to a memory leak.
#[unstable(feature = "c_str_memory", reason = "recently added")]
pub fn into_ptr(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.
boxed::into_raw(self.inner) as *const libc::c_char
}

/// Returns the contents of this `CString` as a slice of bytes.
///
/// The returned slice does **not** contain the trailing nul separator and
Expand Down

0 comments on commit a425a42

Please sign in to comment.