From 43ab6c7d5a9a9951de487baf49b91fd4b85e584d Mon Sep 17 00:00:00 2001 From: arcnmx Date: Tue, 29 Dec 2015 13:02:08 -0500 Subject: [PATCH 1/3] AsRef and related conversions for CString --- src/libstd/ffi/c_str.rs | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs index 3f3913471b8..d2a885dc7fe 100644 --- a/src/libstd/ffi/c_str.rs +++ b/src/libstd/ffi/c_str.rs @@ -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}; @@ -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 { @@ -522,6 +522,37 @@ impl ToOwned for CStr { } } +#[unstable(feature = "cstring_asref", reason = "recently added", issue = "0")] +impl ops::Index 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> 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 for CStr { + fn as_ref(&self) -> &CStr { + self + } +} + +#[unstable(feature = "cstring_asref", reason = "recently added", issue = "0")] +impl AsRef for CString { + fn as_ref(&self) -> &CStr { + self + } +} + #[cfg(test)] mod tests { use prelude::v1::*; From 965556d1626a1ca26db08b1d4193ae9cc026223a Mon Sep 17 00:00:00 2001 From: arcnmx Date: Thu, 31 Dec 2015 13:56:21 -0500 Subject: [PATCH 2/3] impl From<&CStr> for CString --- src/libstd/ffi/c_str.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs index d2a885dc7fe..c8d43bfcef0 100644 --- a/src/libstd/ffi/c_str.rs +++ b/src/libstd/ffi/c_str.rs @@ -522,6 +522,13 @@ impl ToOwned for CStr { } } +#[unstable(feature = "cstring_asref", reason = "recently added", issue = "0")] +impl<'a> From<&'a CStr> for CString { + fn from(s: &'a CStr) -> CString { + s.to_owned() + } +} + #[unstable(feature = "cstring_asref", reason = "recently added", issue = "0")] impl ops::Index for CString { type Output = CStr; @@ -532,13 +539,6 @@ impl ops::Index for CString { } } -#[unstable(feature = "cstring_asref", reason = "recently added", issue = "0")] -impl<'a, T: ?Sized + AsRef> 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 for CStr { fn as_ref(&self) -> &CStr { From 53878e7546e3e6f3665dea572fbfa48f005ecad2 Mon Sep 17 00:00:00 2001 From: arcnmx Date: Thu, 31 Dec 2015 14:21:40 -0500 Subject: [PATCH 3/3] CStr impl stability --- src/libstd/ffi/c_str.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs index c8d43bfcef0..9a41272299e 100644 --- a/src/libstd/ffi/c_str.rs +++ b/src/libstd/ffi/c_str.rs @@ -522,14 +522,14 @@ impl ToOwned for CStr { } } -#[unstable(feature = "cstring_asref", reason = "recently added", issue = "0")] +#[stable(feature = "cstring_asref", since = "1.7.0")] impl<'a> From<&'a CStr> for CString { fn from(s: &'a CStr) -> CString { s.to_owned() } } -#[unstable(feature = "cstring_asref", reason = "recently added", issue = "0")] +#[stable(feature = "cstring_asref", since = "1.7.0")] impl ops::Index for CString { type Output = CStr; @@ -539,14 +539,14 @@ impl ops::Index for CString { } } -#[unstable(feature = "cstring_asref", reason = "recently added", issue = "0")] +#[stable(feature = "cstring_asref", since = "1.7.0")] impl AsRef for CStr { fn as_ref(&self) -> &CStr { self } } -#[unstable(feature = "cstring_asref", reason = "recently added", issue = "0")] +#[stable(feature = "cstring_asref", since = "1.7.0")] impl AsRef for CString { fn as_ref(&self) -> &CStr { self