diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs index 3f3913471b895..d2a885dc7fe26 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 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> 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 for CStr { + fn as_ref(&self) -> &CStr { + self + } +} + +#[unstable(feature = "cstring_asref", reason = "recently added", issue = "0")] +impl AsRef for CString { + fn as_ref(&self) -> &CStr { + self + } +} + #[cfg(test)] mod tests { use prelude::v1::*;