Skip to content

Commit a2bf9f1

Browse files
committed
Auto merge of #1246 - gnzlbg:rustfmt_style, r=gnzlbg
Check style using rustfmt and reformat A couple of recent PRs attempt to reformat the library using rustfmt and run into issues against libc's style checker because rustfmt default settings differ. This PR uses the rustfmt-preview from nightly to check that the library is appropriately formatted, adding a style as similar to libc's style as possible, so that both style checkers work properly simultaneously.
2 parents f9b96ee + 0a5484e commit a2bf9f1

File tree

6 files changed

+92
-77
lines changed

6 files changed

+92
-77
lines changed

.travis.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,11 @@ matrix:
8181
- env: TARGET=wasm32-unknown-unknown
8282
install: rustup target add $TARGET
8383
script: cargo build --no-default-features --target $TARGET --release
84-
84+
- name: "Style"
85+
install: rustup component add rustfmt-preview
86+
script:
87+
- rustc ci/style.rs && ./style src
88+
- cargo fmt --all -- --check
8589
- name: "Shellcheck"
8690
install: true
8791
script:
@@ -102,7 +106,7 @@ script:
102106
export CARGO_TARGET_DIR=`pwd`/target;
103107
sh ci/run.sh $TARGET;
104108
fi
105-
- rustc ci/style.rs && ./style src
109+
106110
env:
107111
global:
108112
secure: "e2/3QjgRN9atOuSHp22TrYG7QVKcYUWY48Hi9b60w+r1+BhPkTseIJLte7WefRhdXtqpjjUJTooKDhnurFOeHaCT+nmBgiv+FPU893sBl4bhesY4m0vgUJVbNZcs6lTImYekWVb+aqjGdgV/XAgCw7c3kPmrZV0MzGDWL64Xaps="

libc-test/build.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,10 @@ use std::env;
77

88
#[cfg(unix)]
99
fn do_cc() {
10-
cc::Build::new()
11-
.file("src/cmsg.c")
12-
.compile("cmsg");
10+
cc::Build::new().file("src/cmsg.c").compile("cmsg");
1311
}
1412
#[cfg(not(unix))]
15-
fn do_cc() {
16-
}
13+
fn do_cc() {}
1714

