Skip to content

Commit

Permalink
Make use of the crate's prelude to replace individual imports
Browse files Browse the repository at this point in the history
This step was performed automatically with the following:

    #!/bin/bash

    set -eux

    files=()
    prelude_types=(
        c_char
        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
    )

    # 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' -print0)

    for file in "${files[@]}"; do
        # If the file already has some sort of glob import, skip it
        if rg --pcre2 -q 'use crate::(?!prelude).*\*' "$file"; then
            continue
        fi

        # Core types always require the prelude to handle rustc-dep-of-std
        if rg -qU "(Option|Clone|Copy)" "$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_types[@]}"; do
            if rg -qU "(crate::$ty|use crate::(\{\n){,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
            perl -pi -0777 -e 's/(use crate::)/use crate::prelude::*;\n$1/' "$file"
        fi

        for ty in "${prelude_types[@]}"; do
            export TY="$ty" # env for perl to use

            # Remove simple imports `use crate::ty;`
            perl -pi -0777 -e 's/use crate::($ENV{TY});//g' "$file"

            # Replace pathed `crate::ty`
            perl -pi -0777 -e 's/crate::($ENV{TY})\b/$1/g' "$file"

            # Remove the type if it is part of a group import
            perl -pi -0777 -e 's/(use crate::\{?(.*|(\n.*){,2}))\b$ENV{TY}\b,? ?/$1/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
  • Loading branch information
tgross35 committed Nov 27, 2024
1 parent b8356e9 commit 7102017
Show file tree
Hide file tree
Showing 149 changed files with 258 additions and 244 deletions.
3 changes: 2 additions & 1 deletion src/fuchsia/aarch64.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{c_int, c_long, c_uint, c_ulong, c_ulonglong, c_ushort, off_t, size_t};
use crate::off_t;
use crate::prelude::*;

pub type c_char = u8;
pub type __u64 = c_ulonglong;
Expand Down
2 changes: 1 addition & 1 deletion src/fuchsia/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! More functions and definitions can be found in the more specific modules
//! according to the platform in question.
use crate::c_void;
use crate::prelude::*;

// PUB_TYPE

Expand Down
3 changes: 2 additions & 1 deletion src/fuchsia/riscv64.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{c_int, c_long, c_ulong, c_ulonglong, c_ushort, off_t};
use crate::off_t;
use crate::prelude::*;

// From psABI Calling Convention for RV64
pub type c_char = u8;
Expand Down
3 changes: 2 additions & 1 deletion src/fuchsia/x86_64.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{c_int, c_long, c_ulong, c_ulonglong, off_t, size_t};
use crate::off_t;
use crate::prelude::*;

pub type c_char = i8;
pub type wchar_t = i32;
Expand Down
2 changes: 0 additions & 2 deletions src/hermit.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! Hermit C type definitions
use crate::c_void;

