@@ -23,18 +23,20 @@ use ptr;
23
23
use slice;
24
24
use str:: { self , Utf8Error } ;
25
25
26
- /// A type representing an owned C-compatible string
26
+ /// A type representing an owned C-compatible string.
27
27
///
28
28
/// This type serves the primary purpose of being able to safely generate a
29
29
/// C-compatible string from a Rust byte slice or vector. An instance of this
30
30
/// type is a static guarantee that the underlying bytes contain no interior 0
31
31
/// bytes and the final byte is 0.
32
32
///
33
- /// A `CString` is created from either a byte slice or a byte vector. A `u8`
34
- /// slice can be obtained with the `as_bytes` method. Slices produced from a
33
+ /// A `CString` is created from either a byte slice or a byte vector. A [ `u8`]
34
+ /// slice can be obtained with the `as_bytes` method. Slices produced from a
35
35
/// `CString` do *not* contain the trailing nul terminator unless otherwise
36
36
/// specified.
37
37
///
38
+ /// [`u8`]: ../primitive.u8.html
39
+ ///
38
40
/// # Examples
39
41
///
40
42
/// ```no_run
@@ -81,12 +83,14 @@ pub struct CString {
81
83
///
82
84
/// Note that this structure is **not** `repr(C)` and is not recommended to be
83
85
/// placed in the signatures of FFI functions. Instead safe wrappers of FFI
84
- /// functions may leverage the unsafe `from_ptr` constructor to provide a safe
86
+ /// functions may leverage the unsafe [ `from_ptr`] constructor to provide a safe
85
87
/// interface to other consumers.
86
88
///
89
+ /// [`from_ptr`]: #method.from_ptr
90
+ ///
87
91
/// # Examples
88
92
///
89
- /// Inspecting a foreign C string
93
+ /// Inspecting a foreign C string:
90
94
///
91
95
/// ```no_run
92
96
/// use std::ffi::CStr;
@@ -100,7 +104,7 @@ pub struct CString {
100
104
/// }
101
105
/// ```
102
106
///
103
- /// Passing a Rust-originating C string
107
+ /// Passing a Rust-originating C string:
104
108
///
105
109
/// ```no_run
106
110
/// use std::ffi::{CString, CStr};
@@ -116,7 +120,9 @@ pub struct CString {
116
120
/// work(&s);
117
121
/// ```
118
122
///
119
- /// Converting a foreign C string into a Rust `String`
123
+ /// Converting a foreign C string into a Rust [`String`]:
124
+ ///
125
+ /// [`String`]: ../string/struct.String.html
120
126
///
121
127
/// ```no_run
122
128
/// use std::ffi::CStr;
@@ -142,14 +148,18 @@ pub struct CStr {
142
148
inner : [ c_char ]
143
149
}
144
150
145
- /// An error returned from `CString::new` to indicate that a nul byte was found
151
+ /// An error returned from [ `CString::new`] to indicate that a nul byte was found
146
152
/// in the vector provided.
153
+ ///
154
+ /// [`CString::new`]: struct.CString.html#method.new
147
155
#[ derive( Clone , PartialEq , Eq , Debug ) ]
148
156
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
149
157
pub struct NulError ( usize , Vec < u8 > ) ;
150
158
151
- /// An error returned from `CStr::from_bytes_with_nul` to indicate that a nul
159
+ /// An error returned from [ `CStr::from_bytes_with_nul`] to indicate that a nul
152
160
/// byte was found too early in the slice provided or one wasn't found at all.
161
+ ///
162
+ /// [`CStr::from_bytes_with_nul`]: struct.CStr.html#method.from_bytes_with_nul
153
163
#[ derive( Clone , PartialEq , Eq , Debug ) ]
154
164
#[ stable( feature = "cstr_from_bytes" , since = "1.10.0" ) ]
155
165
pub struct FromBytesWithNulError {
@@ -175,8 +185,10 @@ impl FromBytesWithNulError {
175
185
}
176
186
}
177
187
178
- /// An error returned from `CString::into_string` to indicate that a UTF-8 error
188
+ /// An error returned from [ `CString::into_string`] to indicate that a UTF-8 error
179
189
/// was encountered during the conversion.
190
+ ///
191
+ /// [`CString::into_string`]: struct.CString.html#method.into_string
180
192
#[ derive( Clone , PartialEq , Eq , Debug ) ]
181
193
#[ stable( feature = "cstring_into" , since = "1.7.0" ) ]
182
194
pub struct IntoStringError {
@@ -224,10 +236,12 @@ impl CString {
224
236
/// Creates a C-compatible string from a byte vector without checking for
225
237
/// interior 0 bytes.
226
238
///
227
- /// This method is equivalent to `new` except that no runtime assertion
239
+ /// This method is equivalent to [ `new`] except that no runtime assertion
228
240
/// is made that `v` contains no 0 bytes, and it requires an actual
229
241
/// byte vector, not anything that can be converted to one with Into.
230
242
///
243
+ /// [`new`]: #method.new
244
+ ///
231
245
/// # Examples
232
246
///
233
247
/// ```
@@ -252,9 +266,11 @@ impl CString {
252
266
/// # Safety
253
267
///
254
268
/// This should only ever be called with a pointer that was earlier
255
- /// obtained by calling `into_raw` on a `CString`. Other usage (e.g. trying to take
269
+ /// obtained by calling [ `into_raw`] on a `CString`. Other usage (e.g. trying to take
256
270
/// ownership of a string that was allocated by foreign code) is likely to lead
257
271
/// to undefined behavior or allocator corruption.
272
+ ///
273
+ /// [`into_raw`]: #method.into_raw
258
274
#[ stable( feature = "cstr_memory" , since = "1.4.0" ) ]
259
275
pub unsafe fn from_raw ( ptr : * mut c_char ) -> CString {
260
276
let len = libc:: strlen ( ptr) + 1 ; // Including the NUL byte
@@ -265,19 +281,23 @@ impl CString {
265
281
/// Transfers ownership of the string to a C caller.
266
282
///
267
283
/// The pointer must be returned to Rust and reconstituted using
268
- /// `from_raw` to be properly deallocated. Specifically, one
284
+ /// [ `from_raw`] to be properly deallocated. Specifically, one
269
285
/// should *not* use the standard C `free` function to deallocate
270
286
/// this string.
271
287
///
272
- /// Failure to call `from_raw` will lead to a memory leak.
288
+ /// Failure to call [`from_raw`] will lead to a memory leak.
289
+ ///
290
+ /// [`from_raw`]: #method.from_raw
273
291
#[ stable( feature = "cstr_memory" , since = "1.4.0" ) ]
274
292
pub fn into_raw ( self ) -> * mut c_char {
275
293
Box :: into_raw ( self . into_inner ( ) ) as * mut c_char
276
294
}
277
295
278
- /// Converts the `CString` into a `String` if it contains valid Unicode data.
296
+ /// Converts the `CString` into a [ `String`] if it contains valid Unicode data.
279
297
///
280
298
/// On failure, ownership of the original `CString` is returned.
299
+ ///
300
+ /// [`String`]: ../string/struct.String.html
281
301
#[ stable( feature = "cstring_into" , since = "1.7.0" ) ]
282
302
pub fn into_string ( self ) -> Result < String , IntoStringError > {
283
303
String :: from_utf8 ( self . into_bytes ( ) )
@@ -299,8 +319,10 @@ impl CString {
299
319
vec
300
320
}
301
321
302
- /// Equivalent to the `into_bytes` function except that the returned vector
322
+ /// Equivalent to the [ `into_bytes`] function except that the returned vector
303
323
/// includes the trailing nul byte.
324
+ ///
325
+ /// [`into_bytes`]: #method.into_bytes
304
326
#[ stable( feature = "cstring_into" , since = "1.7.0" ) ]
305
327
pub fn into_bytes_with_nul ( self ) -> Vec < u8 > {
306
328
self . into_inner ( ) . into_vec ( )
@@ -315,26 +337,34 @@ impl CString {
315
337
& self . inner [ ..self . inner . len ( ) - 1 ]
316
338
}
317
339
318
- /// Equivalent to the `as_bytes` function except that the returned slice
340
+ /// Equivalent to the [ `as_bytes`] function except that the returned slice
319
341
/// includes the trailing nul byte.
342
+ ///
343
+ /// [`as_bytes`]: #method.as_bytes
320
344
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
321
345
pub fn as_bytes_with_nul ( & self ) -> & [ u8 ] {
322
346
& self . inner
323
347
}
324
348
325
- /// Extracts a `CStr` slice containing the entire string.
349
+ /// Extracts a [`CStr`] slice containing the entire string.
350
+ ///
351
+ /// [`CStr`]: struct.CStr.html
326
352
#[ unstable( feature = "as_c_str" , issue = "40380" ) ]
327
353
pub fn as_c_str ( & self ) -> & CStr {
328
354
& * self
329
355
}
330
356
331
- /// Converts this `CString` into a boxed `CStr`.
357
+ /// Converts this `CString` into a boxed [`CStr`].
358
+ ///
359
+ /// [`CStr`]: struct.CStr.html
332
360
#[ unstable( feature = "into_boxed_c_str" , issue = "40380" ) ]
333
361
pub fn into_boxed_c_str ( self ) -> Box < CStr > {
334
362
unsafe { mem:: transmute ( self . into_inner ( ) ) }
335
363
}
336
364
337
- // Bypass "move out of struct which implements `Drop` trait" restriction.
365
+ // Bypass "move out of struct which implements [`Drop`] trait" restriction.
366
+ ///
367
+ /// [`Drop`]: ../ops/trait.Drop.html
338
368
fn into_inner ( self ) -> Box < [ u8 ] > {
339
369
unsafe {
340
370
let result = ptr:: read ( & self . inner ) ;
@@ -443,7 +473,9 @@ impl Default for Box<CStr> {
443
473
444
474
impl NulError {
445
475
/// Returns the position of the nul byte in the slice that was provided to
446
- /// `CString::new`.
476
+ /// [`CString::new`].
477
+ ///
478
+ /// [`CString::new`]: struct.CString.html#method.new
447
479
///
448
480
/// # Examples
449
481
///
@@ -518,8 +550,10 @@ impl fmt::Display for FromBytesWithNulError {
518
550
}
519
551
520
552
impl IntoStringError {
521
- /// Consumes this error, returning original `CString` which generated the
553
+ /// Consumes this error, returning original [ `CString`] which generated the
522
554
/// error.
555
+ ///
556
+ /// [`CString`]: struct.CString.html
523
557
#[ stable( feature = "cstring_into" , since = "1.7.0" ) ]
524
558
pub fn into_cstring ( self ) -> CString {
525
559
self . inner
@@ -557,9 +591,9 @@ impl CStr {
557
591
/// allows inspection and interoperation of non-owned C strings. This method
558
592
/// is unsafe for a number of reasons:
559
593
///
560
- /// * There is no guarantee to the validity of `ptr`
594
+ /// * There is no guarantee to the validity of `ptr`.
561
595
/// * The returned lifetime is not guaranteed to be the actual lifetime of
562
- /// `ptr`
596
+ /// `ptr`.
563
597
/// * There is no guarantee that the memory pointed to by `ptr` contains a
564
598
/// valid nul terminator byte at the end of the string.
565
599
///
@@ -703,26 +737,30 @@ impl CStr {
703
737
704
738
/// Converts this C string to a byte slice containing the trailing 0 byte.
705
739
///
706
- /// This function is the equivalent of `to_bytes` except that it will retain
740
+ /// This function is the equivalent of [ `to_bytes`] except that it will retain
707
741
/// the trailing nul instead of chopping it off.
708
742
///
709
743
/// > **Note**: This method is currently implemented as a 0-cost cast, but
710
744
/// > it is planned to alter its definition in the future to perform the
711
745
/// > length calculation whenever this method is called.
746
+ ///
747
+ /// [`to_bytes`]: #method.to_bytes
712
748
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
713
749
pub fn to_bytes_with_nul ( & self ) -> & [ u8 ] {
714
750
unsafe { mem:: transmute ( & self . inner ) }
715
751
}
716
752
717
- /// Yields a `&str` slice if the `CStr` contains valid UTF-8.
753
+ /// Yields a [ `&str`] slice if the `CStr` contains valid UTF-8.
718
754
///
719
755
/// This function will calculate the length of this string and check for
720
- /// UTF-8 validity, and then return the `&str` if it's valid.
756
+ /// UTF-8 validity, and then return the [ `&str`] if it's valid.
721
757
///
722
758
/// > **Note**: This method is currently implemented to check for validity
723
759
/// > after a 0-cost cast, but it is planned to alter its definition in the
724
760
/// > future to perform the length calculation in addition to the UTF-8
725
761
/// > check whenever this method is called.
762
+ ///
763
+ /// [`&str`]: ../primitive.str.html
726
764
#[ stable( feature = "cstr_to_str" , since = "1.4.0" ) ]
727
765
pub fn to_str ( & self ) -> Result < & str , str:: Utf8Error > {
728
766
// NB: When CStr is changed to perform the length check in .to_bytes()
@@ -732,23 +770,29 @@ impl CStr {
732
770
str:: from_utf8 ( self . to_bytes ( ) )
733
771
}
734
772
735
- /// Converts a `CStr` into a `Cow< str>`.
773
+ /// Converts a `CStr` into a [ `Cow`]`<`[` str`]` >`.
736
774
///
737
775
/// This function will calculate the length of this string (which normally
738
776
/// requires a linear amount of work to be done) and then return the
739
- /// resulting slice as a `Cow< str>`, replacing any invalid UTF-8 sequences
777
+ /// resulting slice as a [ `Cow`]`<`[` str`]` >`, replacing any invalid UTF-8 sequences
740
778
/// with `U+FFFD REPLACEMENT CHARACTER`.
741
779
///
742
780
/// > **Note**: This method is currently implemented to check for validity
743
781
/// > after a 0-cost cast, but it is planned to alter its definition in the
744
782
/// > future to perform the length calculation in addition to the UTF-8
745
783
/// > check whenever this method is called.
784
+ ///
785
+ /// [`Cow`]: ../borrow/enum.Cow.html
786
+ /// [`str`]: ../primitive.str.html
746
787
#[ stable( feature = "cstr_to_str" , since = "1.4.0" ) ]
747
788
pub fn to_string_lossy ( & self ) -> Cow < str > {
748
789
String :: from_utf8_lossy ( self . to_bytes ( ) )
749
790
}
750
791
751
- /// Converts a `Box<CStr>` into a `CString` without copying or allocating.
792
+ /// Converts a [`Box`]`<CStr>` into a [`CString`] without copying or allocating.
793
+ ///
794
+ /// [`Box`]: ../boxed/struct.Box.html
795
+ /// [`CString`]: struct.CString.html
752
796
#[ unstable( feature = "into_boxed_c_str" , issue = "40380" ) ]
753
797
pub fn into_c_string ( self : Box < CStr > ) -> CString {
754
798
unsafe { mem:: transmute ( self ) }
0 commit comments