rust-lang / rust-lang/rust-bindgen

Replaces annotations don't work with base types

Open
#1,144 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug E-less-easy help wanted
Dominant language
Rust
Stars
5.3k
Forks
829
Avg merge
1d 1h
Merged PRs (30d)
15

Description

I use bindgen on the data structure shown, and then I'm left with signed char arrays that require a transmute to treat as bytes. for example:

fn hdr_set_name(hdr: &mut dmi::dm_ioctl, name: &str) -> () {
    let name_dest: &mut [u8; DM_NAME_LEN] = unsafe { transmute(&mut hdr.name) };
    let bytes = name.as_bytes();
    name_dest[..bytes.len()].clone_from_slice(bytes);
}

It would be great if bindgen could optionally translate these as unsigned chars instead of signed chars (the default). Or, maybe I'm going about this all wrong??? 😃

Input C/C++ Header
#define DM_NAME_LEN 128
#define DM_UUID_LEN 129
struct dm_ioctl {
	char name[DM_NAME_LEN];	/* device name */
	char uuid[DM_UUID_LEN];	/* unique identifier for
				 * the block device */
	char data[7];		/* padding or data */
};
Bindgen Invocation
    let bindings = bindgen::Builder::default()
        .header("wrapper.h")
        .derive_default(true)
        .impl_debug(true)
        .generate()
        .expect("Unable to generate bindings");
Actual Results
# [ repr ( C ) ]
# [ derive ( Copy , Clone ) ]
pub struct dm_ioctl {
    pub name: [::std::os::raw::c_char; 128usize],
    pub uuid: [::std::os::raw::c_char; 129usize],
    pub data: [::std::os::raw::c_char; 7usize],
}
Desired results
# [ repr ( C ) ]
# [ derive ( Copy , Clone ) ]
pub struct dm_ioctl {
    pub name: [::std::os::raw::c_uchar; 128usize],
    pub uuid: [::std::os::raw::c_uchar; 129usize],
    pub data: [::std::os::raw::c_uchar; 7usize],
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue with wrapper.h and the bindgen::Builder invocation shown, then compare the generated dm_ioctl fields with the desired c_uchar output. Done means bindgen provides a documented way to opt into unsigned-char arrays without requiring transmute.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, cpp, rust
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.