Skip to content

Commit 6fd5cf5

Browse files
committed
Add documentation to more From::from implementations.
For users looking at documentation through IDE popups, this gives them relevant information rather than the generic trait documentation wording “Performs the conversion”. For users reading the documentation for a specific type for any reason, this informs them when the conversion may allocate or copy significant memory versus when it is always a move or cheap copy. Notes on specific cases: * The new documentation for `From<T> for T` explains that it is not a conversion at all. * Also documented `impl<T, U> Into<U> for T where U: From<T>`, the other central blanket implementation of conversion. * I did not add documentation to conversions of a specific error type to a more general error type. * I did not add documentation to unstable code. This change was prepared by searching for the text "From<... for" and so may have missed some cases that for whatever reason did not match. I also looked for `Into` impls but did not find any worth documenting by the above criteria.
1 parent 887999d commit 6fd5cf5

File tree

16 files changed

+63
-12
lines changed

16 files changed

+63
-12
lines changed

library/alloc/src/collections/btree/map.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2047,6 +2047,8 @@ where
20472047

20482048
#[stable(feature = "std_collections_from_array", since = "1.56.0")]
20492049
impl<K: Ord, V, const N: usize> From<[(K, V); N]> for BTreeMap<K, V> {
2050+
/// Converts a `[(K, V); N]` into a `BTreeMap<(K, V)>`.
2051+
///
20502052
/// ```
20512053
/// use std::collections::BTreeMap;
20522054
///

library/alloc/src/collections/btree/set.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,8 @@ impl<T: Ord> FromIterator<T> for BTreeSet<T> {
10921092

10931093
#[stable(feature = "std_collections_from_array", since = "1.56.0")]
10941094
impl<T: Ord, const N: usize> From<[T; N]> for BTreeSet<T> {
1095+
/// Converts a `[T; N]` into a `BTreeSet<T>`.
1096+
///
10951097
/// ```
10961098
/// use std::collections::BTreeSet;
10971099
///

library/alloc/src/collections/linked_list.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1953,6 +1953,8 @@ impl<T: Hash> Hash for LinkedList<T> {
19531953

19541954
#[stable(feature = "std_collections_from_array", since = "1.56.0")]
19551955
impl<T, const N: usize> From<[T; N]> for LinkedList<T> {
1956+
/// Converts a `[T; N]` into a `LinkedList<T>`.
1957+
///
19561958
/// ```
19571959
/// use std::collections::LinkedList;
19581960
///

library/alloc/src/collections/vec_deque/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -3018,6 +3018,8 @@ impl<T, A: Allocator> From<VecDeque<T, A>> for Vec<T, A> {
30183018

30193019
#[stable(feature = "std_collections_from_array", since = "1.56.0")]
30203020
impl<T, const N: usize> From<[T; N]> for VecDeque<T> {
3021+
/// Converts a `[T; N]` into a `VecDeque<T>`.
3022+
///
30213023
/// ```
30223024
/// use std::collections::VecDeque;
30233025
///

library/alloc/src/vec/mod.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -2908,17 +2908,18 @@ impl<T: Clone> From<&mut [T]> for Vec<T> {
29082908
#[cfg(not(no_global_oom_handling))]
29092909
#[stable(feature = "vec_from_array", since = "1.44.0")]
29102910
impl<T, const N: usize> From<[T; N]> for Vec<T> {
2911-
#[cfg(not(test))]
2912-
fn from(s: [T; N]) -> Vec<T> {
2913-
<[T]>::into_vec(box s)
2914-
}
29152911
/// Allocate a `Vec<T>` and move `s`'s items into it.
29162912
///
29172913
/// # Examples
29182914
///
29192915
/// ```
29202916
/// assert_eq!(Vec::from([1, 2, 3]), vec![1, 2, 3]);
29212917
/// ```
2918+
#[cfg(not(test))]
2919+
fn from(s: [T; N]) -> Vec<T> {
2920+
<[T]>::into_vec(box s)
2921+
}
2922+
29222923
#[cfg(test)]
29232924
fn from(s: [T; N]) -> Vec<T> {
29242925
crate::slice::into_vec(box s)

library/core/src/cell.rs

+3
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ impl<T: Ord + Copy> Ord for Cell<T> {
315315
#[stable(feature = "cell_from", since = "1.12.0")]
316316
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
317317
impl<T> const From<T> for Cell<T> {
318+
/// Creates a new `Cell<T>` containing the given value.
318319
fn from(t: T) -> Cell<T> {
319320
Cell::new(t)
320321
}
@@ -1244,6 +1245,7 @@ impl<T: ?Sized + Ord> Ord for RefCell<T> {
12441245
#[stable(feature = "cell_from", since = "1.12.0")]
12451246
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
12461247
impl<T> const From<T> for RefCell<T> {
1248+
/// Creates a new `RefCell<T>` containing the given value.
12471249
fn from(t: T) -> RefCell<T> {
12481250
RefCell::new(t)
12491251
}
@@ -1986,6 +1988,7 @@ impl<T: Default> Default for UnsafeCell<T> {
19861988
#[stable(feature = "cell_from", since = "1.12.0")]
19871989
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
19881990
impl<T> const From<T> for UnsafeCell<T> {
1991+
/// Creates a new `UnsafeCell<T>` containing the given value.
19891992
fn from(t: T) -> UnsafeCell<T> {
19901993
UnsafeCell::new(t)
19911994
}

library/core/src/convert/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,10 @@ impl<T, U> Into<U> for T
538538
where
539539
U: From<T>,
540540
{
541+
/// Calls `U::from(self)`.
542+
///
543+
/// That is, this conversion is whatever the implementation of
544+
/// <code>[From]&lt;T&gt; for U</code> chooses to do.
541545
fn into(self) -> U {
542546
U::from(self)
543547
}
@@ -547,6 +551,7 @@ where
547551
#[stable(feature = "rust1", since = "1.0.0")]
548552
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
549553
impl<T> const From<T> for T {
554+
/// Returns the argument unchanged.
550555
fn from(t: T) -> T {
551556
t
552557
}

library/core/src/lazy.rs

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ impl<T: Eq> Eq for OnceCell<T> {}
7575

7676
#[unstable(feature = "once_cell", issue = "74465")]
7777
impl<T> const From<T> for OnceCell<T> {
78+
/// Creates a new `OnceCell<T>` which already contains the given `value`.
7879
fn from(value: T) -> Self {
7980
OnceCell { inner: UnsafeCell::new(Some(value)) }
8081
}

library/core/src/ptr/non_null.rs

+6
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,9 @@ impl<T: ?Sized> const From<Unique<T>> for NonNull<T> {
714714
#[stable(feature = "nonnull", since = "1.25.0")]
715715
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
716716
impl<T: ?Sized> const From<&mut T> for NonNull<T> {
717+
/// Converts a `&mut T` to a `NonNull<T>`.
718+
///
719+
/// This conversion is safe and infallible since references cannot be null.
717720
#[inline]
718721
fn from(reference: &mut T) -> Self {
719722
// SAFETY: A mutable reference cannot be null.
@@ -724,6 +727,9 @@ impl<T: ?Sized> const From<&mut T> for NonNull<T> {
724727
#[stable(feature = "nonnull", since = "1.25.0")]
725728
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
726729
impl<T: ?Sized> const From<&T> for NonNull<T> {
730+
/// Converts a `&T` to a `NonNull<T>`.
731+
///
732+
/// This conversion is safe and infallible since references cannot be null.
727733
#[inline]
728734
fn from(reference: &T) -> Self {
729735
// SAFETY: A reference cannot be null, so the conditions for

library/core/src/ptr/unique.rs

+3
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ impl<T: ?Sized> fmt::Pointer for Unique<T> {
178178

179179
#[unstable(feature = "ptr_internals", issue = "none")]
180180
impl<T: ?Sized> const From<&mut T> for Unique<T> {
181+
/// Converts a `&mut T` to a `Unique<T>`.
182+
///
183+
/// This conversion is infallible since references cannot be null.
181184
#[inline]
182185
fn from(reference: &mut T) -> Self {
183186
// SAFETY: A mutable reference cannot be null

library/core/src/sync/atomic.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1294,6 +1294,7 @@ impl const From<bool> for AtomicBool {
12941294
#[stable(feature = "atomic_from", since = "1.23.0")]
12951295
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
12961296
impl<T> const From<*mut T> for AtomicPtr<T> {
1297+
/// Converts a `*mut T` into an `AtomicPtr<T>`.
12971298
#[inline]
12981299
fn from(p: *mut T) -> Self {
12991300
Self::new(p)

library/core/src/task/poll.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ impl<T, E> Poll<Option<Result<T, E>>> {
243243
#[stable(feature = "futures_api", since = "1.36.0")]
244244
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
245245
impl<T> const From<T> for Poll<T> {
246-
/// Convert to a `Ready` variant.
246+
/// Moves the value into a [`Poll::Ready`] to make a `Poll<T>`.
247247
///
248248
/// # Example
249249
///

library/std/src/ffi/c_str.rs

+11
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,8 @@ impl Borrow<CStr> for CString {
848848

849849
#[stable(feature = "cstring_from_cow_cstr", since = "1.28.0")]
850850
impl<'a> From<Cow<'a, CStr>> for CString {
851+
/// Converts a `Cow<'a, CStr>` into a `CString`, by copying the contents if they are
852+
/// borrowed.
851853
#[inline]
852854
fn from(s: Cow<'a, CStr>) -> Self {
853855
s.into_owned()
@@ -856,6 +858,8 @@ impl<'a> From<Cow<'a, CStr>> for CString {
856858

857859
#[stable(feature = "box_from_c_str", since = "1.17.0")]
858860
impl From<&CStr> for Box<CStr> {
861+
/// Converts a `&CStr` into a `Box<CStr>`,
862+
/// by copying the contents into a newly allocated [`Box`].
859863
fn from(s: &CStr) -> Box<CStr> {
860864
let boxed: Box<[u8]> = Box::from(s.to_bytes_with_nul());
861865
unsafe { Box::from_raw(Box::into_raw(boxed) as *mut CStr) }
@@ -864,6 +868,8 @@ impl From<&CStr> for Box<CStr> {
864868

865869
#[stable(feature = "box_from_cow", since = "1.45.0")]
866870
impl From<Cow<'_, CStr>> for Box<CStr> {
871+
/// Converts a `Cow<'a, CStr>` into a `Box<CStr>`,
872+
/// by copying the contents if they are borrowed.
867873
#[inline]
868874
fn from(cow: Cow<'_, CStr>) -> Box<CStr> {
869875
match cow {
@@ -960,6 +966,8 @@ impl From<CString> for Arc<CStr> {
960966

961967
#[stable(feature = "shared_from_slice2", since = "1.24.0")]
962968
impl From<&CStr> for Arc<CStr> {
969+
/// Converts a `&CStr` into a `Arc<CStr>`,
970+
/// by copying the contents into a newly allocated [`Arc`].
963971
#[inline]
964972
fn from(s: &CStr) -> Arc<CStr> {
965973
let arc: Arc<[u8]> = Arc::from(s.to_bytes_with_nul());
@@ -979,6 +987,8 @@ impl From<CString> for Rc<CStr> {
979987

980988
#[stable(feature = "shared_from_slice2", since = "1.24.0")]
981989
impl From<&CStr> for Rc<CStr> {
990+
/// Converts a `&CStr` into a `Rc<CStr>`,
991+
/// by copying the contents into a newly allocated [`Rc`].
982992
#[inline]
983993
fn from(s: &CStr) -> Rc<CStr> {
984994
let rc: Rc<[u8]> = Rc::from(s.to_bytes_with_nul());
@@ -1504,6 +1514,7 @@ impl ToOwned for CStr {
15041514

15051515
#[stable(feature = "cstring_asref", since = "1.7.0")]
15061516
impl From<&CStr> for CString {
1517+
/// Copies the contents of the `&CStr` into a newly allocated `CString`.
15071518
fn from(s: &CStr) -> CString {
15081519
s.to_owned()
15091520
}

library/std/src/ffi/os_str.rs

+12
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,8 @@ impl From<String> for OsString {
371371

372372
#[stable(feature = "rust1", since = "1.0.0")]
373373
impl<T: ?Sized + AsRef<OsStr>> From<&T> for OsString {
374+
/// Copies any value implementing <code>[AsRef]&lt;[OsStr]&gt;</code>
375+
/// into a newly allocated [`OsString`].
374376
fn from(s: &T) -> OsString {
375377
s.as_ref().to_os_string()
376378
}
@@ -861,6 +863,7 @@ impl OsStr {
861863

862864
#[stable(feature = "box_from_os_str", since = "1.17.0")]
863865
impl From<&OsStr> for Box<OsStr> {
866+
/// Copies the string into a newly allocated <code>[Box]&lt;[OsStr]&gt;</code>.
864867
#[inline]
865868
fn from(s: &OsStr) -> Box<OsStr> {
866869
let rw = Box::into_raw(s.inner.into_box()) as *mut OsStr;
@@ -870,6 +873,8 @@ impl From<&OsStr> for Box<OsStr> {
870873

871874
#[stable(feature = "box_from_cow", since = "1.45.0")]
872875
impl From<Cow<'_, OsStr>> for Box<OsStr> {
876+
/// Converts a `Cow<'a, OsStr>` into a <code>[Box]&lt;[OsStr]&gt;</code>,
877+
/// by copying the contents if they are borrowed.
873878
#[inline]
874879
fn from(cow: Cow<'_, OsStr>) -> Box<OsStr> {
875880
match cow {
@@ -918,6 +923,7 @@ impl From<OsString> for Arc<OsStr> {
918923

919924
#[stable(feature = "shared_from_slice2", since = "1.24.0")]
920925
impl From<&OsStr> for Arc<OsStr> {
926+
/// Copies the string into a newly allocated <code>[Arc]&lt;[OsStr]&gt;</code>.
921927
#[inline]
922928
fn from(s: &OsStr) -> Arc<OsStr> {
923929
let arc = s.inner.into_arc();
@@ -937,6 +943,7 @@ impl From<OsString> for Rc<OsStr> {
937943

938944
#[stable(feature = "shared_from_slice2", since = "1.24.0")]
939945
impl From<&OsStr> for Rc<OsStr> {
946+
/// Copies the string into a newly allocated <code>[Rc]&lt;[OsStr]&gt;</code>.
940947
#[inline]
941948
fn from(s: &OsStr) -> Rc<OsStr> {
942949
let rc = s.inner.into_rc();
@@ -946,6 +953,7 @@ impl From<&OsStr> for Rc<OsStr> {
946953

947954
#[stable(feature = "cow_from_osstr", since = "1.28.0")]
948955
impl<'a> From<OsString> for Cow<'a, OsStr> {
956+
/// Moves the string into a [`Cow::Owned`].
949957
#[inline]
950958
fn from(s: OsString) -> Cow<'a, OsStr> {
951959
Cow::Owned(s)
@@ -954,6 +962,7 @@ impl<'a> From<OsString> for Cow<'a, OsStr> {
954962

955963
#[stable(feature = "cow_from_osstr", since = "1.28.0")]
956964
impl<'a> From<&'a OsStr> for Cow<'a, OsStr> {
965+
/// Converts the string reference into a [`Cow::Borrowed`].
957966
#[inline]
958967
fn from(s: &'a OsStr) -> Cow<'a, OsStr> {
959968
Cow::Borrowed(s)
@@ -962,6 +971,7 @@ impl<'a> From<&'a OsStr> for Cow<'a, OsStr> {
962971

963972
#[stable(feature = "cow_from_osstr", since = "1.28.0")]
964973
impl<'a> From<&'a OsString> for Cow<'a, OsStr> {
974+
/// Converts the string reference into a [`Cow::Borrowed`].
965975
#[inline]
966976
fn from(s: &'a OsString) -> Cow<'a, OsStr> {
967977
Cow::Borrowed(s.as_os_str())
@@ -970,6 +980,8 @@ impl<'a> From<&'a OsString> for Cow<'a, OsStr> {
970980

971981
#[stable(feature = "osstring_from_cow_osstr", since = "1.28.0")]
972982
impl<'a> From<Cow<'a, OsStr>> for OsString {
983+
/// Converts a `Cow<'a, OsStr>` into an [`OsString`],
984+
/// by copying the contents if they are borrowed.
973985
#[inline]
974986
fn from(s: Cow<'a, OsStr>) -> Self {
975987
s.into_owned()

library/std/src/path.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1581,7 +1581,7 @@ impl From<Cow<'_, Path>> for Box<Path> {
15811581

15821582
#[stable(feature = "path_buf_from_box", since = "1.18.0")]
15831583
impl From<Box<Path>> for PathBuf {
1584-
/// Converts a `Box<Path>` into a `PathBuf`
1584+
/// Converts a <code>[Box]&lt;[Path]&gt;</code> into a [`PathBuf`].
15851585
///
15861586
/// This conversion does not allocate or copy memory.
15871587
#[inline]
@@ -1592,7 +1592,7 @@ impl From<Box<Path>> for PathBuf {
15921592

15931593
#[stable(feature = "box_from_path_buf", since = "1.20.0")]
15941594
impl From<PathBuf> for Box<Path> {
1595-
/// Converts a `PathBuf` into a `Box<Path>`
1595+
/// Converts a [`PathBuf`] into a <code>[Box]&lt;[Path]&gt;</code>.
15961596
///
15971597
/// This conversion currently should not allocate memory,
15981598
/// but this behavior is not guaranteed on all platforms or in all future versions.
@@ -1612,7 +1612,7 @@ impl Clone for Box<Path> {
16121612

16131613
#[stable(feature = "rust1", since = "1.0.0")]
16141614
impl<T: ?Sized + AsRef<OsStr>> From<&T> for PathBuf {
1615-
/// Converts a borrowed `OsStr` to a `PathBuf`.
1615+
/// Converts a borrowed [`OsStr`] to a [`PathBuf`].
16161616
///
16171617
/// Allocates a [`PathBuf`] and copies the data into it.
16181618
#[inline]

library/std/src/process.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,7 @@ impl fmt::Debug for Stdio {
12891289

12901290
#[stable(feature = "stdio_from", since = "1.20.0")]
12911291
impl From<ChildStdin> for Stdio {
1292-
/// Converts a `ChildStdin` into a `Stdio`
1292+
/// Converts a [`ChildStdin`] into a [`Stdio`].
12931293
///
12941294
/// # Examples
12951295
///
@@ -1318,7 +1318,7 @@ impl From<ChildStdin> for Stdio {
13181318

13191319
#[stable(feature = "stdio_from", since = "1.20.0")]
13201320
impl From<ChildStdout> for Stdio {
1321-
/// Converts a `ChildStdout` into a `Stdio`
1321+
/// Converts a [`ChildStdout`] into a [`Stdio`].
13221322
///
13231323
/// # Examples
13241324
///
@@ -1347,7 +1347,7 @@ impl From<ChildStdout> for Stdio {
13471347

13481348
#[stable(feature = "stdio_from", since = "1.20.0")]
13491349
impl From<ChildStderr> for Stdio {
1350-
/// Converts a `ChildStderr` into a `Stdio`
1350+
/// Converts a [`ChildStderr`] into a [`Stdio`].
13511351
///
13521352
/// # Examples
13531353
///
@@ -1378,7 +1378,7 @@ impl From<ChildStderr> for Stdio {
13781378

13791379
#[stable(feature = "stdio_from", since = "1.20.0")]
13801380
impl From<fs::File> for Stdio {
1381-
/// Converts a `File` into a `Stdio`
1381+
/// Converts a [`File`](fs::File) into a [`Stdio`].
13821382
///
13831383
/// # Examples
13841384
///

0 commit comments

Comments
 (0)