Skip to content

Commit 2cc6839

Browse files
authored
Rollup merge of #42152 - GuillaumeGomez:cstr-docs, r=frewsxcv
Add missing links for CStr and CString r? @rust-lang/docs
2 parents 951eb55 + 819acb5 commit 2cc6839

File tree

1 file changed

+74
-30
lines changed

1 file changed

+74
-30
lines changed

src/libstd/ffi/c_str.rs

+74-30
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,20 @@ use ptr;
2323
use slice;
2424
use str::{self, Utf8Error};
2525

26-
/// A type representing an owned C-compatible string
26+
/// A type representing an owned C-compatible string.
2727
///
2828
/// This type serves the primary purpose of being able to safely generate a
2929
/// C-compatible string from a Rust byte slice or vector. An instance of this
3030
/// type is a static guarantee that the underlying bytes contain no interior 0
3131
/// bytes and the final byte is 0.
3232
///
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
3535
/// `CString` do *not* contain the trailing nul terminator unless otherwise
3636
/// specified.
3737
///
38+
/// [`u8`]: ../primitive.u8.html
39+
///
3840
/// # Examples
3941
///
4042
/// ```no_run
@@ -81,12 +83,14 @@ pub struct CString {
8183
///
8284
/// Note that this structure is **not** `repr(C)` and is not recommended to be
8385
/// 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
8587
/// interface to other consumers.
8688
///
89+
/// [`from_ptr`]: #method.from_ptr
90+
///
8791
/// # Examples
8892
///
89-
/// Inspecting a foreign C string
93+
/// Inspecting a foreign C string:
9094
///
9195
/// ```no_run
9296
/// use std::ffi::CStr;
@@ -100,7 +104,7 @@ pub struct CString {
100104
/// }
101105
/// ```
102106
///
103-
/// Passing a Rust-originating C string
107+
/// Passing a Rust-originating C string:
104108
///
105109
/// ```no_run
106110
/// use std::ffi::{CString, CStr};
@@ -116,7 +120,9 @@ pub struct CString {
116120
/// work(&s);
117121
/// ```
118122
///
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
120126
///
121127
/// ```no_run
122128
/// use std::ffi::CStr;
@@ -142,14 +148,18 @@ pub struct CStr {
142148
inner: [c_char]
143149
}
144150

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
146152
/// in the vector provided.
153+
///
154+
/// [`CString::new`]: struct.CString.html#method.new
147155
#[derive(Clone, PartialEq, Eq, Debug)]
148156
#[stable(feature = "rust1", since = "1.0.0")]
149157
pub struct NulError(usize, Vec<u8>);
150158

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
152160
/// 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
153163
#[derive(Clone, PartialEq, Eq, Debug)]
154164
#[stable(feature = "cstr_from_bytes", since = "1.10.0")]
155165
pub struct FromBytesWithNulError {
@@ -175,8 +185,10 @@ impl FromBytesWithNulError {
175185
}
176186
}
177187

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
179189
/// was encountered during the conversion.
190+
///
191+
/// [`CString::into_string`]: struct.CString.html#method.into_string
180192
#[derive(Clone, PartialEq, Eq, Debug)]
181193
#[stable(feature = "cstring_into", since = "1.7.0")]
182194
pub struct IntoStringError {
@@ -224,10 +236,12 @@ impl CString {
224236
/// Creates a C-compatible string from a byte vector without checking for
225237
/// interior 0 bytes.
226238
///
227-
/// This method is equivalent to `new` except that no runtime assertion
239+
/// This method is equivalent to [`new`] except that no runtime assertion
228240
/// is made that `v` contains no 0 bytes, and it requires an actual
229241
/// byte vector, not anything that can be converted to one with Into.
230242
///
243+
/// [`new`]: #method.new
244+
///
231245
/// # Examples
232246
///
233247
/// ```
@@ -252,9 +266,11 @@ impl CString {
252266
/// # Safety
253267
///
254268
/// 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
256270
/// ownership of a string that was allocated by foreign code) is likely to lead
257271
/// to undefined behavior or allocator corruption.
272+
///
273+
/// [`into_raw`]: #method.into_raw
258274
#[stable(feature = "cstr_memory", since = "1.4.0")]
259275
pub unsafe fn from_raw(ptr: *mut c_char) -> CString {
260276
let len = libc::strlen(ptr) + 1; // Including the NUL byte
@@ -265,19 +281,23 @@ impl CString {
265281
/// Transfers ownership of the string to a C caller.
266282
///
267283
/// 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
269285
/// should *not* use the standard C `free` function to deallocate
270286
/// this string.
271287
///
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
273291
#[stable(feature = "cstr_memory", since = "1.4.0")]
274292
pub fn into_raw(self) -> *mut c_char {
275293
Box::into_raw(self.into_inner()) as *mut c_char
276294
}
277295

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.
279297
///
280298
/// On failure, ownership of the original `CString` is returned.
299+
///
300+
/// [`String`]: ../string/struct.String.html
281301
#[stable(feature = "cstring_into", since = "1.7.0")]
282302
pub fn into_string(self) -> Result<String, IntoStringError> {
283303
String::from_utf8(self.into_bytes())
@@ -299,8 +319,10 @@ impl CString {
299319
vec
300320
}
301321

302-
/// Equivalent to the `into_bytes` function except that the returned vector
322+
/// Equivalent to the [`into_bytes`] function except that the returned vector
303323
/// includes the trailing nul byte.
324+
///
325+
/// [`into_bytes`]: #method.into_bytes
304326
#[stable(feature = "cstring_into", since = "1.7.0")]
305327
pub fn into_bytes_with_nul(self) -> Vec<u8> {
306328
self.into_inner().into_vec()
@@ -315,26 +337,34 @@ impl CString {
315337
&self.inner[..self.inner.len() - 1]
316338
}
317339

318-
/// Equivalent to the `as_bytes` function except that the returned slice
340+
/// Equivalent to the [`as_bytes`] function except that the returned slice
319341
/// includes the trailing nul byte.
342+
///
343+
/// [`as_bytes`]: #method.as_bytes
320344
#[stable(feature = "rust1", since = "1.0.0")]
321345
pub fn as_bytes_with_nul(&self) -> &[u8] {
322346
&self.inner
323347
}
324348

325-
/// Extracts a `CStr` slice containing the entire string.
349+
/// Extracts a [`CStr`] slice containing the entire string.
350+
///
351+
/// [`CStr`]: struct.CStr.html
326352
#[unstable(feature = "as_c_str", issue = "40380")]
327353
pub fn as_c_str(&self) -> &CStr {
328354
&*self
329355
}
330356

331-
/// Converts this `CString` into a boxed `CStr`.
357+
/// Converts this `CString` into a boxed [`CStr`].
358+
///
359+
/// [`CStr`]: struct.CStr.html
332360
#[unstable(feature = "into_boxed_c_str", issue = "40380")]
333361
pub fn into_boxed_c_str(self) -> Box<CStr> {
334362
unsafe { mem::transmute(self.into_inner()) }
335363
}
336364

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
338368
fn into_inner(self) -> Box<[u8]> {
339369
unsafe {
340370
let result = ptr::read(&self.inner);
@@ -443,7 +473,9 @@ impl Default for Box<CStr> {
443473

444474
impl NulError {
445475
/// 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
447479
///
448480
/// # Examples
449481
///
@@ -518,8 +550,10 @@ impl fmt::Display for FromBytesWithNulError {
518550
}
519551

520552
impl IntoStringError {
521-
/// Consumes this error, returning original `CString` which generated the
553+
/// Consumes this error, returning original [`CString`] which generated the
522554
/// error.
555+
///
556+
/// [`CString`]: struct.CString.html
523557
#[stable(feature = "cstring_into", since = "1.7.0")]
524558
pub fn into_cstring(self) -> CString {
525559
self.inner
@@ -557,9 +591,9 @@ impl CStr {
557591
/// allows inspection and interoperation of non-owned C strings. This method
558592
/// is unsafe for a number of reasons:
559593
///
560-
/// * There is no guarantee to the validity of `ptr`
594+
/// * There is no guarantee to the validity of `ptr`.
561595
/// * The returned lifetime is not guaranteed to be the actual lifetime of
562-
/// `ptr`
596+
/// `ptr`.
563597
/// * There is no guarantee that the memory pointed to by `ptr` contains a
564598
/// valid nul terminator byte at the end of the string.
565599
///
@@ -703,26 +737,30 @@ impl CStr {
703737

704738
/// Converts this C string to a byte slice containing the trailing 0 byte.
705739
///
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
707741
/// the trailing nul instead of chopping it off.
708742
///
709743
/// > **Note**: This method is currently implemented as a 0-cost cast, but
710744
/// > it is planned to alter its definition in the future to perform the
711745
/// > length calculation whenever this method is called.
746+
///
747+
/// [`to_bytes`]: #method.to_bytes
712748
#[stable(feature = "rust1", since = "1.0.0")]
713749
pub fn to_bytes_with_nul(&self) -> &[u8] {
714750
unsafe { mem::transmute(&self.inner) }
715751
}
716752

717-
/// Yields a `&str` slice if the `CStr` contains valid UTF-8.
753+
/// Yields a [`&str`] slice if the `CStr` contains valid UTF-8.
718754
///
719755
/// 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.
721757
///
722758
/// > **Note**: This method is currently implemented to check for validity
723759
/// > after a 0-cost cast, but it is planned to alter its definition in the
724760
/// > future to perform the length calculation in addition to the UTF-8
725761
/// > check whenever this method is called.
762+
///
763+
/// [`&str`]: ../primitive.str.html
726764
#[stable(feature = "cstr_to_str", since = "1.4.0")]
727765
pub fn to_str(&self) -> Result<&str, str::Utf8Error> {
728766
// NB: When CStr is changed to perform the length check in .to_bytes()
@@ -732,23 +770,29 @@ impl CStr {
732770
str::from_utf8(self.to_bytes())
733771
}
734772

735-
/// Converts a `CStr` into a `Cow<str>`.
773+
/// Converts a `CStr` into a [`Cow`]`<`[`str`]`>`.
736774
///
737775
/// This function will calculate the length of this string (which normally
738776
/// 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
740778
/// with `U+FFFD REPLACEMENT CHARACTER`.
741779
///
742780
/// > **Note**: This method is currently implemented to check for validity
743781
/// > after a 0-cost cast, but it is planned to alter its definition in the
744782
/// > future to perform the length calculation in addition to the UTF-8
745783
/// > check whenever this method is called.
784+
///
785+
/// [`Cow`]: ../borrow/enum.Cow.html
786+
/// [`str`]: ../primitive.str.html
746787
#[stable(feature = "cstr_to_str", since = "1.4.0")]
747788
pub fn to_string_lossy(&self) -> Cow<str> {
748789
String::from_utf8_lossy(self.to_bytes())
749790
}
750791

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
752796
#[unstable(feature = "into_boxed_c_str", issue = "40380")]
753797
pub fn into_c_string(self: Box<CStr>) -> CString {
754798
unsafe { mem::transmute(self) }

0 commit comments

Comments
 (0)