Open
Description
opened on Nov 26, 2017
A bitfield with a signed integer works as expected (including overflow) for all functionality except for the Rust getter which in all cases returned an unsigned value.
Input C/C++ Header
typedef struct {
unsigned int a: 1;
unsigned int b: 1;
int c: 2;
} StructWithBitfields;
StructWithBitfields create_bitfield();
void walk_bitfield();
void print_bitfield(StructWithBitfields bfield);
C Static functions
#include <stdio.h>
#include "../wrapper.h"
StructWithBitfields create_bitfield() {
StructWithBitfields bfield;
bfield.a = 0;
bfield.b = 0;
bfield.c = 0;
return bfield;
}
void walk_bitfield() {
StructWithBitfields bfield = create_bitfield();
print_bitfield(bfield);
bfield.c = 1;
print_bitfield(bfield);
bfield.c = 2;
print_bitfield(bfield);
bfield.c = 3;
print_bitfield(bfield);
bfield.c = 4;
print_bitfield(bfield);
bfield.c = -1;
print_bitfield(bfield);
bfield.c = -2;
print_bitfield(bfield);
bfield.c = -3;
print_bitfield(bfield);
bfield.c = -4;
print_bitfield(bfield);
}
void print_bitfield(StructWithBitfields bfield) {
printf("StructWithBitfields: a:%d, b:%d, c:%d\n", bfield.a, bfield.b, bfield.c);
}
Bindgen Invocation
extern crate bindgen;
extern crate gcc;
use std::env;
use std::path::PathBuf;
fn main() {
gcc::Config::new()
.file("static/bitfields.c")
.compile("bitfield-static-functions.a");
let bindings = bindgen::Builder::default()
.header("wrapper.h")
.derive_default(true)
.generate()
// Unwrap the Result and panic on failure.
.expect("Unable to generate bindings");
// Write the bindings to the $OUT_DIR/bindings.rs file.
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}
Main Source
extern crate bitfields_sys;
use std::default::Default;
use bitfields_sys::{StructWithBitfields,create_bitfield, walk_bitfield, print_bitfield};
fn main() {
println!("Print C walk");
unsafe{walk_bitfield()};
println!("Print Rust walk");
let mut bfield = StructWithBitfields{
_bitfield_1: StructWithBitfields::new_bitfield_1(0,0,0),
..Default::default()
};
println!("c set to {}", bfield.c());
bfield.set_c(1);
println!("c set to {}", bfield.c());
bfield.set_c(2);
println!("c set to {}", bfield.c());
bfield.set_c(3);
println!("c set to {}", bfield.c());
bfield.set_c(4);
println!("c set to {}", bfield.c());
bfield.set_c(-1);
println!("c set to {}", bfield.c());
bfield.set_c(-2);
println!("c set to {}", bfield.c());
bfield.set_c(-3);
println!("c set to {}", bfield.c());
bfield.set_c(-4);
println!("c set to {}", bfield.c());
}
Actual Results
Print Rust walk
c set to 0
c set to 1
c set to 2
c set to 3
c set to 0
c set to 3
c set to 2
c set to 1
c set to 0
Expected Results (Just the c field).
Print C walk
StructWithBitfields: a:0, b:0, c:0
StructWithBitfields: a:0, b:0, c:1
StructWithBitfields: a:0, b:0, c:-2
StructWithBitfields: a:0, b:0, c:-1
StructWithBitfields: a:0, b:0, c:0
StructWithBitfields: a:0, b:0, c:-1
StructWithBitfields: a:0, b:0, c:-2
StructWithBitfields: a:0, b:0, c:1
StructWithBitfields: a:0, b:0, c:0
Activity