From 9a7b789c3717146ca3ce3fca1eb23fa3a239f0fb Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Sun, 12 Mar 2017 15:04:32 -0400 Subject: Add doc example for `OsStr::to_os_string`. --- src/libstd/ffi/os_str.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'src/libstd/ffi') diff --git a/src/libstd/ffi/os_str.rs b/src/libstd/ffi/os_str.rs index 41bdd9c51d4..3914f398961 100644 --- a/src/libstd/ffi/os_str.rs +++ b/src/libstd/ffi/os_str.rs @@ -398,6 +398,16 @@ impl OsStr { /// Copies the slice into an owned [`OsString`]. /// /// [`OsString`]: struct.OsString.html + /// + /// # Examples + /// + /// ``` + /// use std::ffi::{OsStr, OsString}; + /// + /// let os_str = OsStr::new("foo"); + /// let os_string = os_str.to_os_string(); + /// assert_eq!(os_string, OsString::from("foo")); + /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn to_os_string(&self) -> OsString { OsString { inner: self.inner.to_owned() } -- cgit 1.4.1-3-g733a5 From 4d57d92f071d24a4c189cc8aef897be25bcd9d55 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Sun, 12 Mar 2017 16:21:34 -0400 Subject: Add doc example for `OsString::reserve`. --- src/libstd/ffi/os_str.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'src/libstd/ffi') diff --git a/src/libstd/ffi/os_str.rs b/src/libstd/ffi/os_str.rs index 3914f398961..bdd8ce5e6d8 100644 --- a/src/libstd/ffi/os_str.rs +++ b/src/libstd/ffi/os_str.rs @@ -188,6 +188,16 @@ impl OsString { /// in the given `OsString`. /// /// The collection may reserve more space to avoid frequent reallocations. + /// + /// # Examples + /// + /// ``` + /// use std::ffi::OsString; + /// + /// let mut s = OsString::new(); + /// s.reserve(10); + /// assert!(s.capacity() >= 10); + /// ``` #[stable(feature = "osstring_simple_functions", since = "1.9.0")] pub fn reserve(&mut self, additional: usize) { self.inner.reserve(additional) -- cgit 1.4.1-3-g733a5 From 5537955b1747f8f8c873cf7681ec8a497404e730 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Sun, 12 Mar 2017 16:21:58 -0400 Subject: Add doc example for `OsString::reserve_exact`. --- src/libstd/ffi/os_str.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'src/libstd/ffi') diff --git a/src/libstd/ffi/os_str.rs b/src/libstd/ffi/os_str.rs index bdd8ce5e6d8..d960d76146f 100644 --- a/src/libstd/ffi/os_str.rs +++ b/src/libstd/ffi/os_str.rs @@ -210,6 +210,16 @@ impl OsString { /// Note that the allocator may give the collection more space than it /// requests. Therefore capacity can not be relied upon to be precisely /// minimal. Prefer reserve if future insertions are expected. + /// + /// # Examples + /// + /// ``` + /// use std::ffi::OsString; + /// + /// let mut s = OsString::new(); + /// s.reserve_exact(10); + /// assert!(s.capacity() >= 10); + /// ``` #[stable(feature = "osstring_simple_functions", since = "1.9.0")] pub fn reserve_exact(&mut self, additional: usize) { self.inner.reserve_exact(additional) -- cgit 1.4.1-3-g733a5 From bda57dbc059a15222173b40a5e4d7e5579adcfec Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Sun, 12 Mar 2017 16:22:12 -0400 Subject: Add doc example for `OsString::shrink_to_fit`. --- src/libstd/ffi/os_str.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'src/libstd/ffi') diff --git a/src/libstd/ffi/os_str.rs b/src/libstd/ffi/os_str.rs index d960d76146f..9da6eca3408 100644 --- a/src/libstd/ffi/os_str.rs +++ b/src/libstd/ffi/os_str.rs @@ -226,6 +226,22 @@ impl OsString { } /// Shrinks the capacity of the `OsString` to match its length. + /// + /// # Examples + /// + /// ``` + /// #![feature(osstring_shrink_to_fit)] + /// + /// use std::ffi::OsString; + /// + /// let mut s = OsString::from("foo"); + /// + /// s.reserve(100); + /// assert!(s.capacity() >= 100); + /// + /// s.shrink_to_fit(); + /// assert_eq!(3, s.capacity()); + /// ``` #[unstable(feature = "osstring_shrink_to_fit", issue = "40421")] pub fn shrink_to_fit(&mut self) { self.inner.shrink_to_fit() -- cgit 1.4.1-3-g733a5 From 6adbbfc6ba8786ea91e1051ea14d64a91839f5b5 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Sun, 12 Mar 2017 16:22:29 -0400 Subject: Add doc example for `OsString::into_boxed_os_str`. --- src/libstd/ffi/os_str.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src/libstd/ffi') diff --git a/src/libstd/ffi/os_str.rs b/src/libstd/ffi/os_str.rs index 9da6eca3408..b0f79f9a395 100644 --- a/src/libstd/ffi/os_str.rs +++ b/src/libstd/ffi/os_str.rs @@ -248,6 +248,18 @@ impl OsString { } /// Converts this `OsString` into a boxed `OsStr`. + /// + /// # Examples + /// + /// ``` + /// #![feature(into_boxed_os_str)] + /// + /// use std::ffi::{OsString, OsStr}; + /// + /// let s = OsString::from("hello"); + /// + /// let b: Box = s.into_boxed_os_str(); + /// ``` #[unstable(feature = "into_boxed_os_str", issue = "0")] pub fn into_boxed_os_str(self) -> Box { unsafe { mem::transmute(self.inner.into_box()) } -- cgit 1.4.1-3-g733a5 From 9ffb54568c1d52bfee0162dd75b2c415cbf6fce4 Mon Sep 17 00:00:00 2001 From: Clar Charr Date: Thu, 6 Apr 2017 14:43:37 -0400 Subject: Remove some CStr transmutes. --- src/libstd/ffi/c_str.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/libstd/ffi') diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs index 2d14bb66bf4..fc1b9a97632 100644 --- a/src/libstd/ffi/c_str.rs +++ b/src/libstd/ffi/c_str.rs @@ -356,7 +356,7 @@ impl ops::Deref for CString { type Target = CStr; fn deref(&self) -> &CStr { - unsafe { mem::transmute(self.as_bytes_with_nul()) } + unsafe { CStr::from_bytes_with_nul_unchecked(self.as_bytes_with_nul()) } } } @@ -583,7 +583,8 @@ impl CStr { #[stable(feature = "rust1", since = "1.0.0")] pub unsafe fn from_ptr<'a>(ptr: *const c_char) -> &'a CStr { let len = libc::strlen(ptr); - mem::transmute(slice::from_raw_parts(ptr, len as usize + 1)) + let ptr = ptr as *const u8; + CStr::from_bytes_with_nul_unchecked(slice::from_raw_parts(ptr, len as usize + 1)) } /// Creates a C string wrapper from a byte slice. -- cgit 1.4.1-3-g733a5 From 68909b0ec0d8738a8f1a0bb7a80998a246943471 Mon Sep 17 00:00:00 2001 From: Clar Charr Date: Wed, 5 Apr 2017 17:13:46 -0400 Subject: Add as_c_str. --- src/doc/unstable-book/src/SUMMARY.md | 1 + src/doc/unstable-book/src/as-c-str.md | 8 ++++++++ src/libstd/ffi/c_str.rs | 6 ++++++ 3 files changed, 15 insertions(+) create mode 100644 src/doc/unstable-book/src/as-c-str.md (limited to 'src/libstd/ffi') diff --git a/src/doc/unstable-book/src/SUMMARY.md b/src/doc/unstable-book/src/SUMMARY.md index 9ce097e78a4..1f2ef3669b8 100644 --- a/src/doc/unstable-book/src/SUMMARY.md +++ b/src/doc/unstable-book/src/SUMMARY.md @@ -12,6 +12,7 @@ - [alloc_system](alloc-system.md) - [allocator](allocator.md) - [allow_internal_unstable](allow-internal-unstable.md) +- [as_c_str](as-c-str.md) - [as_unsafe_cell](as-unsafe-cell.md) - [ascii_ctype](ascii-ctype.md) - [asm](asm.md) diff --git a/src/doc/unstable-book/src/as-c-str.md b/src/doc/unstable-book/src/as-c-str.md new file mode 100644 index 00000000000..ed32eedb348 --- /dev/null +++ b/src/doc/unstable-book/src/as-c-str.md @@ -0,0 +1,8 @@ +# `as_c_str` + +The tracking issue for this feature is: [#40380] + +[#40380]: https://github.com/rust-lang/rust/issues/40380 + +------------------------ + diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs index 2d14bb66bf4..d157d52259d 100644 --- a/src/libstd/ffi/c_str.rs +++ b/src/libstd/ffi/c_str.rs @@ -324,6 +324,12 @@ impl CString { &self.inner } + /// Extracts a `CStr` slice containing the entire string. + #[unstable(feature = "as_c_str", issue = "40380")] + pub fn as_c_str(&self) -> &CStr { + &*self + } + /// Converts this `CString` into a boxed `CStr`. #[unstable(feature = "into_boxed_c_str", issue = "40380")] pub fn into_boxed_c_str(self) -> Box { -- cgit 1.4.1-3-g733a5