diff --git a/libbpf-cargo/src/gen/btf.rs b/libbpf-cargo/src/gen/btf.rs index 4acdbf3c..a446c6c0 100644 --- a/libbpf-cargo/src/gen/btf.rs +++ b/libbpf-cargo/src/gen/btf.rs @@ -797,6 +797,8 @@ impl<'s> GenBtf<'s> { _ => bail!("Invalid enum size: {}", t.size()), }; + let enum_name = self.anon_types.type_name_or_anon(&t); + let mut signed = "u"; for value in t.iter() { if value.value < 0 { @@ -805,30 +807,22 @@ impl<'s> GenBtf<'s> { } } - writeln!( - def, - r#"#[derive(Debug, Copy, Clone, Default, PartialEq, Eq)]"# - )?; - writeln!(def, r#"#[repr({signed}{repr_size})]"#)?; - writeln!( - def, - r#"pub enum {name} {{"#, - name = self.anon_types.type_name_or_anon(&t), - )?; + writeln!(def, r#"#[derive(Debug, Copy, Clone, Default)]"#)?; + writeln!(def, r#"pub struct {enum_name}({signed}{repr_size});"#,)?; - for (i, value) in t.iter().enumerate() { - if i == 0 { - writeln!(def, r#" #[default]"#)?; - } + writeln!(def, r#"impl {enum_name} {{"#,)?; + + for value in t.iter() { + writeln!(def, "#[allow(non_upper_case_globals)]")?; writeln!( def, - r#" {name} = {value},"#, + r#" pub const {name}: {enum_name} = {enum_name}({value});"#, name = value.name.unwrap().to_string_lossy(), value = value.value, )?; } - writeln!(def, "}}")?; + writeln!(def, r#"}}"#,)?; Ok(()) } diff --git a/libbpf-cargo/src/test.rs b/libbpf-cargo/src/test.rs index 6d44271f..84d36741 100644 --- a/libbpf-cargo/src/test.rs +++ b/libbpf-cargo/src/test.rs @@ -1824,13 +1824,12 @@ enum Foo foo; "#; let expected_output = r#" -#[derive(Debug, Copy, Clone, Default, PartialEq, Eq)] -#[repr(u32)] -pub enum Foo { - #[default] - Zero = 0, - One = 1, - seven = 7, +#[derive(Debug, Copy, Clone, Default)] +pub struct Foo(u32); +impl Foo { + pub const Zero: Foo = Foo(0); + pub const One: Foo = Foo(1); + pub const seven: Foo = Foo(7); } "#; @@ -2565,13 +2564,11 @@ impl Default for Foo { } } } -#[derive(Debug, Copy, Clone, Default, PartialEq, Eq)] -#[repr(u32)] -pub enum __anon_1 { - #[default] - FOO = 1, -} -"#; +#[derive(Debug, Copy, Clone, Default)] +pub struct __anon_1(u32); +impl __anon_1 { + pub const FOO: __anon_1 = __anon_1(1); +}"#; let mmap = build_btf_mmap(prog_text); let btf = btf_from_mmap(&mmap);