Repository: libbpf/libbpf-rs (language: rust)

Issue:
bool in bpf code becomes u8 in generated skeleton code
Hi,

The current skel generation does this. I was looking up how to convert the bool properly to u8 when I found [this](https://stackoverflow.com/a/47705543/2277013) . Any thoughts on why the skel generation represents bool in C as u8 in Rust? Also, how would you convert rust bool to rust u8 so that the bool value does get passed to bpf code?

Thanks in advance


Discussion / hints:
I got this right now
```
fn bool_2_u8(value: bool) -> u8 {
    if value {1} else {0}
}
```
or is something that uses transmute, like [this](https://stackoverflow.com/q/36924530/2277013), to be used?

Fix patch:
diff --git a/libbpf-cargo/src/btf/btf.rs b/libbpf-cargo/src/btf/btf.rs
index c709fd7c..32224abc 100644
--- a/libbpf-cargo/src/btf/btf.rs
+++ b/libbpf-cargo/src/btf/btf.rs
@@ -197,10 +197,13 @@ impl<'a> Btf<'a> {
                     _ => bail!("Invalid integer width"),
                 };
 
-                if t.encoding == btf::BtfIntEncoding::Signed {
-                    format!("i{}", width)
-                } else {
-                    format!("u{}", width)
+                match t.encoding {
+                    btf::BtfIntEncoding::Signed => format!("i{}", width),
+                    btf::BtfIntEncoding::Bool => {
+                        assert!(t.bits as usize == (std::mem::size_of::<bool>() * 8));
+                        format!("bool")
+                    }
+                    btf::BtfIntEncoding::Char | btf::BtfIntEncoding::None => format!("u{}", width),
                 }
             }
             BtfType::Ptr(t) => {


Test patch:
diff --git a/libbpf-cargo/src/test.rs b/libbpf-cargo/src/test.rs
index 178434f2..e166cf52 100644
--- a/libbpf-cargo/src/test.rs
+++ b/libbpf-cargo/src/test.rs
@@ -1844,3 +1844,39 @@ impl Default for __anon_1 {
 
     assert_definition(&btf, struct_foo, expected_output);
 }
+
+#[test]
+fn test_btf_dump_definition_int_encodings() {
+    let prog_text = r#"
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+
+struct Foo {
+    s32 a;
+    u16 b;
+    s16 c;
+    bool d;
+    char e;
+};
+struct Foo foo;
+"#;
+
+    let expected_output = r#"
+#[derive(Debug, Default, Copy, Clone)]
+#[repr(C)]
+pub struct Foo {
+    pub a: i32,
+    pub b: u16,
+    pub c: i16,
+    pub d: bool,
+    pub e: i8,
+}
+"#;
+
+    let btf = build_btf_prog(prog_text);
+
+    // Find the struct
+    let struct_foo = find_type_in_btf!(btf, Struct, "Foo");
+
+    assert_definition(&btf, struct_foo, expected_output);
+}