Skip to content

Commit 108310d

Browse files
committed
Make use of the crate's prelude to replace individual imports
Automatically apply changes with the following: #!/bin/bash set -eux files=() # Types either defined in this crate or in `core` prelude_types=( c_char c_double c_float c_int c_longlong c_long c_short c_uchar c_uint c_ulonglong c_ulong c_ushort c_void intptr_t size_t ssize_t Clone Copy Option Send Sync ) # Reexports from core prelude_modules=( fmt hash iter mem ) # Everything in the prelude prelude=( "${prelude_types[@]}" "${prelude_modules[@]}" ) # Generate a list of all files excluding `lib.rs` (since the prelude being # defined there makes string matching weird). while IFS= read -r -d '' file; do files+=("$file") done < <(find src -name '*.rs' -not -name '*lib.rs' -not -name '*macros.rs' -not -name 'fixed_width_ints.rs' -print0) for file in "${files[@]}"; do needs_prelude=0 # If the file already has some sort of glob import, skip it if rg --pcre2 -q 'use (crate|super)::(?!prelude).*\*' "$file"; then continue fi # Core types always require the prelude to handle rustc-dep-of-std if rg --pcre2 -q '\b(?<!\.)(Option|Clone|Copy|Send|Sync|fmt|hash|iter|mem)\b' "$file"; then needs_prelude=1 fi # If we use any types that are specified in the prelude then we will import it for ty in "${prelude[@]}"; do # If the type is defined in the current module, we don't need it from the prelude if rg -q "type $ty =" "$file"; then continue fi if rg -qU '((crate|super)::'"$ty"'|use (crate|super)::(\{\n){0,2}.*'"$ty)" "$file"; then needs_prelude=1 fi done # Check if the prelude is needed and does not already exist; if so, add it if [ "$needs_prelude" = "1" ] && ! rg -q 'use crate::prelude::\*' "$file"; then # Split the file into two parts: module-level attributes and rest # Imports will be added after module-level attributes attrs=$(awk '/^#!|^\/\/!/ {found=NR} {lines[NR]=$0} END {for (i=1; i<=found; i++) print lines[i]}' "$file") rest=$(awk '/^#!|^\/\/!/ {found=NR} END {if (found) {for (i=found+1; i<=NR; i++) print lines[i]} else {for (i=1; i<=NR; i++) print lines[i]}} {lines[NR]=$0}' "$file") printf "%s\n" "$attrs" > "$file" printf "\n%s\n\n" "use crate::prelude::*;" >> "$file" printf "%s" "$rest" >> "$file" fi for ty in "${prelude[@]}"; do export TY="$ty" # env for perl to use # Remove simple imports `use crate::ty;` perl -pi -0777 -e 's/use ((crate|super)::)?($ENV{TY});//g' "$file" # Remove the type if it is part of a group import perl -pi -0777 -e 's/(use (crate|super)::\{?(.*|(\n.*){0,2}))\b$ENV{TY}\b,? ?/$1/g' "$file" # Replace pathed `crate::ty` perl -pi -0777 -e 's/(crate|super)::($ENV{TY})\b/$2/g' "$file" done # For some reason, rustfmt doesn't trim leading newlines. Do so manually here. perl -pi -0777 -e 's/\A\n+//' "$file" rustfmt "$file" done ./ci/style.sh (backport <#4161>) (cherry picked from commit f8a018a) Applied by rerunning the script rather than resolving conflicts manually.
1 parent 877b6f6 commit 108310d

File tree

151 files changed

+1537
-1547
lines changed

Some content is hidden

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

151 files changed

+1537
-1547
lines changed

src/fuchsia/aarch64.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use crate::{c_int, c_long, c_uint, c_ulong, c_ulonglong, c_ushort, off_t, size_t};
1+
use crate::off_t;
2+
use crate::prelude::*;
23

34
pub type c_char = u8;
45
pub type __u64 = c_ulonglong;

src/fuchsia/mod.rs

+63-63
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! More functions and definitions can be found in the more specific modules
44
//! according to the platform in question.
55
6-
use crate::c_void;
6+
use crate::prelude::*;
77

88
// PUB_TYPE
99

