Skip to content

Commit 1cc0ae4

Browse files
authored
Rollup merge of #89869 - kpreid:from-doc, r=yaahc
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. * The new documentation for construction of maps and sets from arrays of keys mentions the handling of duplicates. Future work could be to do this for *all* code paths that convert an iterable to a map or set. * 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.
2 parents 930fc4f + 6fd5cf5 commit 1cc0ae4

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
@@ -2052,6 +2052,8 @@ where
20522052

20532053
#[stable(feature = "std_collections_from_array", since = "1.56.0")]
20542054
impl<K: Ord, V, const N: usize> From<[(K, V); N]> for BTreeMap<K, V> {
2055+
/// Converts a `[(K, V); N]` into a `BTreeMap<(K, V)>`.
2056+
///
20552057
/// ```
20562058
/// use std::collections::BTreeMap;
20572059
///

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

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

10981098
#[stable(feature = "std_collections_from_array", since = "1.56.0")]
10991099
impl<T: Ord, const N: usize> From<[T; N]> for BTreeSet<T> {
1100+
/// Converts a `[T; N]` into a `BTreeSet<T>`.
1101+
///
11001102
/// ```
11011103
/// use std::collections::BTreeSet;
11021104
///

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
@@ -3049,6 +3049,8 @@ impl<T, A: Allocator> From<VecDeque<T, A>> for Vec<T, A> {
30493049

30503050
#[stable(feature = "std_collections_from_array", since = "1.56.0")]
30513051
impl<T, const N: usize> From<[T; N]> for VecDeque<T> {
3052+
/// Converts a `[T; N]` into a `VecDeque<T>`.
3053+
///
30523054
/// ```
30533055
/// use std::collections::VecDeque;
30543056
///

library/alloc/src/vec/mod.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -2906,17 +2906,18 @@ impl<T: Clone> From<&mut [T]> for Vec<T> {
29062906
#[cfg(not(no_global_oom_handling))]
29072907
#[stable(feature = "vec_from_array", since = "1.44.0")]
29082908
impl<T, const N: usize> From<[T; N]> for Vec<T> {
2909-
#[cfg(not(test))]
2910-
fn from(s: [T; N]) -> Vec<T> {
2911-
<[T]>::into_vec(box s)
2912-
}
29132909
/// Allocate a `Vec<T>` and move `s`'s items into it.
29142910
///
29152911
/// # Examples
29162912
///
29172913
/// ```
29182914
/// assert_eq!(Vec::from([1, 2, 3]), vec![1, 2, 3]);
29192915
/// ```
2916+
#[cfg(not(test))]
2917+
fn from(s: [T; N]) -> Vec<T> {
2918+
<[T]>::into_vec(box s)
2919+
}
2920+
29202921
#[cfg(test)]
29212922
fn from(s: [T; N]) -> Vec<T> {
29222923
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
}
@@ -1979,6 +1981,7 @@ impl<T: Default> Default for UnsafeCell<T> {
19791981
#[stable(feature = "cell_from", since = "1.12.0")]
19801982
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
19811983
impl<T> const From<T> for UnsafeCell<T> {
1984+
/// Creates a new `UnsafeCell<T>` containing the given value.
19821985
fn from(t: T) -> UnsafeCell<T> {
19831986
UnsafeCell::new(t)
19841987
}

library/core/src/convert/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,10 @@ impl<T, U> const Into<U> for T
538538
where
539539
U: ~const 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
@@ -721,6 +721,9 @@ impl<T: ?Sized> const From<Unique<T>> for NonNull<T> {
721721
#[stable(feature = "nonnull", since = "1.25.0")]
722722
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
723723
impl<T: ?Sized> const From<&mut T> for NonNull<T> {
724+
/// Converts a `&mut T` to a `NonNull<T>`.
725+
///
726+
/// This conversion is safe and infallible since references cannot be null.
724727
#[inline]
725728
fn from(reference: &mut T) -> Self {
726729
// SAFETY: A mutable reference cannot be null.
@@ -731,6 +734,9 @@ impl<T: ?Sized> const From<&mut T> for NonNull<T> {
731734
#[stable(feature = "nonnull", since = "1.25.0")]
732735
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
733736
impl<T: ?Sized> const From<&T> for NonNull<T> {
737+
/// Converts a `&T` to a `NonNull<T>`.
738+
///
739+
/// This conversion is safe and infallible since references cannot be null.
734740
#[inline]
735741
fn from(reference: &T) -> Self {
736742
// 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
@@ -1295,6 +1295,7 @@ impl const From<bool> for AtomicBool {
12951295
#[stable(feature = "atomic_from", since = "1.23.0")]
12961296
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
12971297
impl<T> const From<*mut T> for AtomicPtr<T> {
1298+
/// Converts a `*mut T` into an `AtomicPtr<T>`.
12981299
#[inline]
12991300
fn from(p: *mut T) -> Self {
13001301
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
@@ -871,6 +871,8 @@ impl Borrow<CStr> for CString {
871871

872872
#[stable(feature = "cstring_from_cow_cstr", since = "1.28.0")]
873873
impl<'a> From<Cow<'a, CStr>> for CString {
874+
/// Converts a `Cow<'a, CStr>` into a `CString`, by copying the contents if they are
875+
/// borrowed.
874876
#[inline]
875877
fn from(s: Cow<'a, CStr>) -> Self {
876878
s.into_owned()
@@ -879,6 +881,8 @@ impl<'a> From<Cow<'a, CStr>> for CString {
879881

880882
#[stable(feature = "box_from_c_str", since = "1.17.0")]
881883
impl From<&CStr> for Box<CStr> {
884+
/// Converts a `&CStr` into a `Box<CStr>`,
885+
/// by copying the contents into a newly allocated [`Box`].
882886
fn from(s: &CStr) -> Box<CStr> {
883887
let boxed: Box<[u8]> = Box::from(s.to_bytes_with_nul());
884888
unsafe { Box::from_raw(Box::into_raw(boxed) as *mut CStr) }
@@ -887,6 +891,8 @@ impl From<&CStr> for Box<CStr> {
887891

888892
#[stable(feature = "box_from_cow", since = "1.45.0")]
889893
impl From<Cow<'_, CStr>> for Box<CStr> {
894+
/// Converts a `Cow<'a, CStr>` into a `Box<CStr>`,
895+
/// by copying the contents if they are borrowed.
890896
#[inline]
891897
fn from(cow: Cow<'_, CStr>) -> Box<CStr> {
892898
match cow {
@@ -984,6 +990,8 @@ impl From<CString> for Arc<CStr> {
984990

985991
#[stable(feature = "shared_from_slice2", since = "1.24.0")]
986992
impl From<&CStr> for Arc<CStr> {
993+
/// Converts a `&CStr` into a `Arc<CStr>`,
994+
/// by copying the contents into a newly allocated [`Arc`].
987995
#[inline]
988996
fn from(s: &CStr) -> Arc<CStr> {
989997
let arc: Arc<[u8]> = Arc::from(s.to_bytes_with_nul());
@@ -1004,6 +1012,8 @@ impl From<CString> for Rc<CStr> {
10041012

10051013
#[stable(feature = "shared_from_slice2", since = "1.24.0")]
10061014
impl From<&CStr> for Rc<CStr> {
1015+
/// Converts a `&CStr` into a `Rc<CStr>`,
1016+
/// by copying the contents into a newly allocated [`Rc`].
10071017
#[inline]
10081018
fn from(s: &CStr) -> Rc<CStr> {
10091019
let rc: Rc<[u8]> = Rc::from(s.to_bytes_with_nul());
@@ -1530,6 +1540,7 @@ impl ToOwned for CStr {
15301540

15311541
#[stable(feature = "cstring_asref", since = "1.7.0")]
15321542
impl From<&CStr> for CString {
1543+
/// Copies the contents of the `&CStr` into a newly allocated `CString`.
15331544
fn from(s: &CStr) -> CString {
15341545
s.to_owned()
15351546
}

library/std/src/ffi/os_str.rs

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

453453
#[stable(feature = "rust1", since = "1.0.0")]
454454
impl<T: ?Sized + AsRef<OsStr>> From<&T> for OsString {
455+
/// Copies any value implementing <code>[AsRef]&lt;[OsStr]&gt;</code>
456+
/// into a newly allocated [`OsString`].
455457
fn from(s: &T) -> OsString {
456458
s.as_ref().to_os_string()
457459
}
@@ -942,6 +944,7 @@ impl OsStr {
942944

943945
#[stable(feature = "box_from_os_str", since = "1.17.0")]
944946
impl From<&OsStr> for Box<OsStr> {
947+
/// Copies the string into a newly allocated <code>[Box]&lt;[OsStr]&gt;</code>.
945948
#[inline]
946949
fn from(s: &OsStr) -> Box<OsStr> {
947950
let rw = Box::into_raw(s.inner.into_box()) as *mut OsStr;
@@ -951,6 +954,8 @@ impl From<&OsStr> for Box<OsStr> {
951954

952955
#[stable(feature = "box_from_cow", since = "1.45.0")]
953956
impl From<Cow<'_, OsStr>> for Box<OsStr> {
957+
/// Converts a `Cow<'a, OsStr>` into a <code>[Box]&lt;[OsStr]&gt;</code>,
958+
/// by copying the contents if they are borrowed.
954959
#[inline]
955960
fn from(cow: Cow<'_, OsStr>) -> Box<OsStr> {
956961
match cow {
@@ -1000,6 +1005,7 @@ impl From<OsString> for Arc<OsStr> {
10001005

10011006
#[stable(feature = "shared_from_slice2", since = "1.24.0")]
10021007
impl From<&OsStr> for Arc<OsStr> {
1008+
/// Copies the string into a newly allocated <code>[Arc]&lt;[OsStr]&gt;</code>.
10031009
#[inline]
10041010
fn from(s: &OsStr) -> Arc<OsStr> {
10051011
let arc = s.inner.into_arc();
@@ -1020,6 +1026,7 @@ impl From<OsString> for Rc<OsStr> {
10201026

10211027
#[stable(feature = "shared_from_slice2", since = "1.24.0")]
10221028
impl From<&OsStr> for Rc<OsStr> {
1029+
/// Copies the string into a newly allocated <code>[Rc]&lt;[OsStr]&gt;</code>.
10231030
#[inline]
10241031
fn from(s: &OsStr) -> Rc<OsStr> {
10251032
let rc = s.inner.into_rc();
@@ -1029,6 +1036,7 @@ impl From<&OsStr> for Rc<OsStr> {
10291036

10301037
#[stable(feature = "cow_from_osstr", since = "1.28.0")]
10311038
impl<'a> From<OsString> for Cow<'a, OsStr> {
1039+
/// Moves the string into a [`Cow::Owned`].
10321040
#[inline]
10331041
fn from(s: OsString) -> Cow<'a, OsStr> {
10341042
Cow::Owned(s)
@@ -1037,6 +1045,7 @@ impl<'a> From<OsString> for Cow<'a, OsStr> {
10371045

10381046
#[stable(feature = "cow_from_osstr", since = "1.28.0")]
10391047
impl<'a> From<&'a OsStr> for Cow<'a, OsStr> {
1048+
/// Converts the string reference into a [`Cow::Borrowed`].
10401049
#[inline]
10411050
fn from(s: &'a OsStr) -> Cow<'a, OsStr> {
10421051
Cow::Borrowed(s)
@@ -1045,6 +1054,7 @@ impl<'a> From<&'a OsStr> for Cow<'a, OsStr> {
10451054

10461055
#[stable(feature = "cow_from_osstr", since = "1.28.0")]
10471056
impl<'a> From<&'a OsString> for Cow<'a, OsStr> {
1057+
/// Converts the string reference into a [`Cow::Borrowed`].
10481058
#[inline]
10491059
fn from(s: &'a OsString) -> Cow<'a, OsStr> {
10501060
Cow::Borrowed(s.as_os_str())
@@ -1053,6 +1063,8 @@ impl<'a> From<&'a OsString> for Cow<'a, OsStr> {
10531063

10541064
#[stable(feature = "osstring_from_cow_osstr", since = "1.28.0")]
10551065
impl<'a> From<Cow<'a, OsStr>> for OsString {
1066+
/// Converts a `Cow<'a, OsStr>` into an [`OsString`],
1067+
/// by copying the contents if they are borrowed.
10561068
#[inline]
10571069
fn from(s: Cow<'a, OsStr>) -> Self {
10581070
s.into_owned()

library/std/src/path.rs

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

16011601
#[stable(feature = "path_buf_from_box", since = "1.18.0")]
16021602
impl From<Box<Path>> for PathBuf {
1603-
/// Converts a `Box<Path>` into a `PathBuf`
1603+
/// Converts a <code>[Box]&lt;[Path]&gt;</code> into a [`PathBuf`].
16041604
///
16051605
/// This conversion does not allocate or copy memory.
16061606
#[inline]
@@ -1611,7 +1611,7 @@ impl From<Box<Path>> for PathBuf {
16111611

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

16321632
#[stable(feature = "rust1", since = "1.0.0")]
16331633
impl<T: ?Sized + AsRef<OsStr>> From<&T> for PathBuf {
1634-
/// Converts a borrowed `OsStr` to a `PathBuf`.
1634+
/// Converts a borrowed [`OsStr`] to a [`PathBuf`].
16351635
///
16361636
/// Allocates a [`PathBuf`] and copies the data into it.
16371637
#[inline]

library/std/src/process.rs

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

12781278
#[stable(feature = "stdio_from", since = "1.20.0")]
12791279
impl From<ChildStdin> for Stdio {
1280-
/// Converts a `ChildStdin` into a `Stdio`
1280+
/// Converts a [`ChildStdin`] into a [`Stdio`].
12811281
///
12821282
/// # Examples
12831283
///
@@ -1306,7 +1306,7 @@ impl From<ChildStdin> for Stdio {
13061306

13071307
#[stable(feature = "stdio_from", since = "1.20.0")]
13081308
impl From<ChildStdout> for Stdio {
1309-
/// Converts a `ChildStdout` into a `Stdio`
1309+
/// Converts a [`ChildStdout`] into a [`Stdio`].
13101310
///
13111311
/// # Examples
13121312
///
@@ -1335,7 +1335,7 @@ impl From<ChildStdout> for Stdio {
13351335

13361336
#[stable(feature = "stdio_from", since = "1.20.0")]
13371337
impl From<ChildStderr> for Stdio {
1338-
/// Converts a `ChildStderr` into a `Stdio`
1338+
/// Converts a [`ChildStderr`] into a [`Stdio`].
13391339
///
13401340
/// # Examples
13411341
///
@@ -1366,7 +1366,7 @@ impl From<ChildStderr> for Stdio {
13661366

13671367
#[stable(feature = "stdio_from", since = "1.20.0")]
13681368
impl From<fs::File> for Stdio {
1369-
/// Converts a `File` into a `Stdio`
1369+
/// Converts a [`File`](fs::File) into a [`Stdio`].
13701370
///
13711371
/// # Examples
13721372
///

0 commit comments

Comments
 (0)