Skip to content

Commit a265c95

Browse files
authored
Remove more unnecessary libc usage (#1229)
By using `std::ffi` types. A follow-up to #1221 and #1222.
2 parents 75bff7f + 5305b7d commit a265c95

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+77
-99
lines changed

Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

c2rust-bitfields/Cargo.toml

-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,5 @@ categories.workspace = true
1414
[dependencies]
1515
c2rust-bitfields-derive = { version = "0.20.0", path = "../c2rust-bitfields-derive" }
1616

17-
[dev-dependencies]
18-
libc = "0.2"
19-
2017
[features]
2118
no_std = []

c2rust-bitfields/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ And this is enough to build our rust struct:
4343
#[repr(C, align(1))]
4444
#[derive(BitfieldStruct)]
4545
struct Date {
46-
#[bitfield(name = "day", ty = "libc::c_uchar", bits = "0..=4")]
47-
#[bitfield(name = "month", ty = "libc::c_uchar", bits = "5..=8")]
48-
#[bitfield(name = "year", ty = "libc::c_ushort", bits = "9..=23")]
46+
#[bitfield(name = "day", ty = "std::ffi::c_uchar", bits = "0..=4")]
47+
#[bitfield(name = "month", ty = "std::ffi::c_uchar", bits = "5..=8")]
48+
#[bitfield(name = "year", ty = "std::ffi::c_ushort", bits = "9..=23")]
4949
day_month_year: [u8; 3]
5050
}
5151

c2rust-bitfields/c2rust-tests/test_structs.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use c2rust_bitfields::BitfieldStruct;
2-
use libc::{c_double, c_short, c_uchar, c_uint, c_ulong, c_ushort};
2+
use std::ffi::{c_double, c_short, c_uchar, c_uint, c_ulong, c_ushort};
33
use std::mem::{size_of, transmute};
44

55
#[link(name = "test")]
@@ -46,8 +46,8 @@ struct CompactDate {
4646
// Compact combination of d + m
4747
// which can't be accessed via ptr in C anyway
4848
// so we combine the fields into one:
49-
#[bitfield(name = "d", ty = "libc::c_uchar", bits = "0..=4")]
50-
#[bitfield(name = "m", ty = "libc::c_uchar", bits = "8..=11")]
49+
#[bitfield(name = "d", ty = "std::ffi::c_uchar", bits = "0..=4")]
50+
#[bitfield(name = "m", ty = "std::ffi::c_uchar", bits = "8..=11")]
5151
d_m: [u8; 2],
5252
y: u16,
5353
}
@@ -190,8 +190,8 @@ fn test_overflow() {
190190
struct OverlappingByteDate {
191191
// This is also compact, however, the first byte is shared between the two
192192
// bitfields and the month also has a bit in the second byte
193-
#[bitfield(name = "d", ty = "libc::c_ulong", bits = "0..=4")]
194-
#[bitfield(name = "m", ty = "libc::c_ushort", bits = "5..=8")]
193+
#[bitfield(name = "d", ty = "std::ffi::c_ulong", bits = "0..=4")]
194+
#[bitfield(name = "m", ty = "std::ffi::c_ushort", bits = "5..=8")]
195195
d_m: [u8; 2],
196196
y: u16,
197197
#[bitfield(padding)]
@@ -261,11 +261,11 @@ fn test_overlapping_byte_date2() {
261261
#[derive(BitfieldStruct, Copy, Clone)]
262262
struct UnnamedBitfield {
263263
z: f64,
264-
#[bitfield(name = "x", ty = "libc::c_ushort", bits = "0..=4")]
264+
#[bitfield(name = "x", ty = "std::ffi::c_ushort", bits = "0..=4")]
265265
x: [u8; 1],
266266
#[bitfield(padding)]
267267
_pad: [u8; 1],
268-
#[bitfield(name = "y", ty = "libc::c_ushort", bits = "0..=8")]
268+
#[bitfield(name = "y", ty = "std::ffi::c_ushort", bits = "0..=8")]
269269
y: [u8; 2],
270270
}
271271

@@ -301,9 +301,9 @@ fn test_unnamed_bitfield() {
301301
#[repr(C, align(2))]
302302
#[derive(BitfieldStruct, Copy, Clone)]
303303
struct SignedBitfields {
304-
#[bitfield(name = "x", ty = "libc::c_short", bits = "0..=3")]
305-
#[bitfield(name = "y", ty = "libc::c_ushort", bits = "4..=8")]
306-
#[bitfield(name = "z", ty = "libc::c_short", bits = "9..=13")]
304+
#[bitfield(name = "x", ty = "std::ffi::c_short", bits = "0..=3")]
305+
#[bitfield(name = "y", ty = "std::ffi::c_ushort", bits = "4..=8")]
306+
#[bitfield(name = "z", ty = "std::ffi::c_short", bits = "9..=13")]
307307
x_y_z: [u8; 2],
308308
}
309309

@@ -478,8 +478,8 @@ fn test_signed_underflow_overflow() {
478478
#[repr(C, align(2))]
479479
#[derive(BitfieldStruct, Copy, Clone)]
480480
struct SingleBits {
481-
#[bitfield(name = "x", ty = "libc::c_ushort", bits = "0..=0")]
482-
#[bitfield(name = "y", ty = "libc::c_short", bits = "1..=1")]
481+
#[bitfield(name = "x", ty = "std::ffi::c_ushort", bits = "0..=0")]
482+
#[bitfield(name = "y", ty = "std::ffi::c_short", bits = "1..=1")]
483483
x_y: [u8; 1],
484484
#[bitfield(padding)]
485485
_pad: [u8; 1],
@@ -532,9 +532,9 @@ fn test_single_bits() {
532532
#[repr(C, align(1))]
533533
#[derive(BitfieldStruct)]
534534
struct ThreeByteDate {
535-
#[bitfield(name = "day", ty = "libc::c_uchar", bits = "0..=4")]
536-
#[bitfield(name = "month", ty = "libc::c_uchar", bits = "5..=8")]
537-
#[bitfield(name = "year", ty = "libc::c_ushort", bits = "9..=23")]
535+
#[bitfield(name = "day", ty = "std::ffi::c_uchar", bits = "0..=4")]
536+
#[bitfield(name = "month", ty = "std::ffi::c_uchar", bits = "5..=8")]
537+
#[bitfield(name = "year", ty = "std::ffi::c_ushort", bits = "9..=23")]
538538
day_month_year: [u8; 3],
539539
}
540540

tests/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Then create a new `.rs` file with the following skeleton (_does not need to be a
1313
```rust
1414
use crate::c_file::rust_example;
1515
16-
use libc::c_int;
16+
use std::ffi::c_int;
1717
1818
#[link(name = "test")]
1919
extern "C" {

tests/arrays/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7-
libc = "0.2"

tests/arrays/src/test_arrays.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::arrays::rust_entry;
22
use crate::incomplete_arrays::{rust_check_some_ints, rust_entry2, rust_test_sized_array};
33
use crate::variable_arrays::{rust_alloca_arrays, rust_variable_arrays};
4-
use libc::{c_int, c_uint};
4+
use std::ffi::{c_int, c_uint};
55

66
#[link(name = "test")]
77
extern "C" {

tests/asm.aarch64/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7-
libc = "0.2"
87
c2rust-asm-casts = { path = "../../c2rust-asm-casts", version = "0.20.0" }

tests/asm.aarch64/src/test_asm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! extern_crate_c2rust_asm_casts
22
33
use crate::asm::rust_entry;
4-
use libc::{c_int, c_uint};
4+
use std::ffi::{c_int, c_uint};
55

66
#[link(name = "test")]
77
extern "C" {

tests/asm.arm/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7-
libc = "0.2"

tests/asm.arm/src/test_asm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::asm::rust_entry;
2-
use libc::{c_int, c_uint};
2+
use std::ffi::{c_int, c_uint};
33

44
#[link(name = "test")]
55
extern "C" {

tests/asm.x86_64/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7-
libc = "0.2"
87
c2rust-asm-casts = { path = "../../c2rust-asm-casts", version = "0.20.0" }

tests/asm.x86_64/src/test_asm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! extern_crate_c2rust_asm_casts
22
33
use crate::asm::rust_entry;
4-
use libc::{c_int, c_uint};
4+
use std::ffi::{c_int, c_uint};
55

66
#[link(name = "test")]
77
extern "C" {

tests/builtins/src/test_builtins.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::atomics::{rust_atomics_entry, rust_new_atomics};
44
use crate::math::{rust_ffs, rust_ffsl, rust_ffsll, rust_isfinite, rust_isinf_sign, rust_isnan};
55
use crate::mem_x_fns::{rust_assume_aligned, rust_mem_x};
6-
use libc::{c_char, c_double, c_int, c_long, c_longlong, c_uint};
6+
use std::ffi::{c_char, c_double, c_int, c_long, c_longlong, c_uint};
77

88
#[link(name = "test")]
99
extern "C" {

tests/casts/src/test_casts.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::cast_funptr::{rust_entry, rust_get_identity, rust_identity};
22
use crate::casts::rust_cast_stuff;
33

4-
use libc::{c_int, c_uint, c_void};
4+
use std::ffi::{c_int, c_uint, c_void};
55

66
use std::mem::transmute;
77

@@ -46,9 +46,9 @@ pub fn test_identity() {
4646
assert_eq!(rust_id, i);
4747
}
4848

49-
let transmuted_rust_identity: unsafe extern "C" fn(_: libc::c_int) -> libc::c_int =
49+
let transmuted_rust_identity: unsafe extern "C" fn(_: c_int) -> c_int =
5050
unsafe { transmute(rust_get_identity()) };
51-
let transmuted_identity: unsafe extern "C" fn(_: libc::c_int) -> libc::c_int =
51+
let transmuted_identity: unsafe extern "C" fn(_: c_int) -> c_int =
5252
unsafe { transmute(get_identity()) };
5353

5454
for i in 0..10 {

tests/conditionals/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7-
libc = "0.2"

tests/conditionals/src/test_conditionals.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::unused_conditionals::{
55
rust_unused_conditional1, rust_unused_conditional2, rust_unused_conditional3,
66
};
77
use crate::else_if_chain::rust_entry4;
8-
use libc::{c_int, c_uint};
8+
use std::ffi::{c_int, c_uint};
99

1010
#[link(name = "test")]
1111
extern "C" {
@@ -85,4 +85,4 @@ pub fn test_else_if_chain(){
8585
assert_eq!(entry4(20) , rust_entry4(20));
8686
assert_eq!(entry4(30) , rust_entry4(30));
8787
}
88-
}
88+
}

tests/enums/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7-
libc = "0.2"

tests/enums/src/test_enums.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::non_canonical_enum_def::{
99
use crate::top_enum::{rust_entry4, E as otherE};
1010
use crate::enum_compound::rust_entry6;
1111

12-
use libc::{c_int, c_uint};
12+
use std::ffi::{c_int, c_uint};
1313

1414
#[link(name = "test")]
1515
extern "C" {

tests/example/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7-
libc = "0.2"

tests/example/src/test_add.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::add::rust_add;
2-
use libc::c_uint;
2+
use std::ffi::c_uint;
33

44
#[link(name = "test")]
55
extern "C" {

tests/example/src/test_sub.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::sub::rust_sub;
2-
use libc::c_uint;
2+
use std::ffi::c_uint;
33

44
#[link(name = "test")]
55
extern "C" {

tests/floats/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7-
libc = "0.2"

tests/floats/src/test_no_wrapping_neg.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::no_float_wrapping_neg::{rust_double_inc_dec, rust_float_inc_dec, rust_no_wrapping_neg};
2-
use libc::{c_double, c_float};
2+
use std::ffi::{c_double, c_float};
33

44
#[link(name = "test")]
55
extern "C" {

tests/gotos/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7-
libc = "0.2"

tests/gotos/src/test_irreducible.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::irreducible::rust_irreducible;
2-
use libc::c_int;
2+
use std::ffi::c_int;
33

44
#[link(name = "test")]
55
extern "C" {

tests/gotos/src/test_stmt_expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::stmt_expr::rust_stmt_expr_func;
22

3-
use libc::c_int;
3+
use std::ffi::c_int;
44

55
pub fn test_stmt_expr_relooper() {
66
unsafe {

tests/ints/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7-
libc = "0.2"

tests/ints/src/const_test.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11

22
// `const`-ness of variable bindings (both in function arguments and locals) in C corresponds
33
// directly to `mut`-ness of pattern bindings in Rust.
4-
int constant_arguments(int const x /* should be 'x: libc::c_int' */) {
5-
int const y = x + 2; // should be 'y: libc::c_int'
4+
int constant_arguments(int const x /* should be 'x: std::ffi::c_int' */) {
5+
int const y = x + 2; // should be 'y: std::ffi::c_int'
66
return y;
77
}
88

99
// Still just a `const` argument - nothing special here
10-
void constant_pointer(int *const x /* should be 'x: *mut libc::c_int' */) {
10+
void constant_pointer(int *const x /* should be 'x: *mut std::ffi::c_int' */) {
1111
*x += 1;
1212
}
1313

1414
// `const`-ness of pointee type in C corresponds directly to `const`/`mut` pointers in Rust
15-
int pointer_to_constant(int const *x /* should be 'mut x: *const libc::c_int' */) {
15+
int pointer_to_constant(int const *x /* should be 'mut x: *const std::ffi::c_int' */) {
1616
return x[1] + 1;
1717
}
1818

tests/ints/src/implicit_int.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ int identity(x)
1313
void implicit_int(void)
1414
{
1515
// assign to t using the address-of operator
16-
my_fn *t = &identity; // 't: my_fn: fn(libc::c_int) -> libc::c_int'
16+
my_fn *t = &identity; // 't: my_fn: fn(std::ffi::c_int) -> std::ffi::c_int'
1717
// assign to u using function to pointer decay
18-
my_fn *u = identity; // 't: my_fn: fn(libc::c_int) -> libc::c_int'
18+
my_fn *u = identity; // 't: my_fn: fn(std::ffi::c_int) -> std::ffi::c_int'
1919
}
2020

tests/ints/src/test_arithmetic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::arithmetic::rust_entry2;
2-
use libc::{c_int, c_uint};
2+
use std::ffi::{c_int, c_uint};
33

44
#[link(name = "test")]
55
extern "C" {

tests/ints/src/test_compound_assignment.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::compound_assignment::rust_compound_assignment;
22

3-
use libc::{c_int, c_uint};
3+
use std::ffi::{c_int, c_uint};
44

55
#[link(name = "test")]
66
extern "C" {

tests/ints/src/test_const.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::const_test::rust_entry4;
2-
use libc::{c_int, c_uint};
2+
use std::ffi::{c_int, c_uint};
33

44
#[link(name = "test")]
55
extern "C" {

tests/ints/src/test_implicit_ints.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33

44
use crate::implicit_int::{identity as rust_identity, implicit_int as rust_implicit_int};
5-
use libc::{c_int, c_uint};
5+
use std::ffi::{c_int, c_uint};
66

77
extern "C" {
88
fn identity(_: c_int) -> c_int;

tests/ints/src/test_ints.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::chars::rust_multibyte_chars;
22
use crate::size_t::rust_entry;
3-
use libc::{c_int, c_uint};
3+
use std::ffi::{c_int, c_uint};
44

55
#[link(name = "test")]
66
extern "C" {

tests/ints/src/test_sieve_of_eratosthenes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::sieve_of_eratosthenes::rust_sieve_of_eratosthenes;
2-
use libc::c_int;
2+
use std::ffi::c_int;
33

44
#[link(name = "test")]
55
extern "C" {

tests/ints/src/test_volatile.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::volatile::rust_entry3;
2-
use libc::{c_int, c_uint};
2+
use std::ffi::{c_int, c_uint};
33

44
#[link(name = "test")]
55
extern "C" {

tests/items/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7-
libc = "0.2"

tests/items/src/test_linking.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::linking::{rust_l, rust_w};
2-
use libc::c_int;
2+
use std::ffi::c_int;
33

44
#[link(name = "test")]
55
extern "C" {

0 commit comments

Comments
 (0)