about summary refs log tree commit diff
path: root/src/libsyntax_ext/deriving/generic
diff options
context:
space:
mode:
authorUlrik Sverdrup <bluss@users.noreply.github.com>2016-03-01 02:27:27 +0100
committerUlrik Sverdrup <bluss@users.noreply.github.com>2016-03-01 02:27:27 +0100
commitedcc02bfee262ce8bc3f087d9793ce68d73b1a40 (patch)
tree209c1bbe422feed715024758bcbb7176227c549b /src/libsyntax_ext/deriving/generic
parent57e0a7e5d8872c8fcea47fc20239b8921bda2576 (diff)
downloadrust-edcc02bfee262ce8bc3f087d9793ce68d73b1a40.tar.gz
rust-edcc02bfee262ce8bc3f087d9793ce68d73b1a40.zip
derive: Emit only PartialOrd::partial_cmp for simple enums
Using the same logic as for `PartialEq`, when possible define only
`partial_cmp` and leave `lt, le, gt, ge` to their default
implementations. This works well for c-like enums.
Diffstat (limited to 'src/libsyntax_ext/deriving/generic')
-rw-r--r--src/libsyntax_ext/deriving/generic/mod.rs18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/libsyntax_ext/deriving/generic/mod.rs b/src/libsyntax_ext/deriving/generic/mod.rs
index c0237a5d29a..e8954b1a2fc 100644
--- a/src/libsyntax_ext/deriving/generic/mod.rs
+++ b/src/libsyntax_ext/deriving/generic/mod.rs
@@ -1638,3 +1638,21 @@ pub fn cs_same_method<F>(f: F,
         }
     }
 }
+
+/// Return true if the type has no value fields
+/// (for an enum, no variant has any fields)
+pub fn is_type_without_fields(item: &Annotatable) -> bool {
+    if let Annotatable::Item(ref item) = *item {
+        match item.node {
+            ast::ItemKind::Enum(ref enum_def, _) => {
+                enum_def.variants.iter().all(|v| v.node.data.fields().is_empty())
+            }
+            ast::ItemKind::Struct(ref variant_data, _) => {
+                variant_data.fields().is_empty()
+            }
+            _ => false
+        }
+    } else {
+        false
+    }
+}