sinf 0.1.0

Library for obtaining in-depth system information at both the hardware and software levels
Documentation
use std::ffi::c_char;

use super::DiskInner;

pub(super) trait GetValues {
    fn update_old(&mut self);
    fn get_read(&mut self) -> &mut u64;
    fn get_written(&mut self) -> &mut u64;
    fn dev_id(&self) -> &[c_char];
}

impl GetValues for crate::Disk {
    fn update_old(&mut self) {
        self.inner.update_old()
    }
    fn get_read(&mut self) -> &mut u64 {
        self.inner.get_read()
    }
    fn get_written(&mut self) -> &mut u64 {
        self.inner.get_written()
    }
    fn dev_id(&self) -> &[c_char] {
        self.inner.dev_id()
    }
}

impl GetValues for &mut DiskInner {
    fn update_old(&mut self) {
        self.old_read_bytes = self.read_bytes;
        self.old_written_bytes = self.written_bytes;
    }
    fn get_read(&mut self) -> &mut u64 {
        &mut self.read_bytes
    }
    fn get_written(&mut self) -> &mut u64 {
        &mut self.written_bytes
    }
    fn dev_id(&self) -> &[c_char] {
        self.dev_id.as_slice()
    }
}
impl GetValues for DiskInner {
    fn update_old(&mut self) {
        self.old_read_bytes = self.read_bytes;
        self.old_written_bytes = self.written_bytes;
    }
    fn get_read(&mut self) -> &mut u64 {
        &mut self.read_bytes
    }
    fn get_written(&mut self) -> &mut u64 {
        &mut self.written_bytes
    }
    fn dev_id(&self) -> &[c_char] {
        self.dev_id.as_slice()
    }
}

pub(super) fn same_name(dev_id: &[c_char], drive_name: &[c_char]) -> bool {
    // NetBSD iostats returns device names without partition letters (e.g. "ld0"), whereas mount points use full device names with partitions (e.g. "ld0a").

    for (c1, c2) in dev_id.iter().zip(drive_name.iter()) {
        // Stop the comparison at the null terminator in `drive_name`.
        if *c2 == 0 {
            return true;
        }
        if c1 != c2 {
            return false;
        }
    }
    true
}