cfg_if! {
if #[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))] {
pub type c_char = u8;
Expand Down
2 changes: 1 addition & 1 deletion src/solid/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! [SOLID]: https://solid.kmckk.com/
use crate::c_void;
use crate::prelude::*;

pub type c_schar = i8;
pub type c_uchar = u8;
Expand Down
6 changes: 2 additions & 4 deletions src/unix/aix/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::{
c_double, c_int, c_longlong, c_short, c_uchar, c_uint, c_ulonglong, c_ushort, c_void, intptr_t,
size_t, ssize_t,
};
use crate::c_double;
use crate::prelude::*;

pub type c_char = u8;
pub type caddr_t = *mut c_char;
Expand Down
6 changes: 2 additions & 4 deletions src/unix/aix/powerpc64.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::{
c_char, c_int, c_longlong, c_short, c_uint, c_ulonglong, c_ushort, c_void, off_t, size_t,
ssize_t,
};
use crate::off_t;
use crate::prelude::*;

pub type c_long = i64;
pub type c_ulong = u64;
Expand Down
2 changes: 1 addition & 1 deletion src/unix/bsd/apple/b32/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! 32-bit specific Apple (ios/darwin) definitions
use crate::{c_char, c_int, c_uchar, c_ushort};
use crate::prelude::*;

pub type c_long = i32;
pub type c_ulong = u32;
Expand Down
2 changes: 1 addition & 1 deletion src/unix/bsd/apple/b64/aarch64/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::c_int;
use crate::prelude::*;

pub type boolean_t = c_int;
pub type mcontext_t = *mut __darwin_mcontext64;
Expand Down
2 changes: 1 addition & 1 deletion src/unix/bsd/apple/b64/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! 64-bit specific Apple (ios/darwin) definitions
use crate::{c_char, c_int, c_uchar, c_uint, c_ushort};
use crate::prelude::*;

pub type c_long = i64;
pub type c_ulong = u64;
Expand Down
2 changes: 1 addition & 1 deletion src/unix/bsd/apple/b64/x86_64/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{c_char, c_int, c_short, c_uint, c_void, size_t};
use crate::prelude::*;

pub type boolean_t = c_uint;
pub type mcontext_t = *mut __darwin_mcontext64;
Expand Down
6 changes: 2 additions & 4 deletions src/unix/bsd/apple/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
//!
//! This covers *-apple-* triples currently
use crate::{
c_int, c_longlong, c_short, c_uchar, c_uint, c_ulonglong, c_ushort, c_void, cmsghdr, intptr_t,
off_t, size_t, ssize_t,
};
use crate::prelude::*;
use crate::{cmsghdr, off_t};

pub type c_char = i8;
pub type wchar_t = i32;
Expand Down
5 changes: 2 additions & 3 deletions src/unix/bsd/freebsdlike/dragonfly/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{
c_int, c_short, c_uchar, c_uint, c_ushort, c_void, cmsghdr, intptr_t, off_t, size_t, ssize_t,
};
use crate::prelude::*;
use crate::{cmsghdr, off_t};

pub type dev_t = u32;
pub type c_char = i8;
Expand Down
2 changes: 1 addition & 1 deletion src/unix/bsd/freebsdlike/freebsd/aarch64.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{c_int, c_longlong, size_t};
use crate::prelude::*;

pub type c_char = u8;
pub type c_long = i64;
Expand Down
2 changes: 1 addition & 1 deletion src/unix/bsd/freebsdlike/freebsd/arm.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{c_int, c_uint, c_void, size_t};
use crate::prelude::*;

pub type c_char = u8;
pub type c_long = i32;
Expand Down
3 changes: 2 additions & 1 deletion src/unix/bsd/freebsdlike/freebsd/freebsd11/b32.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{c_long, off_t};
use crate::off_t;
use crate::prelude::*;

#[repr(C)]
#[cfg_attr(feature = "extra_traits", derive(Debug, Eq, Hash, PartialEq))]
Expand Down
3 changes: 2 additions & 1 deletion src/unix/bsd/freebsdlike/freebsd/freebsd11/b64.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{c_long, off_t};
use crate::off_t;
use crate::prelude::*;

#[repr(C)]
#[cfg_attr(feature = "extra_traits", derive(Debug, Eq, Hash, PartialEq))]
Expand Down
4 changes: 1 addition & 3 deletions src/unix/bsd/freebsdlike/freebsd/freebsd11/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::{
c_char, c_int, c_long, c_short, c_uchar, c_uint, c_ushort, c_void, intptr_t, size_t, ssize_t,
};
use crate::prelude::*;

// APIs that were changed after FreeBSD 11

Expand Down
6 changes: 2 additions & 4 deletions src/unix/bsd/freebsdlike/freebsd/freebsd12/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::{
c_char, c_int, c_long, c_short, c_uchar, c_uint, c_ulong, c_ushort, c_void, off_t, size_t,
ssize_t,
};
use crate::off_t;
use crate::prelude::*;

// APIs in FreeBSD 12 that have changed since 11.

Expand Down
2 changes: 1 addition & 1 deletion src/unix/bsd/freebsdlike/freebsd/freebsd12/x86_64.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::c_int;
use crate::prelude::*;

pub const PROC_KPTI_CTL: c_int = crate::PROC_PROCCTL_MD_MIN;
pub const PROC_KPTI_CTL_ENABLE_ON_EXEC: c_int = 1;
Expand Down
6 changes: 2 additions & 4 deletions src/unix/bsd/freebsdlike/freebsd/freebsd13/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::{
c_char, c_int, c_long, c_short, c_uchar, c_uint, c_ulong, c_ushort, c_void, off_t, size_t,
ssize_t,
};
use crate::off_t;
use crate::prelude::*;

// APIs in FreeBSD 13 that have changed since 11.

Expand Down
2 changes: 1 addition & 1 deletion src/unix/bsd/freebsdlike/freebsd/freebsd13/x86_64.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::c_int;
use crate::prelude::*;

pub const PROC_KPTI_CTL: c_int = crate::PROC_PROCCTL_MD_MIN;
pub const PROC_KPTI_CTL_ENABLE_ON_EXEC: c_int = 1;
Expand Down
6 changes: 2 additions & 4 deletions src/unix/bsd/freebsdlike/freebsd/freebsd14/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::{
c_char, c_int, c_long, c_short, c_uchar, c_uint, c_ulong, c_ushort, c_void, off_t, size_t,
ssize_t,
};
use crate::off_t;
use crate::prelude::*;

// APIs in FreeBSD 14 that have changed since 11.

Expand Down
2 changes: 1 addition & 1 deletion src/unix/bsd/freebsdlike/freebsd/freebsd14/x86_64.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::c_int;
use crate::prelude::*;

pub const PROC_KPTI_CTL: c_int = crate::PROC_PROCCTL_MD_MIN;
pub const PROC_KPTI_CTL_ENABLE_ON_EXEC: c_int = 1;
Expand Down
6 changes: 2 additions & 4 deletions src/unix/bsd/freebsdlike/freebsd/freebsd15/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::{
c_char, c_int, c_long, c_short, c_uchar, c_uint, c_ulong, c_ushort, c_void, off_t, size_t,
ssize_t,
};
use crate::off_t;
use crate::prelude::*;

// APIs in FreeBSD 15 that have changed since 11.

Expand Down
2 changes: 1 addition & 1 deletion src/unix/bsd/freebsdlike/freebsd/freebsd15/x86_64.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::c_int;
use crate::prelude::*;

pub const PROC_KPTI_CTL: c_int = crate::PROC_PROCCTL_MD_MIN;
pub const PROC_KPTI_CTL_ENABLE_ON_EXEC: c_int = 1;
Expand Down
5 changes: 2 additions & 3 deletions src/unix/bsd/freebsdlike/freebsd/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{
c_int, c_longlong, c_short, c_uchar, c_uint, c_ushort, c_void, cmsghdr, off_t, size_t, ssize_t,
};
use crate::prelude::*;
use crate::{cmsghdr, off_t};

pub type fflags_t = u32;

Expand Down
2 changes: 1 addition & 1 deletion src/unix/bsd/freebsdlike/freebsd/powerpc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{c_int, size_t};
use crate::prelude::*;

pub type c_char = u8;
pub type c_long = i32;
Expand Down
2 changes: 1 addition & 1 deletion src/unix/bsd/freebsdlike/freebsd/powerpc64.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{c_int, size_t};
use crate::prelude::*;

pub type c_char = u8;
pub type c_long = i64;
Expand Down
2 changes: 1 addition & 1 deletion src/unix/bsd/freebsdlike/freebsd/riscv64.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{c_int, c_longlong, size_t};
use crate::prelude::*;

pub type c_char = u8;
pub type c_long = i64;
Expand Down
2 changes: 1 addition & 1 deletion src/unix/bsd/freebsdlike/freebsd/x86.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{c_int, size_t};
use crate::prelude::*;

pub type c_char = i8;
pub type c_long = i32;
Expand Down
2 changes: 1 addition & 1 deletion src/unix/bsd/freebsdlike/freebsd/x86_64/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{c_int, c_void, size_t};
use crate::prelude::*;

pub type c_char = i8;
pub type c_long = i64;
Expand Down
6 changes: 2 additions & 4 deletions src/unix/bsd/freebsdlike/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::{
c_double, c_int, c_short, c_uchar, c_uint, c_ulonglong, c_ushort, c_void, off_t, size_t,
ssize_t,
};
use crate::prelude::*;
use crate::{c_double, off_t};

pub type mode_t = u16;
pub type pthread_attr_t = *mut c_void;
Expand Down
3 changes: 2 additions & 1 deletion src/unix/bsd/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{c_double, c_int, c_short, c_uint, c_ushort, c_void, size_t, ssize_t};
use crate::c_double;
use crate::prelude::*;

pub type off_t = i64;
pub type useconds_t = u32;
Expand Down
3 changes: 2 additions & 1 deletion src/unix/bsd/netbsdlike/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{c_int, c_short, c_uint, c_ushort, c_void, off_t, size_t, ssize_t};
use crate::off_t;
use crate::prelude::*;

pub type wchar_t = i32;
pub type time_t = i64;
Expand Down
3 changes: 2 additions & 1 deletion src/unix/bsd/netbsdlike/netbsd/aarch64.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{c_int, c_uchar, c_uint, PT_FIRSTMACH};
use crate::prelude::*;
use crate::PT_FIRSTMACH;

pub type c_long = i64;
pub type c_ulong = u64;
Expand Down
3 changes: 2 additions & 1 deletion src/unix/bsd/netbsdlike/netbsd/arm.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{c_int, c_longlong, PT_FIRSTMACH};
use crate::prelude::*;
use crate::PT_FIRSTMACH;

pub type c_long = i32;
pub type c_ulong = u32;
Expand Down
3 changes: 2 additions & 1 deletion src/unix/bsd/netbsdlike/netbsd/mips.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{c_int, c_longlong, PT_FIRSTMACH};
use crate::prelude::*;
use crate::PT_FIRSTMACH;

pub type c_long = i32;
pub type c_ulong = u32;
Expand Down
6 changes: 2 additions & 4 deletions src/unix/bsd/netbsdlike/netbsd/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::{
c_int, c_short, c_uchar, c_uint, c_ulonglong, c_ushort, c_void, cmsghdr, intptr_t, off_t,
size_t, ssize_t,
};
use crate::prelude::*;
use crate::{cmsghdr, off_t};

pub type clock_t = c_uint;
pub type suseconds_t = c_int;
Expand Down
3 changes: 2 additions & 1 deletion src/unix/bsd/netbsdlike/netbsd/powerpc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{c_double, c_int, PT_FIRSTMACH};
use crate::prelude::*;
use crate::{c_double, PT_FIRSTMACH};

pub type c_long = i32;
pub type c_ulong = u32;
Expand Down
3 changes: 2 additions & 1 deletion src/unix/bsd/netbsdlike/netbsd/riscv64.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use PT_FIRSTMACH;

use crate::{c_double, c_int};
use crate::c_double;
use crate::prelude::*;

pub type c_long = i64;
pub type c_ulong = u64;
Expand Down
2 changes: 1 addition & 1 deletion src/unix/bsd/netbsdlike/netbsd/sparc64.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::c_uchar;
use crate::prelude::*;

pub type c_long = i64;
pub type c_ulong = u64;
Expand Down
2 changes: 1 addition & 1 deletion src/unix/bsd/netbsdlike/netbsd/x86.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{c_int, c_uchar};
use crate::prelude::*;

pub type c_long = i32;
pub type c_ulong = u32;
Expand Down
3 changes: 2 additions & 1 deletion src/unix/bsd/netbsdlike/netbsd/x86_64.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{c_int, c_uchar, c_uint, PT_FIRSTMACH};
use crate::prelude::*;
use crate::PT_FIRSTMACH;

pub type c_long = i64;
pub type c_ulong = u64;
Expand Down
2 changes: 1 addition & 1 deletion src/unix/bsd/netbsdlike/openbsd/aarch64.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::c_int;
use crate::prelude::*;

pub type c_long = i64;
pub type c_ulong = u64;
Expand Down
1 change: 1 addition & 0 deletions src/unix/bsd/netbsdlike/openbsd/arm.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::c_double;
use crate::prelude::*;

pub type c_long = i32;
pub type c_ulong = u32;
Expand Down
5 changes: 2 additions & 3 deletions src/unix/bsd/netbsdlike/openbsd/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::prelude::*;
use crate::unix::bsd::O_SYNC;
use crate::{
c_int, c_longlong, c_short, c_uchar, c_uint, c_ushort, c_void, cmsghdr, off_t, size_t,
};
use crate::{cmsghdr, off_t};

pub type clock_t = i64;
pub type suseconds_t = c_long;
Expand Down
1 change: 1 addition & 0 deletions src/unix/bsd/netbsdlike/openbsd/powerpc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::c_double;
use crate::prelude::*;

pub type c_long = i32;
pub type c_ulong = u32;
Expand Down
2 changes: 1 addition & 1 deletion src/unix/bsd/netbsdlike/openbsd/riscv64.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::c_int;
use crate::prelude::*;

pub type c_long = i64;
pub type c_ulong = u64;
Expand Down
2 changes: 1 addition & 1 deletion src/unix/bsd/netbsdlike/openbsd/x86.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::c_int;
use crate::prelude::*;

pub type c_long = i32;
pub type c_ulong = u32;
Expand Down
3 changes: 2 additions & 1 deletion src/unix/bsd/netbsdlike/openbsd/x86_64.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{c_int, PT_FIRSTMACH};
use crate::prelude::*;
use crate::PT_FIRSTMACH;

pub type c_long = i64;
pub type c_ulong = u64;
Expand Down
5 changes: 2 additions & 3 deletions src/unix/haiku/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{
c_double, c_int, c_short, c_uchar, c_uint, c_ushort, c_void, intptr_t, size_t, ssize_t,
};
use crate::c_double;
use crate::prelude::*;

pub type rlim_t = crate::uintptr_t;
pub type sa_family_t = u8;
Expand Down
Loading

0 comments on commit 7102017

Please sign in to comment.