c_str: move .unwrap & document it more clearly.

This should be called rarely, but it was placed first in the list of
methods, making it very tempting to call.
This commit is contained in:
Huon Wilson 2014-06-15 08:42:55 +10:00
parent d4d4bc4fe9
commit 569f13a521

View File

@ -122,17 +122,6 @@ pub unsafe fn new(buf: *const libc::c_char, owns_buffer: bool) -> CString {
CString { buf: buf, owns_buffer_: owns_buffer }
}
/// Unwraps the wrapped `*libc::c_char` from the `CString` wrapper.
///
/// The original object is destructed after this method is called, and if
/// the underlying pointer was previously allocated, care must be taken to
/// ensure that it is deallocated properly.
pub unsafe fn unwrap(self) -> *const libc::c_char {
let mut c_str = self;
c_str.owns_buffer_ = false;
c_str.buf
}
/// Return a pointer to the NUL-terminated string data.
///
/// `.as_ptr` returns an internal pointer into the `CString`, and
@ -289,6 +278,22 @@ pub fn iter<'a>(&'a self) -> CChars<'a> {
marker: marker::ContravariantLifetime,
}
}
/// Unwraps the wrapped `*libc::c_char` from the `CString` wrapper.
///
/// Any ownership of the buffer by the `CString` wrapper is
/// forgotten, meaning that the backing allocation of this
/// `CString` is not automatically freed if it owns the
/// allocation. In this case, a user of `.unwrap()` should ensure
/// the allocation is freed, to avoid leaking memory.
///
/// Prefer `.as_ptr()` when just retrieving a pointer to the
/// string data, as that does not relinquish ownership.
pub unsafe fn unwrap(mut self) -> *const libc::c_char {
self.owns_buffer_ = false;
self.buf
}
}
impl Drop for CString {