]> git.lizzy.rs Git - rust.git/blobdiff - src/librustrt/c_str.rs
auto merge of #15421 : catharsis/rust/doc-ffi-minor-fixes, r=alexcrichton
[rust.git] / src / librustrt / c_str.rs
index 6118fb2f999671221db6d2b389e5fe97a55b64f1..396d51f4fcb13a4128d62046199608646d72c385 100644 (file)
@@ -69,6 +69,7 @@ fn main() {
 
 use alloc::libc_heap::malloc_raw;
 use collections::string::String;
+use collections::hash;
 use core::kinds::marker;
 use core::mem;
 use core::ptr;
@@ -116,23 +117,28 @@ fn eq(&self, other: &CString) -> bool {
     }
 }
 
+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
@@ -289,6 +295,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 {
@@ -371,7 +393,7 @@ unsafe fn with_c_str_unchecked<T>(&self, f: |*const libc::c_char| -> T) -> T {
 // (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 {
@@ -799,7 +821,7 @@ fn check(s: &str, c_str: *const libc::c_char) {
         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());
@@ -808,17 +830,17 @@ fn bench_to_str(b: &mut Bencher, s: &str) {
 
     #[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) {