Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make all source files visible to rustfmt #1398

Merged
merged 3 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ mod debug;
mod serde;
pub(crate) mod utils;

// Make formattable by rustfmt.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So just to confirm: this cfg is always evaluated to false but at least it allows rustfmt to see the file. That's a super clever trick. Thanks for that!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right.

In the longer term, hopefully rustfmt will get a built-in understanding of the syntax of cfg_if! (rust-lang/rustfmt#3253) or cfg_match! (rust-lang/rust#115585) and the workaround won't be needed.

Or alternatively, someone might make an alternative macro that doesn't rely on dedicated rustfmt support for its syntax, something like:

#[apply_cfg]
enum __ {
    r#if = #[cfg(feature = "unknown-ci")]
    {
        // This is used in CI to check that the build for unknown targets is compiling fine.
        mod unknown;
        use crate::unknown as sys;

        #[cfg(test)]
        pub(crate) const MIN_USERS: usize = 0;
    },

    r#else = #[cfg(any(
        target_os = "macos",
        target_os = "ios",
        target_os = "linux",
        target_os = "android",
        target_os = "freebsd"
    ))]
    {
        mod unix;
        use crate::unix::sys;

        #[cfg(feature = "network")]
        mod network;
        #[cfg(feature = "network")]
        use crate::unix::network_helper;

        #[cfg(test)]
        pub(crate) const MIN_USERS: usize = 1;
    },

    ...
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer the current solution. It duplicates code but at least it's "easy" to see what's going on. Gonna check with rustfmt team if they'd be open to add some special cases for macros like cfg_if!.

#[cfg(any())]
mod network;
#[cfg(any())]
mod unix;
#[cfg(any())]
mod unknown;
#[cfg(any())]
mod windows;

/// This function is only used on Linux targets, when the `system` feature is enabled. In other
/// cases, it does nothing and returns `false`.
///
Expand Down
7 changes: 4 additions & 3 deletions src/unix/apple/macos/component/arm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ impl ComponentsInner {
None => continue,
};

let name_ptr = CFStringGetCStringPtr(name.inner() as *const _, kCFStringEncodingUTF8);
let name_ptr =
CFStringGetCStringPtr(name.inner() as *const _, kCFStringEncodingUTF8);
if name_ptr.is_null() {
continue;
}
Expand All @@ -135,8 +136,8 @@ pub(crate) struct ComponentInner {
critical: Option<f32>,
}

unsafe impl Send for ComponentInner{}
unsafe impl Sync for ComponentInner{}
unsafe impl Send for ComponentInner {}
unsafe impl Sync for ComponentInner {}

impl ComponentInner {
pub(crate) fn new(
Expand Down
66 changes: 49 additions & 17 deletions src/unix/apple/macos/ffi.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
// Take a look at the license at the top of the repository in the LICENSE file.

#[cfg(any(feature = "system", feature = "disk"))]
use core_foundation_sys::base::CFAllocatorRef;
#[cfg(any(
feature = "system",
feature = "disk",
all(feature = "component", any(target_arch = "x86", target_arch = "x86_64")),
all(
feature = "component",
any(target_arch = "x86", target_arch = "x86_64")
),
))]
use core_foundation_sys::base::mach_port_t;
#[cfg(any(feature = "system", feature = "disk"))]
use core_foundation_sys::base::CFAllocatorRef;
#[cfg(any(feature = "system", feature = "disk"))]
use core_foundation_sys::dictionary::CFDictionaryRef;
#[cfg(any(
feature = "system",
feature = "disk",
all(feature = "component", any(target_arch = "x86", target_arch = "x86_64")),
all(
feature = "component",
any(target_arch = "x86", target_arch = "x86_64")
),
))]
use core_foundation_sys::dictionary::CFMutableDictionaryRef;
#[cfg(any(feature = "system", feature = "disk"))]
Expand All @@ -23,7 +29,10 @@ use libc::c_char;
#[cfg(any(
feature = "system",
feature = "disk",
all(feature = "component", any(target_arch = "x86", target_arch = "x86_64")),
all(
feature = "component",
any(target_arch = "x86", target_arch = "x86_64")
),
))]
use libc::kern_return_t;

