Skip to content

Commit 32d51bd

Browse files
committed
---
yaml --- r: 212602 b: refs/heads/auto c: e20a6db h: refs/heads/master v: v3
1 parent e9f251e commit 32d51bd

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503
1010
refs/tags/release-0.3.1: 495bae036dfe5ec6ceafd3312b4dca48741e845b
1111
refs/tags/release-0.4: e828ea2080499553b97dfe33b3f4d472b4562ad7
1212
refs/tags/release-0.5: 7e3bcfbf21278251ee936ad53e92e9b719702d73
13-
refs/heads/auto: 97294be30c8712a91060d0ce043adefb4f867db8
13+
refs/heads/auto: e20a6dbeed095427e5d5487844f65e7eb1599651
1414
refs/heads/servo: af82457af293e2a842ba6b7759b70288da276167
1515
refs/tags/release-0.6: b4ebcfa1812664df5e142f0134a5faea3918544c
1616
refs/tags/0.1: b19db808c2793fe2976759b85a355c3ad8c8b336

branches/auto/src/libstd/ffi/c_str.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#![unstable(feature = "std_misc")]
1212

1313
use borrow::{Cow, ToOwned};
14-
use boxed::Box;
14+
use boxed::{self, Box};
1515
use clone::Clone;
1616
use convert::{Into, From};
1717
use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering};
@@ -202,6 +202,34 @@ impl CString {
202202
CString { inner: v.into_boxed_slice() }
203203
}
204204

205+
/// Retakes ownership of a CString that was transferred to C.
206+
///
207+
/// The only appropriate argument is a pointer obtained by calling
208+
/// `into_ptr`. The length of the string will be recalculated
209+
/// using the pointer.
210+
#[unstable(feature = "cstr_memory", reason = "recently added")]
211+
pub unsafe fn from_ptr(ptr: *const libc::c_char) -> CString {
212+
let len = libc::strlen(ptr) + 1; // Including the NUL byte
213+
let slice = slice::from_raw_parts(ptr, len as usize);
214+
CString { inner: mem::transmute(slice) }
215+
}
216+
217+
/// Transfers ownership of the string to a C caller.
218+
///
219+
/// The pointer must be returned to Rust and reconstituted using
220+
/// `from_ptr` to be properly deallocated. Specifically, one
221+
/// should *not* use the standard C `free` function to deallocate
222+
/// this string.
223+
///
224+
/// Failure to call `from_ptr` will lead to a memory leak.
225+
#[unstable(feature = "cstr_memory", reason = "recently added")]
226+
pub fn into_ptr(self) -> *const libc::c_char {
227+
// It is important that the bytes be sized to fit - we need
228+
// the capacity to be determinable from the string length, and
229+
// shrinking to fit is the only way to be sure.
230+
boxed::into_raw(self.inner) as *const libc::c_char
231+
}
232+
205233
/// Returns the contents of this `CString` as a slice of bytes.
206234
///
207235
/// The returned slice does **not** contain the trailing nul separator and

0 commit comments

Comments
 (0)