Skip to content

Commit 9940ed0

Browse files
committed
docs: clean up trait docs for tuples
1 parent 7fe2c4b commit 9940ed0

File tree

3 files changed

+132
-72
lines changed

3 files changed

+132
-72
lines changed

library/core/src/fmt/mod.rs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2313,23 +2313,39 @@ macro_rules! peel {
23132313
macro_rules! tuple {
23142314
() => ();
23152315
( $($name:ident,)+ ) => (
2316-
#[stable(feature = "rust1", since = "1.0.0")]
2317-
impl<$($name:Debug),+> Debug for ($($name,)+) where last_type!($($name,)+): ?Sized {
2318-
#[allow(non_snake_case, unused_assignments)]
2319-
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
2320-
let mut builder = f.debug_tuple("");
2321-
let ($(ref $name,)+) = *self;
2322-
$(
2323-
builder.field(&$name);
2324-
)+
2325-
2326-
builder.finish()
2316+
maybe_tuple_doc! {
2317+
$($name)+ @
2318+
#[stable(feature = "rust1", since = "1.0.0")]
2319+
impl<$($name:Debug),+> Debug for ($($name,)+) where last_type!($($name,)+): ?Sized {
2320+
#[allow(non_snake_case, unused_assignments)]
2321+
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
2322+
let mut builder = f.debug_tuple("");
2323+
let ($(ref $name,)+) = *self;
2324+
$(
2325+
builder.field(&$name);
2326+
)+
2327+
2328+
builder.finish()
2329+
}
23272330
}
23282331
}
23292332
peel! { $($name,)+ }
23302333
)
23312334
}
23322335

2336+
macro_rules! maybe_tuple_doc {
2337+
($a:ident @ #[$meta:meta] $item:item) => {
2338+
#[doc = "This trait is implemented for tuples up to twelve items long."]
2339+
#[$meta]
2340+
$item
2341+
};
2342+
($a:ident $($rest_a:ident)+ @ #[$meta:meta] $item:item) => {
2343+
#[doc(hidden)]
2344+
#[$meta]
2345+
$item
2346+
};
2347+
}
2348+
23332349
macro_rules! last_type {
23342350
($a:ident,) => { $a };
23352351
($a:ident, $($rest_a:ident,)+) => { last_type!($($rest_a,)+) };

library/core/src/hash/mod.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -883,18 +883,34 @@ mod impls {
883883
);
884884

885885
( $($name:ident)+) => (
886-
#[stable(feature = "rust1", since = "1.0.0")]
887-
impl<$($name: Hash),+> Hash for ($($name,)+) where last_type!($($name,)+): ?Sized {
888-
#[allow(non_snake_case)]
889-
#[inline]
890-
fn hash<S: Hasher>(&self, state: &mut S) {
891-
let ($(ref $name,)+) = *self;
892-
$($name.hash(state);)+
886+
maybe_tuple_doc! {
887+
$($name)+ @
888+
#[stable(feature = "rust1", since = "1.0.0")]
889+
impl<$($name: Hash),+> Hash for ($($name,)+) where last_type!($($name,)+): ?Sized {
890+
#[allow(non_snake_case)]
891+
#[inline]
892+
fn hash<S: Hasher>(&self, state: &mut S) {
893+
let ($(ref $name,)+) = *self;
894+
$($name.hash(state);)+
895+
}
893896
}
894897
}
895898
);
896899
}
897900

901+
macro_rules! maybe_tuple_doc {
902+
($a:ident @ #[$meta:meta] $item:item) => {
903+
#[doc = "This trait is implemented for tuples up to twelve items long."]
904+
#[$meta]
905+
$item
906+
};
907+
($a:ident $($rest_a:ident)+ @ #[$meta:meta] $item:item) => {
908+
#[doc(hidden)]
909+
#[$meta]
910+
$item
911+
};
912+
}
913+
898914
macro_rules! last_type {
899915
($a:ident,) => { $a };
900916
($a:ident, $($rest_a:ident,)+) => { last_type!($($rest_a,)+) };

library/core/src/tuple.rs

Lines changed: 82 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -19,75 +19,103 @@ macro_rules! tuple_impls {
1919
};
2020
// "Private" internal implementation
2121
(@impl $( $T:ident )+) => {
22-
#[stable(feature = "rust1", since = "1.0.0")]
23-
impl<$($T:PartialEq),+> PartialEq for ($($T,)+)
24-
where
25-
last_type!($($T,)+): ?Sized
26-
{
27-
#[inline]
28-
fn eq(&self, other: &($($T,)+)) -> bool {
29-
$( ${ignore(T)} self.${index()} == other.${index()} )&&+
30-
}
31-
#[inline]
32-
fn ne(&self, other: &($($T,)+)) -> bool {
33-
$( ${ignore(T)} self.${index()} != other.${index()} )||+
22+
maybe_tuple_doc! {
23+
$($T)+ @
24+
#[stable(feature = "rust1", since = "1.0.0")]
25+
impl<$($T:PartialEq),+> PartialEq for ($($T,)+)
26+
where
27+
last_type!($($T,)+): ?Sized
28+
{
29+
#[inline]
30+
fn eq(&self, other: &($($T,)+)) -> bool {
31+
$( ${ignore(T)} self.${index()} == other.${index()} )&&+
32+
}
33+
#[inline]
34+
fn ne(&self, other: &($($T,)+)) -> bool {
35+
$( ${ignore(T)} self.${index()} != other.${index()} )||+
36+
}
3437
}
3538
}
3639

37-
#[stable(feature = "rust1", since = "1.0.0")]
38-
impl<$($T:Eq),+> Eq for ($($T,)+)
39-
where
40-
last_type!($($T,)+): ?Sized
41-
{}
40+
maybe_tuple_doc! {
41+
$($T)+ @
42+
#[stable(feature = "rust1", since = "1.0.0")]
43+
impl<$($T:Eq),+> Eq for ($($T,)+)
44+
where
45+
last_type!($($T,)+): ?Sized
46+
{}
47+
}
4248

43-
#[stable(feature = "rust1", since = "1.0.0")]
44-
impl<$($T:PartialOrd + PartialEq),+> PartialOrd for ($($T,)+)
45-
where
46-
last_type!($($T,)+): ?Sized
47-
{
48-
#[inline]
49-
fn partial_cmp(&self, other: &($($T,)+)) -> Option<Ordering> {
50-
lexical_partial_cmp!($( ${ignore(T)} self.${index()}, other.${index()} ),+)
51-
}
52-
#[inline]
53-
fn lt(&self, other: &($($T,)+)) -> bool {
54-
lexical_ord!(lt, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
55-
}
56-
#[inline]
57-
fn le(&self, other: &($($T,)+)) -> bool {
58-
lexical_ord!(le, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
59-
}
60-
#[inline]
61-
fn ge(&self, other: &($($T,)+)) -> bool {
62-
lexical_ord!(ge, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
63-
}
64-
#[inline]
65-
fn gt(&self, other: &($($T,)+)) -> bool {
66-
lexical_ord!(gt, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
49+
maybe_tuple_doc! {
50+
$($T)+ @
51+
#[stable(feature = "rust1", since = "1.0.0")]
52+
impl<$($T:PartialOrd + PartialEq),+> PartialOrd for ($($T,)+)
53+
where
54+
last_type!($($T,)+): ?Sized
55+
{
56+
#[inline]
57+
fn partial_cmp(&self, other: &($($T,)+)) -> Option<Ordering> {
58+
lexical_partial_cmp!($( ${ignore(T)} self.${index()}, other.${index()} ),+)
59+
}
60+
#[inline]
61+
fn lt(&self, other: &($($T,)+)) -> bool {
62+
lexical_ord!(lt, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
63+
}
64+
#[inline]
65+
fn le(&self, other: &($($T,)+)) -> bool {
66+
lexical_ord!(le, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
67+
}
68+
#[inline]
69+
fn ge(&self, other: &($($T,)+)) -> bool {
70+
lexical_ord!(ge, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
71+
}
72+
#[inline]
73+
fn gt(&self, other: &($($T,)+)) -> bool {
74+
lexical_ord!(gt, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
75+
}
6776
}
6877
}
6978

70-
#[stable(feature = "rust1", since = "1.0.0")]
71-
impl<$($T:Ord),+> Ord for ($($T,)+)
72-
where
73-
last_type!($($T,)+): ?Sized
74-
{
75-
#[inline]
76-
fn cmp(&self, other: &($($T,)+)) -> Ordering {
77-
lexical_cmp!($( ${ignore(T)} self.${index()}, other.${index()} ),+)
79+
maybe_tuple_doc! {
80+
$($T)+ @
81+
#[stable(feature = "rust1", since = "1.0.0")]
82+
impl<$($T:Ord),+> Ord for ($($T,)+)
83+
where
84+
last_type!($($T,)+): ?Sized
85+
{
86+
#[inline]
87+
fn cmp(&self, other: &($($T,)+)) -> Ordering {
88+
lexical_cmp!($( ${ignore(T)} self.${index()}, other.${index()} ),+)
89+
}
7890
}
7991
}
8092

81-
#[stable(feature = "rust1", since = "1.0.0")]
82-
impl<$($T:Default),+> Default for ($($T,)+) {
83-
#[inline]
84-
fn default() -> ($($T,)+) {
85-
($({ let x: $T = Default::default(); x},)+)
93+
maybe_tuple_doc! {
94+
$($T)+ @
95+
#[stable(feature = "rust1", since = "1.0.0")]
96+
impl<$($T:Default),+> Default for ($($T,)+) {
97+
#[inline]
98+
fn default() -> ($($T,)+) {
99+
($({ let x: $T = Default::default(); x},)+)
100+
}
86101
}
87102
}
88103
}
89104
}
90105

106+
macro_rules! maybe_tuple_doc {
107+
($a:ident @ #[$meta:meta] $item:item) => {
108+
#[doc = "This trait is implemented for tuples up to twelve items long."]
109+
#[$meta]
110+
$item
111+
};
112+
($a:ident $($rest_a:ident)+ @ #[$meta:meta] $item:item) => {
113+
#[doc(hidden)]
114+
#[$meta]
115+
$item
116+
};
117+
}
118+
91119
// Constructs an expression that performs a lexical ordering using method $rel.
92120
// The values are interleaved, so the macro invocation for
93121
// `(a1, a2, a3) < (b1, b2, b3)` would be `lexical_ord!(lt, a1, b1, a2, b2,

0 commit comments

Comments
 (0)