Expand All @@ -32,15 +41,21 @@ use libc::kern_return_t;
#[cfg(any(
feature = "system",
feature = "disk",
all(feature = "component", any(target_arch = "x86", target_arch = "x86_64")),
all(
feature = "component",
any(target_arch = "x86", target_arch = "x86_64")
),
))]
#[allow(non_camel_case_types)]
pub type io_object_t = mach_port_t;

#[cfg(any(
feature = "system",
feature = "disk",
all(feature = "component", any(target_arch = "x86", target_arch = "x86_64")),
all(
feature = "component",
any(target_arch = "x86", target_arch = "x86_64")
),
))]
#[allow(non_camel_case_types)]
pub type io_iterator_t = io_object_t;
Expand Down Expand Up @@ -85,15 +100,21 @@ cfg_if! {
#[cfg(any(
feature = "system",
feature = "disk",
all(feature = "component", any(target_arch = "x86", target_arch = "x86_64")),
all(
feature = "component",
any(target_arch = "x86", target_arch = "x86_64")
),
))]
pub const kIOMasterPortDefault: mach_port_t = 0;

// Note: Obtaining information about disks using IOKIt is allowed inside the default macOS App Sandbox.
#[cfg(any(
feature = "system",
feature = "disk",
all(feature = "component", any(target_arch = "x86", target_arch = "x86_64")),
all(
feature = "component",
any(target_arch = "x86", target_arch = "x86_64")
),
))]
#[link(name = "IOKit", kind = "framework")]
extern "C" {
Expand All @@ -104,7 +125,10 @@ extern "C" {
) -> kern_return_t;
#[cfg(any(
feature = "system",
all(feature = "component", any(target_arch = "x86", target_arch = "x86_64")),
all(
feature = "component",
any(target_arch = "x86", target_arch = "x86_64")
),
))]
pub fn IOServiceMatching(a: *const c_char) -> CFMutableDictionaryRef;

Expand Down Expand Up @@ -134,15 +158,15 @@ extern "C" {
#[cfg(feature = "system")]
pub fn IORegistryEntryGetName(entry: io_registry_entry_t, name: io_name_t) -> kern_return_t;
#[cfg(feature = "disk")]
pub fn IOObjectConformsTo(
object: io_object_t,
className: *const c_char,
) -> libc::boolean_t;
pub fn IOObjectConformsTo(object: io_object_t, className: *const c_char) -> libc::boolean_t;
}

#[cfg(any(
feature = "system",
all(feature = "component", any(target_arch = "x86", target_arch = "x86_64")),
all(
feature = "component",
any(target_arch = "x86", target_arch = "x86_64")
),
))]
pub const KIO_RETURN_SUCCESS: i32 = 0;

Expand Down Expand Up @@ -249,7 +273,11 @@ mod io_service {}
))]
pub use io_service::*;

#[cfg(all(feature = "component", not(feature = "apple-sandbox"), target_arch = "aarch64"))]
#[cfg(all(
feature = "component",
not(feature = "apple-sandbox"),
target_arch = "aarch64"
))]
mod io_service {
use std::ptr::null;

Expand Down Expand Up @@ -368,5 +396,9 @@ mod io_service {
}
}

#[cfg(all(feature = "component", not(feature = "apple-sandbox"), target_arch = "aarch64"))]
#[cfg(all(
feature = "component",
not(feature = "apple-sandbox"),
target_arch = "aarch64"
))]
pub use io_service::*;
14 changes: 14 additions & 0 deletions src/unix/apple/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,17 @@ cfg_if! {
pub mod component;
}
}

// Make formattable by rustfmt.
#[cfg(any())]
mod component;
#[cfg(any())]
mod cpu;
#[cfg(any())]
mod disk;
#[cfg(any())]
mod process;
#[cfg(any())]
mod system;
#[cfg(any())]
mod utils;
2 changes: 1 addition & 1 deletion src/unix/apple/macos/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ impl ProcessInner {
}

pub(crate) fn switch_updated(&mut self) -> bool {
std::mem::replace(&mut self.updated, false)
std::mem::replace(&mut self.updated, false)
}
}

