summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTA Thanh Dinh <tathanhdinh@gmail.com>2018-09-21 17:38:10 +0200
committerTa Thanh Dinh <tathanhdinh@gmail.com>2018-09-22 18:08:16 +0200
commitf9b2007ecd830adec8d98a25bcc2f84c104076ac (patch)
tree8b2fb66056d5cf09a9301f10de1125dd6770caec
parent6fc0a31febb63d77da1a38aa2eea9d10fbea0d0d (diff)
PartialOrd and Ord for enum
-rw-r--r--src/codegen/mod.rs14
-rw-r--r--tests/expectations/tests/ord-enum.rs25
-rw-r--r--tests/headers/ord-enum.h15
3 files changed, 51 insertions, 3 deletions
diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs
index 67d6b6d5..c1d6d53a 100644
--- a/src/codegen/mod.rs
+++ b/src/codegen/mod.rs
@@ -2585,9 +2585,17 @@ impl CodeGenerator for Enum {
}
if !variation.is_const() {
- attrs.push(attributes::derives(
- &["Debug", "Copy", "Clone", "PartialEq", "Eq", "Hash"],
- ));
+ let mut derives = vec!["Debug", "Copy", "Clone", "PartialEq", "Eq", "Hash"];
+
+ if item.can_derive_partialord(ctx) {
+ derives.push("PartialOrd");
+ }
+
+ if item.can_derive_ord(ctx) {
+ derives.push("Ord");
+ }
+
+ attrs.push(attributes::derives(&derives));
}
fn add_constant<'a>(
diff --git a/tests/expectations/tests/ord-enum.rs b/tests/expectations/tests/ord-enum.rs
new file mode 100644
index 00000000..579817bc
--- /dev/null
+++ b/tests/expectations/tests/ord-enum.rs
@@ -0,0 +1,25 @@
+/* automatically generated by rust-bindgen */
+
+#![allow(
+ dead_code,
+ non_snake_case,
+ non_camel_case_types,
+ non_upper_case_globals
+)]
+
+#[repr(i32)]
+#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
+pub enum A {
+ A0 = 0,
+ A1 = 1,
+ A2 = 2,
+ A3 = -1,
+}
+#[repr(i32)]
+#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
+pub enum B {
+ B0 = 1,
+ B1 = 4,
+ B2 = 3,
+ B3 = -1,
+}
diff --git a/tests/headers/ord-enum.h b/tests/headers/ord-enum.h
new file mode 100644
index 00000000..364f711e
--- /dev/null
+++ b/tests/headers/ord-enum.h
@@ -0,0 +1,15 @@
+// bindgen-flags: --rustified-enum * --with-derive-ord
+
+enum A {
+ A0 = 0,
+ A1 = 1,
+ A2 = 2,
+ A3 = A0 - 1,
+};
+
+enum B {
+ B0 = 1,
+ B1 = B0 + 3,
+ B2 = B0 + 2,
+ B3 = B0 - 2,
+}; \ No newline at end of file