@@ -128,17 +128,21 @@ pub struct CString {
128
128
129
129
/// Representation of a borrowed C string.
130
130
///
131
- /// This dynamically sized type is only safely constructed via a borrowed
132
- /// version of an instance of `CString`. This type can be constructed from a raw
133
- /// C string as well and represents a C string borrowed from another location.
131
+ /// This type represents a borrowed reference to a nul-terminated
132
+ /// array of bytes. It can be constructed safely from a `&[`[`u8`]`]`
133
+ /// slice, or unsafely from a raw `*const c_char`. It can then be
134
+ /// converted to a Rust [`&str`] by performing UTF-8 validation, or
135
+ /// into an owned [`CString`].
136
+ ///
137
+ /// `CStr` is to [`CString`] as [`&str`] is to [`String`]: the former
138
+ /// in each pair are borrowed references; the latter are owned
139
+ /// strings.
134
140
///
135
141
/// Note that this structure is **not** `repr(C)` and is not recommended to be
136
- /// placed in the signatures of FFI functions. Instead safe wrappers of FFI
142
+ /// placed in the signatures of FFI functions. Instead, safe wrappers of FFI
137
143
/// functions may leverage the unsafe [`from_ptr`] constructor to provide a safe
138
144
/// interface to other consumers.
139
145
///
140
- /// [`from_ptr`]: #method.from_ptr
141
- ///
142
146
/// # Examples
143
147
///
144
148
/// Inspecting a foreign C string:
@@ -151,7 +155,7 @@ pub struct CString {
151
155
///
152
156
/// unsafe {
153
157
/// let slice = CStr::from_ptr(my_string());
154
- /// println!("string length : {}", slice.to_bytes().len());
158
+ /// println!("string buffer size without nul terminator : {}", slice.to_bytes().len());
155
159
/// }
156
160
/// ```
157
161
///
@@ -173,8 +177,6 @@ pub struct CString {
173
177
///
174
178
/// Converting a foreign C string into a Rust [`String`]:
175
179
///
176
- /// [`String`]: ../string/struct.String.html
177
- ///
178
180
/// ```no_run
179
181
/// use std::ffi::CStr;
180
182
/// use std::os::raw::c_char;
@@ -189,6 +191,12 @@ pub struct CString {
189
191
///
190
192
/// println!("string: {}", my_string_safe());
191
193
/// ```
194
+ ///
195
+ /// [`u8`]: ../primitive.u8.html
196
+ /// [`&str`]: ../primitive.str.html
197
+ /// [`String`]: ../string/struct.String.html
198
+ /// [`CString`]: struct.CString.html
199
+ /// [`from_ptr`]: #method.from_ptr
192
200
#[ derive( Hash ) ]
193
201
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
194
202
pub struct CStr {
@@ -215,8 +223,10 @@ pub struct CStr {
215
223
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
216
224
pub struct NulError ( usize , Vec < u8 > ) ;
217
225
218
- /// An error returned from [`CStr::from_bytes_with_nul`] to indicate that a nul
219
- /// byte was found too early in the slice provided or one wasn't found at all.
226
+ /// An error returned from [`CStr::from_bytes_with_nul`] to indicate
227
+ /// that a nul byte was found too early in the slice provided, or one
228
+ /// wasn't found at all. The slice used to create a `CStr` must have one
229
+ /// and only one nul byte at the end of the slice.
220
230
///
221
231
/// [`CStr::from_bytes_with_nul`]: struct.CStr.html#method.from_bytes_with_nul
222
232
///
@@ -795,9 +805,9 @@ impl fmt::Display for IntoStringError {
795
805
}
796
806
797
807
impl CStr {
798
- /// Casts a raw C string to a safe C string wrapper.
808
+ /// Wraps a raw C string with a safe C string wrapper.
799
809
///
800
- /// This function will cast the provided `ptr` to the `CStr` wrapper which
810
+ /// This function will wrap the provided `ptr` with a `CStr` wrapper, which
801
811
/// allows inspection and interoperation of non-owned C strings. This method
802
812
/// is unsafe for a number of reasons:
803
813
///
@@ -837,9 +847,9 @@ impl CStr {
837
847
838
848
/// Creates a C string wrapper from a byte slice.
839
849
///
840
- /// This function will cast the provided `bytes` to a `CStr` wrapper after
841
- /// ensuring that it is null terminated and does not contain any interior
842
- /// nul bytes.
850
+ /// This function will cast the provided `bytes` to a `CStr`
851
+ /// wrapper after ensuring that the byte slice is nul-terminated
852
+ /// and does not contain any interior nul bytes.
843
853
///
844
854
/// # Examples
845
855
///
@@ -884,7 +894,7 @@ impl CStr {
884
894
/// Unsafely creates a C string wrapper from a byte slice.
885
895
///
886
896
/// This function will cast the provided `bytes` to a `CStr` wrapper without
887
- /// performing any sanity checks. The provided slice must be null terminated
897
+ /// performing any sanity checks. The provided slice ** must** be nul- terminated
888
898
/// and not contain any interior nul bytes.
889
899
///
890
900
/// # Examples
@@ -906,7 +916,7 @@ impl CStr {
906
916
907
917
/// Returns the inner pointer to this C string.
908
918
///
909
- /// The returned pointer will be valid for as long as `self` is and points
919
+ /// The returned pointer will be valid for as long as `self` is, and points
910
920
/// to a contiguous region of memory terminated with a 0 byte to represent
911
921
/// the end of the string.
912
922
///
@@ -927,9 +937,9 @@ impl CStr {
927
937
/// ```
928
938
///
929
939
/// This happens because the pointer returned by `as_ptr` does not carry any
930
- /// lifetime information and the string is deallocated immediately after
940
+ /// lifetime information and the [`CString`] is deallocated immediately after
931
941
/// the `CString::new("Hello").unwrap().as_ptr()` expression is evaluated.
932
- /// To fix the problem, bind the string to a local variable:
942
+ /// To fix the problem, bind the `CString` to a local variable:
933
943
///
934
944
/// ```no_run
935
945
/// use std::ffi::{CString};
@@ -941,6 +951,11 @@ impl CStr {
941
951
/// *ptr;
942
952
/// }
943
953
/// ```
954
+ ///
955
+ /// This way, the lifetime of the `CString` in `hello` encompasses
956
+ /// the lifetime of `ptr` and the `unsafe` block.
957
+ ///
958
+ /// [`CString`]: struct.CString.html
944
959
#[ inline]
945
960
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
946
961
pub fn as_ptr ( & self ) -> * const c_char {
@@ -949,10 +964,6 @@ impl CStr {
949
964
950
965
/// Converts this C string to a byte slice.
951
966
///
952
- /// This function will calculate the length of this string (which normally
953
- /// requires a linear amount of work to be done) and then return the
954
- /// resulting slice of `u8` elements.
955
- ///
956
967
/// The returned slice will **not** contain the trailing nul terminator that this C
957
968
/// string has.
958
969
///
@@ -1002,8 +1013,9 @@ impl CStr {
1002
1013
1003
1014
/// Yields a [`&str`] slice if the `CStr` contains valid UTF-8.
1004
1015
///
1005
- /// This function will calculate the length of this string and check for
1006
- /// UTF-8 validity, and then return the [`&str`] if it's valid.
1016
+ /// If the contents of the `CStr` are valid UTF-8 data, this
1017
+ /// function will return the corresponding [`&str`] slice. Otherwise,
1018
+ /// it will return an error with details of where UTF-8 validation failed.
1007
1019
///
1008
1020
/// > **Note**: This method is currently implemented to check for validity
1009
1021
/// > after a 0-cost cast, but it is planned to alter its definition in the
@@ -1031,18 +1043,22 @@ impl CStr {
1031
1043
1032
1044
/// Converts a `CStr` into a [`Cow`]`<`[`str`]`>`.
1033
1045
///
1034
- /// This function will calculate the length of this string (which normally
1035
- /// requires a linear amount of work to be done) and then return the
1036
- /// resulting slice as a [`Cow`]`<`[`str`]`>`, replacing any invalid UTF-8 sequences
1037
- /// with `U+FFFD REPLACEMENT CHARACTER`.
1046
+ /// If the contents of the `CStr` are valid UTF-8 data, this
1047
+ /// function will return a [`Cow`]`::`[`Borrowed`]`(`[`&str`]`)`
1048
+ /// with the the corresponding [`&str`] slice. Otherwise, it will
1049
+ /// replace any invalid UTF-8 sequences with `U+FFFD REPLACEMENT
1050
+ /// CHARACTER` and return a [`Cow`]`::`[`Owned`]`(`[`String`]`)`
1051
+ /// with the result.
1038
1052
///
1039
1053
/// > **Note**: This method is currently implemented to check for validity
1040
1054
/// > after a 0-cost cast, but it is planned to alter its definition in the
1041
1055
/// > future to perform the length calculation in addition to the UTF-8
1042
1056
/// > check whenever this method is called.
1043
1057
///
1044
1058
/// [`Cow`]: ../borrow/enum.Cow.html
1059
+ /// [`Borrowed`]: ../borrow/enum.Cow.html#variant.Borrowed
1045
1060
/// [`str`]: ../primitive.str.html
1061
+ /// [`String`]: ../string/struct.String.html
1046
1062
///
1047
1063
/// # Examples
1048
1064
///
0 commit comments