summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors-servo <lbergstrom+bors@mozilla.com>2018-09-24 04:55:54 -0400
committerGitHub <noreply@github.com>2018-09-24 04:55:54 -0400
commit65f6779fd8bd677a9ab8077f0006787ed2094408 (patch)
tree6dc12525a5fcc8a8ab4f0a8e49e809a090d3782c
parentdcb9921446ddd38cdd2bc5d02911b65ca3dcf93b (diff)
parentf9b2007ecd830adec8d98a25bcc2f84c104076ac (diff)
Auto merge of #1396 - tathanhdinh:master, r=emilio
PartialOrd and Ord for enum Hello all, This PR tries to fix #1395. Many thanks for any comment.
-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 1bff1e26..7e183ad5 100644
--- a/src/codegen/mod.rs
+++ b/src/codegen/mod.rs
@@ -2590,9 +2590,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