Skip to content

Commit b9075d6

Browse files
committed
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?
2 parents 7d95433 + 53878e7 commit b9075d6

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

src/libstd/ffi/c_str.rs

+33-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use iter::Iterator;
2020
use libc;
2121
use mem;
2222
use memchr;
23-
use ops::Deref;
23+
use ops;
2424
use option::Option::{self, Some, None};
2525
use os::raw::c_char;
2626
use result::Result::{self, Ok, Err};
@@ -282,7 +282,7 @@ impl CString {
282282
}
283283

284284
#[stable(feature = "rust1", since = "1.0.0")]
285-
impl Deref for CString {
285+
impl ops::Deref for CString {
286286
type Target = CStr;
287287

288288
fn deref(&self) -> &CStr {
@@ -522,6 +522,37 @@ impl ToOwned for CStr {
522522
}
523523
}
524524

525+
#[stable(feature = "cstring_asref", since = "1.7.0")]
526+
impl<'a> From<&'a CStr> for CString {
527+
fn from(s: &'a CStr) -> CString {
528+
s.to_owned()
529+
}
530+
}
531+
532+
#[stable(feature = "cstring_asref", since = "1.7.0")]
533+
impl ops::Index<ops::RangeFull> for CString {
534+
type Output = CStr;
535+
536+
#[inline]
537+
fn index(&self, _index: ops::RangeFull) -> &CStr {
538+
self
539+
}
540+
}
541+
542+
#[stable(feature = "cstring_asref", since = "1.7.0")]
543+
impl AsRef<CStr> for CStr {
544+
fn as_ref(&self) -> &CStr {
545+
self
546+
}
547+
}
548+
549+
#[stable(feature = "cstring_asref", since = "1.7.0")]
550+
impl AsRef<CStr> for CString {
551+
fn as_ref(&self) -> &CStr {
552+
self
553+
}
554+
}
555+
525556
#[cfg(test)]
526557
mod tests {
527558
use prelude::v1::*;

0 commit comments

Comments
 (0)