From 4db8c9f0baef0a5851f6c777d8ce31274bf8c10b Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Mon, 5 Jun 2017 23:42:13 -0400 Subject: [PATCH 1/5] Add doc example for `CString::into_raw`. --- src/libstd/ffi/c_str.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs index 1167c39dba8ee..a1ce02b583057 100644 --- a/src/libstd/ffi/c_str.rs +++ b/src/libstd/ffi/c_str.rs @@ -288,6 +288,26 @@ impl CString { /// Failure to call [`from_raw`] will lead to a memory leak. /// /// [`from_raw`]: #method.from_raw + /// + /// # Examples + /// + /// ``` + /// use std::ffi::CString; + /// + /// let c_string = CString::new("foo").unwrap(); + /// + /// let ptr = c_string.into_raw(); + /// + /// unsafe { + /// assert_eq!(b'f', *ptr as u8); + /// assert_eq!(b'o', *ptr.offset(1) as u8); + /// assert_eq!(b'o', *ptr.offset(2) as u8); + /// assert_eq!(b'\0', *ptr.offset(3) as u8); + /// + /// // retake pointer to free memory + /// let _ = CString::from_raw(ptr); + /// } + /// ``` #[stable(feature = "cstr_memory", since = "1.4.0")] pub fn into_raw(self) -> *mut c_char { Box::into_raw(self.into_inner()) as *mut c_char From 3ec1f61accd0386d6923102bd2da30fef4603c47 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Mon, 5 Jun 2017 23:46:44 -0400 Subject: [PATCH 2/5] Add doc example for `CString::into_bytes`. --- src/libstd/ffi/c_str.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs index a1ce02b583057..0e8d72c5f3be2 100644 --- a/src/libstd/ffi/c_str.rs +++ b/src/libstd/ffi/c_str.rs @@ -331,6 +331,16 @@ impl CString { /// /// The returned buffer does **not** contain the trailing nul separator and /// it is guaranteed to not have any interior nul bytes. + /// + /// # Examples + /// + /// ``` + /// use std::ffi::CString; + /// + /// let c_string = CString::new("foo").unwrap(); + /// let bytes = c_string.into_bytes(); + /// assert_eq!(bytes, vec![b'f', b'o', b'o']); + /// ``` #[stable(feature = "cstring_into", since = "1.7.0")] pub fn into_bytes(self) -> Vec { let mut vec = self.into_inner().into_vec(); From 815c12a7657cfc68c7058f83d9c30ff7607c17b2 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Mon, 5 Jun 2017 23:48:13 -0400 Subject: [PATCH 3/5] Add doc example for `CString::into_bytes_with_nul`. --- src/libstd/ffi/c_str.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs index 0e8d72c5f3be2..81563a5906448 100644 --- a/src/libstd/ffi/c_str.rs +++ b/src/libstd/ffi/c_str.rs @@ -353,6 +353,16 @@ impl CString { /// includes the trailing nul byte. /// /// [`into_bytes`]: #method.into_bytes + /// + /// # Examples + /// + /// ``` + /// use std::ffi::CString; + /// + /// let c_string = CString::new("foo").unwrap(); + /// let bytes = c_string.into_bytes_with_nul(); + /// assert_eq!(bytes, vec![b'f', b'o', b'o', b'\0']); + /// ``` #[stable(feature = "cstring_into", since = "1.7.0")] pub fn into_bytes_with_nul(self) -> Vec { self.into_inner().into_vec() From ae4832d48acc563a55e23190e03f75e9e3336293 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Mon, 5 Jun 2017 23:49:52 -0400 Subject: [PATCH 4/5] Add doc example for `CString::as_bytes_with_nul`. --- src/libstd/ffi/c_str.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs index 81563a5906448..21e4779fc3a2b 100644 --- a/src/libstd/ffi/c_str.rs +++ b/src/libstd/ffi/c_str.rs @@ -381,6 +381,16 @@ impl CString { /// includes the trailing nul byte. /// /// [`as_bytes`]: #method.as_bytes + /// + /// # Examples + /// + /// ``` + /// use std::ffi::CString; + /// + /// let c_string = CString::new("foo").unwrap(); + /// let bytes = c_string.as_bytes_with_nul(); + /// assert_eq!(bytes, &[b'f', b'o', b'o', b'\0']); + /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn as_bytes_with_nul(&self) -> &[u8] { &self.inner From 06f63f5edb887aaff70a475de904103119d022e3 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Mon, 5 Jun 2017 23:50:49 -0400 Subject: [PATCH 5/5] Add doc example for `CString::as_bytes`. --- src/libstd/ffi/c_str.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs index 21e4779fc3a2b..2d78f0511d6d7 100644 --- a/src/libstd/ffi/c_str.rs +++ b/src/libstd/ffi/c_str.rs @@ -372,6 +372,16 @@ impl CString { /// /// The returned slice does **not** contain the trailing nul separator and /// it is guaranteed to not have any interior nul bytes. + /// + /// # Examples + /// + /// ``` + /// use std::ffi::CString; + /// + /// let c_string = CString::new("foo").unwrap(); + /// let bytes = c_string.as_bytes(); + /// assert_eq!(bytes, &[b'f', b'o', b'o']); + /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn as_bytes(&self) -> &[u8] { &self.inner[..self.inner.len() - 1]