Skip to content

Commit 5b9ad8d

Browse files
d-e-s-odanielocfb
authored andcommitted
libbpf-cargo: Stop treating C enums as "unsafe"
In the past C enums were mapped to Rust enums in the generated skeletons. Because Rust enums enums have stricter requirements than C ones, we have to wrap them in MaybeUninit (see 87929ff ("libbpf-cargo: Wrap generated "unsafe" types in MaybeUninit")). With the switch to using regular constants for representing these C enums in Rust as done in commit e241983 ("libbpf-cargo: Generate C enums as custom types with const fields"), we no longer have to treat them as "unsafe" and can remove the MaybeUninit wrapper. Signed-off-by: Daniel Müller <deso@posteo.net>
1 parent 19db643 commit 5b9ad8d

File tree

4 files changed

+10
-28
lines changed

4 files changed

+10
-28
lines changed

examples/capable/src/main.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,7 @@ fn main() -> Result<()> {
199199
.tool_config
200200
.verbose
201201
.write(opts.verbose);
202-
open_skel
203-
.maps
204-
.rodata_data
205-
.tool_config
206-
.unique_type
207-
.write(opts.unique_type);
202+
open_skel.maps.rodata_data.tool_config.unique_type = opts.unique_type;
208203

209204
let mut skel = open_skel.load()?;
210205
skel.attach()?;

libbpf-cargo/CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Unreleased
22
----------
33
- Represent C enums with custom types and const fields
4+
- Adjusted Rust correspondents in generated skeletons to no longer be
5+
wrapped in `MaybeUninit`
46

57

68
0.24.7
@@ -74,7 +76,7 @@ Unreleased
7476
- Fixed potential naming issues by escaping reserved keywords used in
7577
identifiers
7678
- Fixed potential unsoundness issues in generated skeletons by wrapping "unsafe"
77-
type in `MaybeUninit`
79+
types in `MaybeUninit`
7880
- Added pointer based ("raw") access to datasec type to generated skeletons
7981
- Added better handling for bitfields to code generation logic
8082
- Updated `libbpf-sys` dependency to `1.4.0`

libbpf-cargo/src/gen/btf.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ fn is_unsafe(ty: BtfType<'_>) -> bool {
4040

4141
btf_type_match!(match ty {
4242
BtfKind::Int(t) => matches!(t.encoding, types::IntEncoding::Bool),
43-
BtfKind::Enum | BtfKind::Enum64 => true,
4443
_ => false,
4544
})
4645
}

libbpf-cargo/src/test.rs

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,17 +1860,10 @@ struct Bar bar;
18601860
"#;
18611861

18621862
let expected_output = r#"
1863-
#[derive(Debug, Copy, Clone)]
1863+
#[derive(Debug, Default, Copy, Clone)]
18641864
#[repr(C)]
18651865
pub struct Bar {
1866-
pub foo: std::mem::MaybeUninit<Foo>,
1867-
}
1868-
impl Default for Bar {
1869-
fn default() -> Self {
1870-
Self {
1871-
foo: std::mem::MaybeUninit::new(Foo::default()),
1872-
}
1873-
}
1866+
pub foo: Foo,
18741867
}
18751868
#[derive(Debug, Copy, Clone)]
18761869
#[repr(transparent)]
@@ -1885,7 +1878,6 @@ impl Foo {
18851878
impl Default for Foo {
18861879
fn default() -> Self { Foo::Zero }
18871880
}
1888-
18891881
"#;
18901882

18911883
let mmap = build_btf_mmap(prog_text);
@@ -2606,17 +2598,10 @@ struct Foo foo;
26062598
"#;
26072599

26082600
let expected_output = r#"
2609-
#[derive(Debug, Copy, Clone)]
2601+
#[derive(Debug, Default, Copy, Clone)]
26102602
#[repr(C)]
26112603
pub struct Foo {
2612-
pub test: std::mem::MaybeUninit<__anon_1>,
2613-
}
2614-
impl Default for Foo {
2615-
fn default() -> Self {
2616-
Self {
2617-
test: std::mem::MaybeUninit::new(__anon_1::default()),
2618-
}
2619-
}
2604+
pub test: __anon_1,
26202605
}
26212606
#[derive(Debug, Copy, Clone)]
26222607
#[repr(transparent)]
@@ -2627,7 +2612,8 @@ impl __anon_1 {
26272612
}
26282613
impl Default for __anon_1 {
26292614
fn default() -> Self { __anon_1::FOO }
2630-
}"#;
2615+
}
2616+
"#;
26312617

26322618
let mmap = build_btf_mmap(prog_text);
26332619
let btf = btf_from_mmap(&mmap);

0 commit comments

Comments
 (0)