use alloc::libc_heap::malloc_raw;
use collections::string::String;
+use collections::hash;
use core::kinds::marker;
use core::mem;
use core::ptr;
}
}
+impl PartialOrd for CString {
+ #[inline]
+ fn partial_cmp(&self, other: &CString) -> Option<Ordering> {
+ self.as_bytes().partial_cmp(&other.as_bytes())
+ }
+}
+
+impl Eq for CString {}
+
+impl<S: hash::Writer> hash::Hash<S> for CString {
+ #[inline]
+ fn hash(&self, state: &mut S) {
+ self.as_bytes().hash(state)
+ }
+}
+
impl CString {
/// Create a C String from a pointer.
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
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 {
// (without forcing an additional & around &str). So we are instead
// temporarily adding an instance for ~str and String, so that we can
// take ToCStr as owned. When DST lands, the string instances should
-// be revisted, and arguments bound by ToCStr should be passed by
+// be revisited, and arguments bound by ToCStr should be passed by
// reference.
impl<'a> ToCStr for &'a str {
Mary had a little lamb, Little lamb
Mary had a little lamb, Little lamb";
- fn bench_to_str(b: &mut Bencher, s: &str) {
+ fn bench_to_string(b: &mut Bencher, s: &str) {
b.iter(|| {
let c_str = s.to_c_str();
check(s, c_str.as_ptr());
#[bench]
fn bench_to_c_str_short(b: &mut Bencher) {
- bench_to_str(b, s_short)
+ bench_to_string(b, s_short)
}
#[bench]
fn bench_to_c_str_medium(b: &mut Bencher) {
- bench_to_str(b, s_medium)
+ bench_to_string(b, s_medium)
}
#[bench]
fn bench_to_c_str_long(b: &mut Bencher) {
- bench_to_str(b, s_long)
+ bench_to_string(b, s_long)
}
fn bench_to_c_str_unchecked(b: &mut Bencher, s: &str) {