Skip to content

Commit

Permalink
Format with rustfmt 1.8.0-nightly
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Nov 26, 2024
1 parent ab99a39 commit ab30881
Show file tree
Hide file tree
Showing 21 changed files with 418 additions and 282 deletions.
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::*;
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
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
50 changes: 30 additions & 20 deletions src/unix/freebsd/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,18 @@ use std::ffi::{OsStr, OsString};
use std::marker::PhantomData;
use std::os::unix::ffi::OsStringExt;
use std::path::{Path, PathBuf};
use std::ptr::{NonNull, null_mut};
use std::ptr::{null_mut, NonNull};
use std::sync::OnceLock;

use libc::c_void;

use crate::{Disk, DiskKind, DiskUsage};
use super::ffi::{
devstat,
devstat_getversion,
geom_stats_open,
geom_stats_snapshot_get,
geom_stats_snapshot_next,
geom_stats_snapshot_reset,
geom_stats_snapshot_free,
DEVSTAT_READ,
devstat, devstat_getversion, geom_stats_open, geom_stats_snapshot_free,
geom_stats_snapshot_get, geom_stats_snapshot_next, geom_stats_snapshot_reset, DEVSTAT_READ,
DEVSTAT_WRITE,
};
use super::utils::{c_buf_to_utf8_str, get_sys_value_str_by_name};
use crate::{Disk, DiskKind, DiskUsage};

#[derive(Debug)]
pub(crate) struct DiskInner {
Expand Down Expand Up @@ -97,7 +91,9 @@ impl crate::DisksInner {
}

pub(crate) fn refresh_list(&mut self) {
unsafe { get_all_list(&mut self.disks, true); }
unsafe {
get_all_list(&mut self.disks, true);
}
}

pub(crate) fn list(&self) -> &[Disk] {
Expand All @@ -109,7 +105,9 @@ impl crate::DisksInner {
}

pub(crate) fn refresh(&mut self) {
unsafe { get_all_list(&mut self.disks, false); }
unsafe {
get_all_list(&mut self.disks, false);
}
}
}

Expand Down Expand Up @@ -200,16 +198,26 @@ unsafe fn initialize_geom() -> Result<(), ()> {
unsafe fn refresh_disk_io<T: GetValues>(disks: &mut [T]) {
static GEOM_STATS: OnceLock<Result<(), ()>> = OnceLock::new();

if GEOM_STATS.get_or_init(|| unsafe { initialize_geom() }).is_err() {
if GEOM_STATS
.get_or_init(|| unsafe { initialize_geom() })
.is_err()
{
return;
}
let Some(mut snap) = GeomSnapshot::new() else { return };
let Some(mut snap) = GeomSnapshot::new() else {
return;
};
for device in snap.iter() {
let device = device.devstat.as_ref();
let Some(device_name) = c_buf_to_utf8_str(&device.device_name) else { continue };
let Some(device_name) = c_buf_to_utf8_str(&device.device_name) else {
continue;
};
let dev_stat_name = format!("{device_name}{}", device.unit_number);

for disk in disks.iter_mut().filter(|d| d.dev_id().is_some_and(|id| *id == dev_stat_name)) {
for disk in disks
.iter_mut()
.filter(|d| d.dev_id().is_some_and(|id| *id == dev_stat_name))
{
disk.update_old();
*disk.get_read() = device.bytes[DEVSTAT_READ];
*disk.get_written() = device.bytes[DEVSTAT_WRITE];
Expand Down Expand Up @@ -256,7 +264,9 @@ unsafe fn refresh_disk_io<T: GetValues>(disks: &mut [T]) {

fn get_disks_mapping() -> HashMap<String, String> {
let mut disk_mapping = HashMap::new();
let Some(mapping) = get_sys_value_str_by_name(b"kern.geom.conftxt\0") else { return disk_mapping };
let Some(mapping) = get_sys_value_str_by_name(b"kern.geom.conftxt\0") else {
return disk_mapping;
};

let mut last_id = String::new();

Expand Down Expand Up @@ -357,8 +367,8 @@ pub unsafe fn get_all_list(container: &mut Vec<Disk>, add_new_disks: bool) {
let dev_mount_point = c_buf_to_utf8_str(&fs_info.f_mntfromname).unwrap_or("");

// USB keys and CDs are removable.
let is_removable =
[b"USB", b"usb"].iter().any(|b| *b == &fs_type[..]) || fs_type.starts_with(b"/dev/cd");
let is_removable = [b"USB", b"usb"].iter().any(|b| *b == &fs_type[..])
|| fs_type.starts_with(b"/dev/cd");

container.push(Disk {
inner: DiskInner {
Expand Down Expand Up @@ -478,7 +488,7 @@ impl<'a> Iterator for GeomSnapshotIter<'a> {
type Item = Devstat<'a>;

fn next(&mut self) -> Option<Self::Item> {
let raw = unsafe { geom_stats_snapshot_next(self.0.0.as_mut()) };
let raw = unsafe { geom_stats_snapshot_next(self.0 .0.as_mut()) };
NonNull::new(raw).map(|devstat| Devstat {
devstat,
phantom: PhantomData,
Expand Down
2 changes: 1 addition & 1 deletion src/unix/freebsd/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#![allow(non_camel_case_types)]

use libc::{c_char, c_int, c_uint, c_void, bintime, kvm_t};
use libc::{bintime, c_char, c_int, c_uint, c_void, kvm_t};

// definitions come from:
// https://github.com/freebsd/freebsd-src/blob/main/lib/libdevstat/devstat.h
Expand Down
Loading

0 comments on commit ab30881

Please sign in to comment.