Skip to content

Commit

Permalink
Auto merge of rust-lang#30616 - arcnmx:cstr-asref, r=aturon
Browse files Browse the repository at this point in the history
Are trait impls still insta-stable? Considering that this design has been around for a long time on `String` and `OsString` it probably doesn't matter much...

The `From` impl is a bit strange to me. It's stolen from `OsString` but I'm not really sure about it... `String` just impls `From<&str>` instead, would that make more sense?
  • Loading branch information
bors committed Dec 31, 2015
2 parents 7d95433 + 53878e7 commit b9075d6
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/libstd/ffi/c_str.rs
Original file line number Diff line number Diff line change
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 {
}
}

#[stable(feature = "cstring_asref", since = "1.7.0")]
impl<'a> From<&'a CStr> for CString {
fn from(s: &'a CStr) -> CString {
s.to_owned()
}
}

#[stable(feature = "cstring_asref", since = "1.7.0")]
impl ops::Index<ops::RangeFull> for CString {
type Output = CStr;

#[inline]
fn index(&self, _index: ops::RangeFull) -> &CStr {
self
}
}

#[stable(feature = "cstring_asref", since = "1.7.0")]
impl AsRef<CStr> for CStr {
fn as_ref(&self) -> &CStr {
self
}
}

#[stable(feature = "cstring_asref", since = "1.7.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 b9075d6

Please sign in to comment.