Auto merge of #30616 - arcnmx:cstr-asref, r=aturon
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?
This commit is contained in:
commit
b9075d6f53
1 changed files with 33 additions and 2 deletions
|
@ -20,7 +20,7 @@ use iter::Iterator;
|
||||||
use libc;
|
use libc;
|
||||||
use mem;
|
use mem;
|
||||||
use memchr;
|
use memchr;
|
||||||
use ops::Deref;
|
use ops;
|
||||||
use option::Option::{self, Some, None};
|
use option::Option::{self, Some, None};
|
||||||
use os::raw::c_char;
|
use os::raw::c_char;
|
||||||
use result::Result::{self, Ok, Err};
|
use result::Result::{self, Ok, Err};
|
||||||
|
@ -282,7 +282,7 @@ impl CString {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
impl Deref for CString {
|
impl ops::Deref for CString {
|
||||||
type Target = CStr;
|
type Target = CStr;
|
||||||
|
|
||||||
fn deref(&self) -> &CStr {
|
fn deref(&self) -> &CStr {
|
||||||
|
@ -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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use prelude::v1::*;
|
use prelude::v1::*;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue