Skip to content

Commit f515e71

Browse files
d-e-s-odanielocfb
authored andcommitted
libbpf-cargo: Implement PartialEq for generated Rust "enums"
With the switch from emitting C enums as Rust enums to mapping them to Rust struct instead, we dropped the PartialEq and Eq derives. Add them back to make working with these generated types more convenient. Signed-off-by: Daniel Müller <[email protected]>
1 parent 153c019 commit f515e71

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

libbpf-cargo/src/gen/btf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ impl<'s> GenBtf<'s> {
808808

809809
let mut first_field = None;
810810

811-
writeln!(def, r#"#[derive(Debug, Copy, Clone)]"#)?;
811+
writeln!(def, r#"#[derive(Debug, Copy, Clone, Eq, PartialEq)]"#)?;
812812
writeln!(def, r#"#[repr(transparent)]"#)?;
813813
writeln!(def, r#"pub struct {enum_name}({signed}{repr_size});"#)?;
814814
writeln!(def, "#[allow(non_upper_case_globals)]")?;

libbpf-cargo/src/test.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ fn test_make_workspace() {
377377
.exists());
378378
}
379379

380-
fn build_rust_project_from_bpf_c(bpf_c: &str, rust: &str) {
380+
fn build_rust_project_from_bpf_c_impl(bpf_c: &str, rust: &str, run: bool) {
381381
let (_dir, proj_dir, cargo_toml) = setup_temp_project();
382382

383383
// Add prog dir
@@ -434,7 +434,7 @@ fn build_rust_project_from_bpf_c(bpf_c: &str, rust: &str) {
434434
.expect("failed to write to main.rs");
435435

436436
let status = Command::new("cargo")
437-
.arg("build")
437+
.arg(if run { "run" } else { "build" })
438438
.arg("--quiet")
439439
.arg("--manifest-path")
440440
.arg(cargo_toml.into_os_string())
@@ -444,6 +444,16 @@ fn build_rust_project_from_bpf_c(bpf_c: &str, rust: &str) {
444444
assert!(status.success());
445445
}
446446

447+
fn build_rust_project_from_bpf_c(bpf_c: &str, rust: &str) {
448+
let run = false;
449+
build_rust_project_from_bpf_c_impl(bpf_c, rust, run)
450+
}
451+
452+
fn run_rust_project_from_bpf_c(bpf_c: &str, rust: &str) {
453+
let run = true;
454+
build_rust_project_from_bpf_c_impl(bpf_c, rust, run)
455+
}
456+
447457
#[test]
448458
fn test_skeleton_empty_source() {
449459
let bpf_c = String::new();
@@ -869,12 +879,15 @@ fn test_skeleton_enum_with_same_value_variants() {
869879
use bpf::*;
870880
871881
fn main() {
872-
let _zero1 = types::Foo::Zero;
873-
let _zero2 = types::Foo::ZeroDup;
882+
let zero1 = types::Foo::Zero;
883+
let zero2 = types::Foo::ZeroDup;
884+
885+
assert_eq!(zero1, zero2);
886+
assert_ne!(types::Foo::Zero, types::Foo::One);
874887
}
875888
"#
876889
.to_string();
877-
let () = build_rust_project_from_bpf_c(&bpf_c, &rust);
890+
let () = run_rust_project_from_bpf_c(&bpf_c, &rust);
878891
}
879892

880893
#[test]
@@ -1865,7 +1878,7 @@ struct Bar bar;
18651878
pub struct Bar {
18661879
pub foo: Foo,
18671880
}
1868-
#[derive(Debug, Copy, Clone)]
1881+
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
18691882
#[repr(transparent)]
18701883
pub struct Foo(u32);
18711884
#[allow(non_upper_case_globals)]
@@ -2603,7 +2616,7 @@ struct Foo foo;
26032616
pub struct Foo {
26042617
pub test: __anon_1,
26052618
}
2606-
#[derive(Debug, Copy, Clone)]
2619+
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
26072620
#[repr(transparent)]
26082621
pub struct __anon_1(u32);
26092622
#[allow(non_upper_case_globals)]

0 commit comments

Comments
 (0)