Skip to content

Commit

Permalink
AsRef and related conversions for CString
Browse files Browse the repository at this point in the history
  • Loading branch information
arcnmx committed Dec 29, 2015
1 parent 27a1834 commit 43ab6c7
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/libstd/ffi/c_str.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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::*;
Expand Down

0 comments on commit 43ab6c7

Please sign in to comment.