Expand Down
22 changes: 22 additions & 0 deletions src/unix/apple/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,25 @@ cfg_if! {

#[doc = include_str!("../../../md_doc/is_supported.md")]
pub const IS_SUPPORTED_SYSTEM: bool = true;

// Make formattable by rustfmt.
#[cfg(any())]
mod component;
#[cfg(any())]
mod cpu;
#[cfg(any())]
mod disk;
#[cfg(any())]
mod groups;
#[cfg(any())]
mod ios;
#[cfg(any())]
mod macos;
#[cfg(any())]
mod network;
#[cfg(any())]
mod process;
#[cfg(any())]
mod system;
#[cfg(any())]
mod users;
46 changes: 36 additions & 10 deletions src/unix/apple/network.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use libc::{self, c_char, c_int, c_uint, if_data64, if_msghdr2, sysctl, CTL_NET, IFNAMSIZ, NET_RT_IFLIST2, PF_ROUTE, RTM_IFINFO2};
use libc::{
self, c_char, c_int, c_uint, if_data64, if_msghdr2, sysctl, CTL_NET, IFNAMSIZ, NET_RT_IFLIST2,
PF_ROUTE, RTM_IFINFO2,
};

use std::collections::{hash_map, HashMap};
use std::mem::{MaybeUninit, size_of};
use std::mem::{size_of, MaybeUninit};
use std::ptr::null_mut;

use crate::network::refresh_networks_addresses;
Expand Down Expand Up @@ -38,11 +41,27 @@ fn update_network_data(inner: &mut NetworkDataInner, data: &if_data64) {
update_field(&mut inner.old_out, &mut inner.current_out, data.ifi_obytes);
update_field(&mut inner.old_in, &mut inner.current_in, data.ifi_ibytes);

update_field(&mut inner.old_packets_out, &mut inner.packets_out, data.ifi_opackets);
update_field(&mut inner.old_packets_in, &mut inner.packets_in, data.ifi_ipackets);

update_field(&mut inner.old_errors_in, &mut inner.errors_in, data.ifi_ierrors);
update_field(&mut inner.old_errors_out, &mut inner.errors_out, data.ifi_oerrors);
update_field(
&mut inner.old_packets_out,
&mut inner.packets_out,
data.ifi_opackets,
);
update_field(
&mut inner.old_packets_in,
&mut inner.packets_in,
data.ifi_ipackets,
);

update_field(
&mut inner.old_errors_in,
&mut inner.errors_in,
data.ifi_ierrors,
);
update_field(
&mut inner.old_errors_out,
&mut inner.errors_out,
data.ifi_oerrors,
);
}

pub(crate) struct NetworksInner {
Expand Down Expand Up @@ -83,7 +102,7 @@ impl NetworksInner {
NETLINK_GENERIC,
IFMIB_IFDATA,
0,
IFDATA_GENERAL
IFDATA_GENERAL,
];

let mut len = 0;
Expand Down Expand Up @@ -146,7 +165,14 @@ impl NetworksInner {
let mut mib_data: MaybeUninit<ifmibdata> = MaybeUninit::uninit();

mib2[4] = (*if2m).ifm_index as _;
let ret = sysctl(mib2.as_mut_ptr(), mib2.len() as _, mib_data.as_mut_ptr() as *mut _, &mut size_of::<ifmibdata>(), null_mut(), 0);
let ret = sysctl(
mib2.as_mut_ptr(),
mib2.len() as _,
mib_data.as_mut_ptr() as *mut _,
&mut size_of::<ifmibdata>(),
null_mut(),
0,
);

match self.interfaces.entry(name) {
hash_map::Entry::Occupied(mut e) => {
Expand Down Expand Up @@ -198,7 +224,7 @@ impl NetworksInner {
packets_out = data.ifi_opackets;
errors_in = data.ifi_ierrors;
errors_out = data.ifi_oerrors;
}
}

e.insert(NetworkData {
inner: NetworkDataInner {
Expand Down
5 changes: 4 additions & 1 deletion src/unix/apple/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use crate::sys::cpu::*;
use crate::sys::process::*;
use crate::sys::utils::{get_sys_value, get_sys_value_by_name};

use crate::{Cpu, CpuRefreshKind, LoadAvg, MemoryRefreshKind, Pid, Process, ProcessesToUpdate, ProcessRefreshKind};
use crate::{
Cpu, CpuRefreshKind, LoadAvg, MemoryRefreshKind, Pid, Process, ProcessRefreshKind,
ProcessesToUpdate,
};

#[cfg(all(target_os = "macos", not(feature = "apple-sandbox")))]
use std::cell::UnsafeCell;
Expand Down
Loading