@@ -1073,8 +1073,8 @@ cfg_if! {
10731073
}
10741074
}
10751075
impl Eq for sysinfo {}
1076-
impl crate::fmt::Debug for sysinfo {
1077-
fn fmt(&self, f: &mut crate::fmt::Formatter) -> crate::fmt::Result {
1076+
impl fmt::Debug for sysinfo {
1077+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
10781078
f.debug_struct("sysinfo")
10791079
.field("uptime", &self.uptime)
10801080
.field("loads", &self.loads)
@@ -1093,8 +1093,8 @@ cfg_if! {
10931093
.finish()
10941094
}
10951095
}
1096-
impl crate::hash::Hash for sysinfo {
1097-
fn hash<H: crate::hash::Hasher>(&self, state: &mut H) {
1096+
impl hash::Hash for sysinfo {
1097+
fn hash<H: hash::Hasher>(&self, state: &mut H) {
10981098
self.uptime.hash(state);
10991099
self.loads.hash(state);
11001100
self.totalram.hash(state);
@@ -1123,16 +1123,16 @@ cfg_if! {
11231123
}
11241124
}
11251125
impl Eq for sockaddr_un {}
1126-
impl crate::fmt::Debug for sockaddr_un {
1127-
fn fmt(&self, f: &mut crate::fmt::Formatter) -> crate::fmt::Result {
1126+
impl fmt::Debug for sockaddr_un {
1127+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
11281128
f.debug_struct("sockaddr_un")
11291129
.field("sun_family", &self.sun_family)
11301130
// FIXME: .field("sun_path", &self.sun_path)
11311131
.finish()
11321132
}
11331133
}
1134-
impl crate::hash::Hash for sockaddr_un {
1135-
fn hash<H: crate::hash::Hasher>(&self, state: &mut H) {
1134+
impl hash::Hash for sockaddr_un {
1135+
fn hash<H: hash::Hasher>(&self, state: &mut H) {
11361136
self.sun_family.hash(state);
11371137
self.sun_path.hash(state);
11381138
}
@@ -1150,17 +1150,17 @@ cfg_if! {
11501150
}
11511151
}
11521152
impl Eq for sockaddr_storage {}
1153-
impl crate::fmt::Debug for sockaddr_storage {
1154-
fn fmt(&self, f: &mut crate::fmt::Formatter) -> crate::fmt::Result {
1153+
impl fmt::Debug for sockaddr_storage {
1154+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
11551155
f.debug_struct("sockaddr_storage")
11561156
.field("ss_family", &self.ss_family)
11571157
.field("__ss_align", &self.__ss_align)
11581158
// FIXME: .field("__ss_pad2", &self.__ss_pad2)
11591159
.finish()
11601160
}
11611161
}
1162-
impl crate::hash::Hash for sockaddr_storage {
1163-
fn hash<H: crate::hash::Hasher>(&self, state: &mut H) {
1162+
impl hash::Hash for sockaddr_storage {
1163+
fn hash<H: hash::Hasher>(&self, state: &mut H) {
11641164
self.ss_family.hash(state);
11651165
self.__ss_align.hash(state);
11661166
self.__ss_pad2.hash(state);
@@ -1196,8 +1196,8 @@ cfg_if! {
11961196
}
11971197
}
11981198
impl Eq for utsname {}
1199-
impl crate::fmt::Debug for utsname {
1200-
fn fmt(&self, f: &mut crate::fmt::Formatter) -> crate::fmt::Result {
1199+
impl fmt::Debug for utsname {
1200+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
12011201
f.debug_struct("utsname")
12021202
// FIXME: .field("sysname", &self.sysname)
12031203
// FIXME: .field("nodename", &self.nodename)
@@ -1207,8 +1207,8 @@ cfg_if! {
12071207
.finish()
12081208
}
12091209
}
1210-
impl crate::hash::Hash for utsname {
1211-
fn hash<H: crate::hash::Hasher>(&self, state: &mut H) {
1210+
impl hash::Hash for utsname {
1211+
fn hash<H: hash::Hasher>(&self, state: &mut H) {
12121212
self.sysname.hash(state);
12131213
self.nodename.hash(state);
12141214
self.release.hash(state);
@@ -1231,8 +1231,8 @@ cfg_if! {
12311231
}
12321232
}
12331233
impl Eq for dirent {}
1234-
impl crate::fmt::Debug for dirent {
1235-
fn fmt(&self, f: &mut crate::fmt::Formatter) -> crate::fmt::Result {
1234+
impl fmt::Debug for dirent {
1235+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
12361236
f.debug_struct("dirent")
12371237
.field("d_ino", &self.d_ino)
12381238
.field("d_off", &self.d_off)
@@ -1242,8 +1242,8 @@ cfg_if! {
12421242
.finish()
12431243
}
12441244
}
1245-
impl crate::hash::Hash for dirent {
1246-
fn hash<H: crate::hash::Hasher>(&self, state: &mut H) {
1245+
impl hash::Hash for dirent {
1246+
fn hash<H: hash::Hasher>(&self, state: &mut H) {
12471247
self.d_ino.hash(state);
12481248
self.d_off.hash(state);
12491249
self.d_reclen.hash(state);
@@ -1266,8 +1266,8 @@ cfg_if! {
12661266
}
12671267
}
12681268
impl Eq for dirent64 {}
1269-
impl crate::fmt::Debug for dirent64 {
1270-
fn fmt(&self, f: &mut crate::fmt::Formatter) -> crate::fmt::Result {
1269+
impl fmt::Debug for dirent64 {
1270+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
12711271
f.debug_struct("dirent64")
12721272
.field("d_ino", &self.d_ino)
12731273
.field("d_off", &self.d_off)
@@ -1277,8 +1277,8 @@ cfg_if! {
12771277
.finish()
12781278
}
12791279
}
1280-
impl crate::hash::Hash for dirent64 {
1281-
fn hash<H: crate::hash::Hasher>(&self, state: &mut H) {
1280+
impl hash::Hash for dirent64 {
1281+
fn hash<H: hash::Hasher>(&self, state: &mut H) {
12821282
self.d_ino.hash(state);
12831283
self.d_off.hash(state);
12841284
self.d_reclen.hash(state);
@@ -1296,8 +1296,8 @@ cfg_if! {
12961296
}
12971297
}
12981298
impl Eq for mq_attr {}
1299-
impl crate::fmt::Debug for mq_attr {
1300-
fn fmt(&self, f: &mut crate::fmt::Formatter) -> crate::fmt::Result {
1299+
impl fmt::Debug for mq_attr {
1300+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
13011301
f.debug_struct("mq_attr")
13021302
.field("mq_flags", &self.mq_flags)
13031303
.field("mq_maxmsg", &self.mq_maxmsg)
@@ -1306,8 +1306,8 @@ cfg_if! {
13061306
.finish()
13071307
}
13081308
}
1309-
impl crate::hash::Hash for mq_attr {
1310-
fn hash<H: crate::hash::Hasher>(&self, state: &mut H) {
1309+
impl hash::Hash for mq_attr {
1310+
fn hash<H: hash::Hasher>(&self, state: &mut H) {
13111311
self.mq_flags.hash(state);
13121312
self.mq_maxmsg.hash(state);
13131313
self.mq_msgsize.hash(state);
@@ -1323,17 +1323,17 @@ cfg_if! {
13231323
}
13241324
}
13251325
impl Eq for sockaddr_nl {}
1326-
impl crate::fmt::Debug for sockaddr_nl {
1327-
fn fmt(&self, f: &mut crate::fmt::Formatter) -> crate::fmt::Result {
1326+
impl fmt::Debug for sockaddr_nl {
1327+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
13281328
f.debug_struct("sockaddr_nl")
13291329
.field("nl_family", &self.nl_family)
13301330
.field("nl_pid", &self.nl_pid)
13311331
.field("nl_groups", &self.nl_groups)
13321332
.finish()
13331333
}
13341334
}
1335-
impl crate::hash::Hash for sockaddr_nl {
1336-
fn hash<H: crate::hash::Hasher>(&self, state: &mut H) {
1335+
impl hash::Hash for sockaddr_nl {
1336+
fn hash<H: hash::Hasher>(&self, state: &mut H) {
13371337
self.nl_family.hash(state);
13381338
self.nl_pid.hash(state);
13391339
self.nl_groups.hash(state);
@@ -1350,8 +1350,8 @@ cfg_if! {
13501350
}
13511351
}
13521352
impl Eq for sigevent {}
1353-
impl crate::fmt::Debug for sigevent {
1354-
fn fmt(&self, f: &mut crate::fmt::Formatter) -> crate::fmt::Result {
1353+
impl fmt::Debug for sigevent {
1354+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
13551355
f.debug_struct("sigevent")
13561356
.field("sigev_value", &self.sigev_value)
13571357
.field("sigev_signo", &self.sigev_signo)
@@ -1361,8 +1361,8 @@ cfg_if! {
13611361
.finish()
13621362
}
13631363
}
1364-
impl crate::hash::Hash for sigevent {
1365-
fn hash<H: crate::hash::Hasher>(&self, state: &mut H) {
1364+
impl hash::Hash for sigevent {
1365+
fn hash<H: hash::Hasher>(&self, state: &mut H) {
13661366
self.sigev_value.hash(state);
13671367
self.sigev_signo.hash(state);
13681368
self.sigev_notify.hash(state);
@@ -1377,15 +1377,15 @@ cfg_if! {
13771377
}
13781378
}
13791379
impl Eq for pthread_cond_t {}
1380-
impl crate::fmt::Debug for pthread_cond_t {
1381-
fn fmt(&self, f: &mut crate::fmt::Formatter) -> crate::fmt::Result {
1380+
impl fmt::Debug for pthread_cond_t {
1381+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
13821382
f.debug_struct("pthread_cond_t")
13831383
// FIXME: .field("size", &self.size)
13841384
.finish()
13851385
}
13861386
}
1387-
impl crate::hash::Hash for pthread_cond_t {
1388-
fn hash<H: crate::hash::Hasher>(&self, state: &mut H) {
1387+
impl hash::Hash for pthread_cond_t {
1388+
fn hash<H: hash::Hasher>(&self, state: &mut H) {
13891389
self.size.hash(state);
13901390
}
13911391
}
@@ -1396,15 +1396,15 @@ cfg_if! {
13961396
}
13971397
}
13981398
impl Eq for pthread_mutex_t {}
1399-
impl crate::fmt::Debug for pthread_mutex_t {
1400-
fn fmt(&self, f: &mut crate::fmt::Formatter) -> crate::fmt::Result {
1399+
impl fmt::Debug for pthread_mutex_t {
1400+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
14011401
f.debug_struct("pthread_mutex_t")
14021402
// FIXME: .field("size", &self.size)
14031403
.finish()
14041404
}
14051405
}
1406-
impl crate::hash::Hash for pthread_mutex_t {
1407-
fn hash<H: crate::hash::Hasher>(&self, state: &mut H) {
1406+
impl hash::Hash for pthread_mutex_t {
1407+
fn hash<H: hash::Hasher>(&self, state: &mut H) {
14081408
self.size.hash(state);
14091409
}
14101410
}
@@ -1415,15 +1415,15 @@ cfg_if! {
14151415
}
14161416
}
14171417
impl Eq for pthread_rwlock_t {}
1418-
impl crate::fmt::Debug for pthread_rwlock_t {
1419-
fn fmt(&self, f: &mut crate::fmt::Formatter) -> crate::fmt::Result {
1418+
impl fmt::Debug for pthread_rwlock_t {
1419+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
14201420
f.debug_struct("pthread_rwlock_t")
14211421
// FIXME: .field("size", &self.size)
14221422
.finish()
14231423
}
14241424
}
1425-
impl crate::hash::Hash for pthread_rwlock_t {
1426-
fn hash<H: crate::hash::Hasher>(&self, state: &mut H) {
1425+
impl hash::Hash for pthread_rwlock_t {
1426+
fn hash<H: hash::Hasher>(&self, state: &mut H) {
14271427
self.size.hash(state);
14281428
}
14291429
}
@@ -3372,20 +3372,20 @@ cfg_if! {
33723372
f! {
33733373
pub fn FD_CLR(fd: c_int, set: *mut fd_set) -> () {
33743374
let fd = fd as usize;
3375-
let size = crate::mem::size_of_val(&(*set).fds_bits[0]) * 8;
3375+
let size = mem::size_of_val(&(*set).fds_bits[0]) * 8;
33763376
(*set).fds_bits[fd / size] &= !(1 << (fd % size));
33773377
return;
33783378
}
33793379

33803380
pub fn FD_ISSET(fd: c_int, set: *const fd_set) -> bool {
33813381
let fd = fd as usize;
3382-
let size = crate::mem::size_of_val(&(*set).fds_bits[0]) * 8;
3382+
let size = mem::size_of_val(&(*set).fds_bits[0]) * 8;
33833383
return ((*set).fds_bits[fd / size] & (1 << (fd % size))) != 0;
33843384
}
33853385

33863386
pub fn FD_SET(fd: c_int, set: *mut fd_set) -> () {
33873387
let fd = fd as usize;
3388-
let size = crate::mem::size_of_val(&(*set).fds_bits[0]) * 8;
3388+
let size = mem::size_of_val(&(*set).fds_bits[0]) * 8;
33893389
(*set).fds_bits[fd / size] |= 1 << (fd % size);
33903390
return;
33913391
}
@@ -3403,21 +3403,21 @@ f! {
34033403
}
34043404

34053405
pub fn CPU_SET(cpu: usize, cpuset: &mut cpu_set_t) -> () {
3406-
let size_in_bits = 8 * crate::mem::size_of_val(&cpuset.bits[0]); // 32, 64 etc
3406+
let size_in_bits = 8 * mem::size_of_val(&cpuset.bits[0]); // 32, 64 etc
34073407
let (idx, offset) = (cpu / size_in_bits, cpu % size_in_bits);
34083408
cpuset.bits[idx] |= 1 << offset;
34093409
()
34103410
}
34113411

34123412
pub fn CPU_CLR(cpu: usize, cpuset: &mut cpu_set_t) -> () {
3413-
let size_in_bits = 8 * crate::mem::size_of_val(&cpuset.bits[0]); // 32, 64 etc
3413+
let size_in_bits = 8 * mem::size_of_val(&cpuset.bits[0]); // 32, 64 etc
34143414
let (idx, offset) = (cpu / size_in_bits, cpu % size_in_bits);
34153415
cpuset.bits[idx] &= !(1 << offset);
34163416
()
34173417
}
34183418

34193419
pub fn CPU_ISSET(cpu: usize, cpuset: &cpu_set_t) -> bool {
3420-
let size_in_bits = 8 * crate::mem::size_of_val(&cpuset.bits[0]);
3420+
let size_in_bits = 8 * mem::size_of_val(&cpuset.bits[0]);
34213421
let (idx, offset) = (cpu / size_in_bits, cpu % size_in_bits);
34223422
0 != (cpuset.bits[idx] & (1 << offset))
34233423
}
@@ -3445,33 +3445,33 @@ f! {
34453445
}
34463446

34473447
pub fn CMSG_NXTHDR(mhdr: *const msghdr, cmsg: *const cmsghdr) -> *mut cmsghdr {
3448-
if ((*cmsg).cmsg_len as size_t) < crate::mem::size_of::<cmsghdr>() {
3448+
if ((*cmsg).cmsg_len as size_t) < mem::size_of::<cmsghdr>() {
34493449
0 as *mut cmsghdr
3450-
} else if __CMSG_NEXT(cmsg).add(crate::mem::size_of::<cmsghdr>()) >= __MHDR_END(mhdr) {
3450+
} else if __CMSG_NEXT(cmsg).add(mem::size_of::<cmsghdr>()) >= __MHDR_END(mhdr) {
34513451
0 as *mut cmsghdr
34523452
} else {
34533453
__CMSG_NEXT(cmsg).cast()
34543454
}
34553455
}
34563456

34573457
pub fn CMSG_FIRSTHDR(mhdr: *const msghdr) -> *mut cmsghdr {
3458-
if (*mhdr).msg_controllen as size_t >= crate::mem::size_of::<cmsghdr>() {
3458+
if (*mhdr).msg_controllen as size_t >= mem::size_of::<cmsghdr>() {
34593459
(*mhdr).msg_control.cast()
34603460
} else {
34613461
0 as *mut cmsghdr
34623462
}
34633463
}
34643464

34653465
pub {const} fn CMSG_ALIGN(len: size_t) -> size_t {
3466-
(len + crate::mem::size_of::<size_t>() - 1) & !(crate::mem::size_of::<size_t>() - 1)
3466+
(len + mem::size_of::<size_t>() - 1) & !(mem::size_of::<size_t>() - 1)
34673467
}
34683468

34693469
pub {const} fn CMSG_SPACE(len: c_uint) -> c_uint {
3470-
(CMSG_ALIGN(len as size_t) + CMSG_ALIGN(crate::mem::size_of::<cmsghdr>())) as c_uint
3470+
(CMSG_ALIGN(len as size_t) + CMSG_ALIGN(mem::size_of::<cmsghdr>())) as c_uint
34713471
}
34723472

34733473
pub {const} fn CMSG_LEN(len: c_uint) -> c_uint {
3474-
(CMSG_ALIGN(crate::mem::size_of::<cmsghdr>()) + len as size_t) as c_uint
3474+
(CMSG_ALIGN(mem::size_of::<cmsghdr>()) + len as size_t) as c_uint
34753475
}
34763476
}
34773477

@@ -3525,8 +3525,8 @@ safe_f! {
35253525
}
35263526

35273527
fn __CMSG_LEN(cmsg: *const cmsghdr) -> ssize_t {
3528-
((unsafe { (*cmsg).cmsg_len as size_t } + crate::mem::size_of::<c_long>() - 1)
3529-
& !(crate::mem::size_of::<c_long>() - 1)) as ssize_t
3528+
((unsafe { (*cmsg).cmsg_len as size_t } + mem::size_of::<c_long>() - 1)
3529+
& !(mem::size_of::<c_long>() - 1)) as ssize_t
35303530
}
35313531

35323532
fn __CMSG_NEXT(cmsg: *const cmsghdr) -> *mut c_uchar {

src/fuchsia/riscv64.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use crate::{c_int, c_long, c_ulong, c_ulonglong, c_ushort, off_t};
1+
use crate::off_t;
2+
use crate::prelude::*;
23

34
// From psABI Calling Convention for RV64
45
pub type c_char = u8;

0 commit comments

Comments
 (0)