Skip to content

Commit 43ab6c7

Browse files
committed
AsRef and related conversions for CString
1 parent 27a1834 commit 43ab6c7

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

src/libstd/ffi/c_str.rs

Lines changed: 33 additions & 2 deletions
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+
#[unstable(feature = "cstring_asref", reason = "recently added", issue = "0")]
526+
impl ops::Index<ops::RangeFull> for CString {
527+
type Output = CStr;
528+
529+
#[inline]
530+
fn index(&self, _index: ops::RangeFull) -> &CStr {
531+
self
532+
}
533+
}
534+
535+
#[unstable(feature = "cstring_asref", reason = "recently added", issue = "0")]
536+
impl<'a, T: ?Sized + AsRef<CStr>> From<&'a T> for CString {
537+
fn from(s: &'a T) -> CString {
538+
s.as_ref().to_owned()
539+
}
540+
}
541+
542+
#[unstable(feature = "cstring_asref", reason = "recently added", issue = "0")]
543+
impl AsRef<CStr> for CStr {
544+
fn as_ref(&self) -> &CStr {
545+
self
546+
}
547+
}
548+
549+
#[unstable(feature = "cstring_asref", reason = "recently added", issue = "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)