1815
fn do_ctest() {
1916
let target = env::var("TARGET").unwrap();
@@ -385,7 +382,7 @@ fn do_ctest() {
385382
// Fixup a few types on windows that don't actually exist.
386383
"time64_t" if windows => "__time64_t".to_string(),
387384
"ssize_t" if windows => "SSIZE_T".to_string(),
388-
// windows
385+
// windows
389386
"sighandler_t" if windows && !mingw => "_crt_signal_t".to_string(),
390387
"sighandler_t" if windows && mingw => "__p_sig_fn_t".to_string(),
391388
// OSX calls this something else
@@ -671,7 +668,11 @@ fn do_ctest() {
671668
// MFD_HUGETLB is not available in some older libc versions on the CI builders. On the
672669
// x86_64 and i686 builders it seems to be available for all targets, so at least test
673670
// it there.
674-
"MFD_HUGETLB" if !(x86_64 || i686) || musl || (x86_64 && android)=> true,
671+
"MFD_HUGETLB"
672+
if !(x86_64 || i686) || musl || (x86_64 && android) =>
673+
{
674+
true
675+
}
675676

676677
"DT_FIFO" | "DT_CHR" | "DT_DIR" | "DT_BLK" | "DT_REG"
677678
| "DT_LNK" | "DT_SOCK"

libc-test/test/cmsg.rs

Lines changed: 68 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -6,94 +6,96 @@ extern crate libc;
66
#[cfg(unix)]
77
mod t {
88

9-
use libc::{self, c_uchar, c_uint, c_void, cmsghdr, msghdr};
10-
use std::mem;
9+
use libc::{self, c_uchar, c_uint, c_void, cmsghdr, msghdr};
10+
use std::mem;
1111

12-
extern {
13-
pub fn cmsg_firsthdr(msgh: *const msghdr) -> *mut cmsghdr;
14-
pub fn cmsg_nxthdr(mhdr: *const msghdr,
15-
cmsg: *const cmsghdr) -> *mut cmsghdr;
16-
pub fn cmsg_space(length: c_uint) -> usize;
17-
pub fn cmsg_len(length: c_uint) -> usize;
18-
pub fn cmsg_data(cmsg: *const cmsghdr) -> *mut c_uchar;
19-
}
12+
extern "C" {
13+
pub fn cmsg_firsthdr(msgh: *const msghdr) -> *mut cmsghdr;
14+
pub fn cmsg_nxthdr(
15+
mhdr: *const msghdr,
16+
cmsg: *const cmsghdr,
17+
) -> *mut cmsghdr;
18+
pub fn cmsg_space(length: c_uint) -> usize;
19+
pub fn cmsg_len(length: c_uint) -> usize;
20+
pub fn cmsg_data(cmsg: *const cmsghdr) -> *mut c_uchar;
21+
}
2022

21-
#[test]
22-
fn test_cmsg_data() {
23-
for l in 0..128 {
24-
let pcmsghdr = l as *const cmsghdr;
25-
unsafe {
26-
assert_eq!(libc::CMSG_DATA(pcmsghdr), cmsg_data(pcmsghdr));
23+
#[test]
24+
fn test_cmsg_data() {
25+
for l in 0..128 {
26+
let pcmsghdr = l as *const cmsghdr;
27+
unsafe {
28+
assert_eq!(libc::CMSG_DATA(pcmsghdr), cmsg_data(pcmsghdr));
29+
}
2730
}
2831
}
29-
}
3032

31-
#[test]
32-
fn test_cmsg_firsthdr() {
33-
let mut mhdr: msghdr = unsafe{mem::zeroed()};
34-
mhdr.msg_control = 0xdeadbeef as *mut c_void;
35-
let pmhdr = &mhdr as *const msghdr;
36-
for l in 0..128 {
37-
mhdr.msg_controllen = l;
38-
unsafe {
39-
assert_eq!(libc::CMSG_FIRSTHDR(pmhdr), cmsg_firsthdr(pmhdr));
33+
#[test]
34+
fn test_cmsg_firsthdr() {
35+
let mut mhdr: msghdr = unsafe { mem::zeroed() };
36+
mhdr.msg_control = 0xdeadbeef as *mut c_void;
37+
let pmhdr = &mhdr as *const msghdr;
38+
for l in 0..128 {
39+
mhdr.msg_controllen = l;
40+
unsafe {
41+
assert_eq!(libc::CMSG_FIRSTHDR(pmhdr), cmsg_firsthdr(pmhdr));
42+
}
4043
}
4144
}
42-
}
4345

44-
#[test]
45-
fn test_cmsg_len() {
46-
for l in 0..128 {
47-
unsafe {
48-
assert_eq!(libc::CMSG_LEN(l) as usize, cmsg_len(l));
46+
#[test]
47+
fn test_cmsg_len() {
48+
for l in 0..128 {
49+
unsafe {
50+
assert_eq!(libc::CMSG_LEN(l) as usize, cmsg_len(l));
51+
}
4952
}
5053
}
51-
}
52-
53-
// Skip on sparc64
54-
// https://github.com/rust-lang/libc/issues/1239
55-
#[cfg(not(target_arch = "sparc64"))]
56-
#[test]
57-
fn test_cmsg_nxthdr() {
58-
use std::ptr;
5954

60-
let mut buffer = [0u8; 256];
61-
let mut mhdr: msghdr = unsafe{mem::zeroed()};
62-
let pmhdr = &mhdr as *const msghdr;
63-
for start_ofs in 0..64 {
64-
let pcmsghdr = &mut buffer[start_ofs] as *mut u8 as *mut cmsghdr;
65-
mhdr.msg_control = pcmsghdr as *mut c_void;
66-
mhdr.msg_controllen = (160 - start_ofs) as _;
67-
for cmsg_len in 0..64 {
68-
for next_cmsg_len in 0..32 {
69-
for i in buffer[start_ofs..].iter_mut() {
70-
*i = 0;
71-
}
72-
unsafe {
73-
(*pcmsghdr).cmsg_len = cmsg_len;
74-
let libc_next = libc::CMSG_NXTHDR(pmhdr, pcmsghdr);
75-
let next = cmsg_nxthdr(pmhdr, pcmsghdr);
76-
assert_eq!(libc_next, next);
55+
// Skip on sparc64
56+
// https://github.com/rust-lang/libc/issues/1239
57+
#[cfg(not(target_arch = "sparc64"))]
58+
#[test]
59+
fn test_cmsg_nxthdr() {
60+
use std::ptr;
7761

78-
if libc_next != ptr::null_mut() {
79-
(*libc_next).cmsg_len = next_cmsg_len;
62+
let mut buffer = [0u8; 256];
63+
let mut mhdr: msghdr = unsafe { mem::zeroed() };
64+
let pmhdr = &mhdr as *const msghdr;
65+
for start_ofs in 0..64 {
66+
let pcmsghdr = &mut buffer[start_ofs] as *mut u8 as *mut cmsghdr;
67+
mhdr.msg_control = pcmsghdr as *mut c_void;
68+
mhdr.msg_controllen = (160 - start_ofs) as _;
69+
for cmsg_len in 0..64 {
70+
for next_cmsg_len in 0..32 {
71+
for i in buffer[start_ofs..].iter_mut() {
72+
*i = 0;
73+
}
74+
unsafe {
75+
(*pcmsghdr).cmsg_len = cmsg_len;
8076
let libc_next = libc::CMSG_NXTHDR(pmhdr, pcmsghdr);
8177
let next = cmsg_nxthdr(pmhdr, pcmsghdr);
8278
assert_eq!(libc_next, next);
79+
80+
if libc_next != ptr::null_mut() {
81+
(*libc_next).cmsg_len = next_cmsg_len;
82+
let libc_next = libc::CMSG_NXTHDR(pmhdr, pcmsghdr);
83+
let next = cmsg_nxthdr(pmhdr, pcmsghdr);
84+
assert_eq!(libc_next, next);
85+
}
8386
}
8487
}
8588
}
8689
}
8790
}
88-
}
8991

90-
#[test]
91-
fn test_cmsg_space() {
92-
unsafe {
93-
for l in 0..128 {
94-
assert_eq!(libc::CMSG_SPACE(l) as usize, cmsg_space(l));
92+
#[test]
93+
fn test_cmsg_space() {
94+
unsafe {
95+
for l in 0..128 {
96+
assert_eq!(libc::CMSG_SPACE(l) as usize, cmsg_space(l));
97+
}
9598
}
9699
}
97-
}
98100

99101
}

rustfmt.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
max_width = 79
2+
comment_width = 79
3+
error_on_line_overflow = true

src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,10 @@
155155
#![cfg_attr(feature = "rustc-dep-of-std", feature(no_core))]
156156
#![cfg_attr(feature = "rustc-dep-of-std", no_core)]
157157
#![cfg_attr(feature = "rustc-dep-of-std", allow(warnings))]
158-
#![cfg_attr(not(any(feature = "use_std", feature = "rustc-dep-of-std")), no_std)]
158+
#![cfg_attr(
159+
not(any(feature = "use_std", feature = "rustc-dep-of-std")),
160+
no_std
161+
)]
159162
// Enable lints
160163
#![cfg_attr(feature = "extra_traits", deny(missing_debug_implementations))]
161164
#![deny(missing_copy_implementations)]

src/macros.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ macro_rules! __item {
9090

9191
#[allow(unused_macros)]
9292
macro_rules! align_const {
93-
($($(#[$attr:meta])* pub const $name:ident : $t1:ty = $t2:ident { $($field:tt)* };)*) => ($(
93+
($($(#[$attr:meta])*
94+
pub const $name:ident : $t1:ty
95+
= $t2:ident { $($field:tt)* };)*) => ($(
9496
#[cfg(feature = "align")]
9597
$(#[$attr])*
9698
pub const $name : $t1 = $t2 {

0 commit comments

Comments
 (0)