about summary refs log tree commit diff
diff options
context:
space:
mode:
authorarcnmx <arcnmx@users.noreply.github.com>2015-12-29 13:02:08 -0500
committerarcnmx <arcnmx@users.noreply.github.com>2015-12-29 13:02:08 -0500
commit43ab6c7d5a9a9951de487baf49b91fd4b85e584d (patch)
tree3923eaba55888e30a2ab5aa4326c1207f5447669
parent27a1834ce522e3ec7fe4726b1661de16ee30c503 (diff)
downloadrust-43ab6c7d5a9a9951de487baf49b91fd4b85e584d.tar.gz
rust-43ab6c7d5a9a9951de487baf49b91fd4b85e584d.zip
AsRef and related conversions for CString
-rw-r--r--src/libstd/ffi/c_str.rs35
1 files changed, 33 insertions, 2 deletions
diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs
index 3f3913471b8..d2a885dc7fe 100644
--- a/src/libstd/ffi/c_str.rs
+++ b/src/libstd/ffi/c_str.rs
@@ -20,7 +20,7 @@ use iter::Iterator;
 use libc;
 use mem;
 use memchr;
-use ops::Deref;
+use ops;
 use option::Option::{self, Some, None};
 use os::raw::c_char;
 use result::Result::{self, Ok, Err};
@@ -282,7 +282,7 @@ impl CString {
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]
-impl Deref for CString {
+impl ops::Deref for CString {
     type Target = CStr;
 
     fn deref(&self) -> &CStr {
@@ -522,6 +522,37 @@ impl ToOwned for CStr {
     }
 }
 
+#[unstable(feature = "cstring_asref", reason = "recently added", issue = "0")]
+impl ops::Index<ops::RangeFull> for CString {
+    type Output = CStr;
+
+    #[inline]
+    fn index(&self, _index: ops::RangeFull) -> &CStr {
+        self
+    }
+}
+
+#[unstable(feature = "cstring_asref", reason = "recently added", issue = "0")]
+impl<'a, T: ?Sized + AsRef<CStr>> From<&'a T> for CString {
+    fn from(s: &'a T) -> CString {
+        s.as_ref().to_owned()
+    }
+}
+
+#[unstable(feature = "cstring_asref", reason = "recently added", issue = "0")]
+impl AsRef<CStr> for CStr {
+    fn as_ref(&self) -> &CStr {
+        self
+    }
+}
+
+#[unstable(feature = "cstring_asref", reason = "recently added", issue = "0")]
+impl AsRef<CStr> for CString {
+    fn as_ref(&self) -> &CStr {
+        self
+    }
+}
+
 #[cfg(test)]
 mod tests {
     use prelude::v1::*;