1 // See src/libstd/primitive_docs.rs for documentation.
4 use crate::cmp::Ordering::*;
6 // macro for implementing n-ary tuple functions and operations
7 macro_rules! tuple_impls {
10 $(($idx:tt) -> $T:ident)+
14 #[stable(feature = "rust1", since = "1.0.0")]
15 impl<$($T:PartialEq),+> PartialEq for ($($T,)+) where last_type!($($T,)+): ?Sized {
17 fn eq(&self, other: &($($T,)+)) -> bool {
18 $(self.$idx == other.$idx)&&+
21 fn ne(&self, other: &($($T,)+)) -> bool {
22 $(self.$idx != other.$idx)||+
26 #[stable(feature = "rust1", since = "1.0.0")]
27 impl<$($T:Eq),+> Eq for ($($T,)+) where last_type!($($T,)+): ?Sized {}
29 #[stable(feature = "rust1", since = "1.0.0")]
30 impl<$($T:PartialOrd + PartialEq),+> PartialOrd for ($($T,)+)
31 where last_type!($($T,)+): ?Sized {
33 fn partial_cmp(&self, other: &($($T,)+)) -> Option<Ordering> {
34 lexical_partial_cmp!($(self.$idx, other.$idx),+)
37 fn lt(&self, other: &($($T,)+)) -> bool {
38 lexical_ord!(lt, $(self.$idx, other.$idx),+)
41 fn le(&self, other: &($($T,)+)) -> bool {
42 lexical_ord!(le, $(self.$idx, other.$idx),+)
45 fn ge(&self, other: &($($T,)+)) -> bool {
46 lexical_ord!(ge, $(self.$idx, other.$idx),+)
49 fn gt(&self, other: &($($T,)+)) -> bool {
50 lexical_ord!(gt, $(self.$idx, other.$idx),+)
54 #[stable(feature = "rust1", since = "1.0.0")]
55 impl<$($T:Ord),+> Ord for ($($T,)+) where last_type!($($T,)+): ?Sized {
57 fn cmp(&self, other: &($($T,)+)) -> Ordering {
58 lexical_cmp!($(self.$idx, other.$idx),+)
62 #[stable(feature = "rust1", since = "1.0.0")]
63 impl<$($T:Default),+> Default for ($($T,)+) {
65 fn default() -> ($($T,)+) {
66 ($({ let x: $T = Default::default(); x},)+)
73 // Constructs an expression that performs a lexical ordering using method $rel.
74 // The values are interleaved, so the macro invocation for
75 // `(a1, a2, a3) < (b1, b2, b3)` would be `lexical_ord!(lt, a1, b1, a2, b2,
76 // a3, b3)` (and similarly for `lexical_cmp`)
77 macro_rules! lexical_ord {
78 ($rel: ident, $a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
79 if $a != $b { lexical_ord!($rel, $a, $b) }
80 else { lexical_ord!($rel, $($rest_a, $rest_b),+) }
82 ($rel: ident, $a:expr, $b:expr) => { ($a) . $rel (& $b) };
85 macro_rules! lexical_partial_cmp {
86 ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
87 match ($a).partial_cmp(&$b) {
88 Some(Equal) => lexical_partial_cmp!($($rest_a, $rest_b),+),
92 ($a:expr, $b:expr) => { ($a).partial_cmp(&$b) };
95 macro_rules! lexical_cmp {
96 ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
98 Equal => lexical_cmp!($($rest_a, $rest_b),+),
102 ($a:expr, $b:expr) => { ($a).cmp(&$b) };
105 macro_rules! last_type {
106 ($a:ident,) => { $a };
107 ($a:ident, $($rest_a:ident,)+) => { last_type!($($rest_a,)+) };