use std::ffi::OsStr;
use std::fmt;
use std::path::Path;
use crate::DiskUsage;
use crate::common::impl_get_set::impl_get_set;
pub struct Disk {
pub(crate) inner: crate::DiskInner,
}
impl Disk {
pub fn kind(&self) -> DiskKind {
self.inner.kind()
}
pub fn name(&self) -> &OsStr {
self.inner.name()
}
pub fn file_system(&self) -> &OsStr {
self.inner.file_system()
}
pub fn mount_point(&self) -> &Path {
self.inner.mount_point()
}
pub fn total_space(&self) -> u64 {
self.inner.total_space()
}
pub fn available_space(&self) -> u64 {
self.inner.available_space()
}
pub fn is_removable(&self) -> bool {
self.inner.is_removable()
}
pub fn is_read_only(&self) -> bool {
self.inner.is_read_only()
}
pub fn refresh(&mut self) -> bool {
self.refresh_specifics(DiskRefreshKind::everything())
}
pub fn refresh_specifics(&mut self, refreshes: DiskRefreshKind) -> bool {
self.inner.refresh_specifics(refreshes)
}
pub fn usage(&self) -> DiskUsage {
self.inner.usage()
}
}
pub struct Disks {
inner: crate::DisksInner,
}
impl Default for Disks {
fn default() -> Self {
Self::new()
}
}
impl From<Disks> for Vec<Disk> {
fn from(disks: Disks) -> Vec<Disk> {
disks.inner.into_vec()
}
}
impl From<Vec<Disk>> for Disks {
fn from(disks: Vec<Disk>) -> Self {
Self {
inner: crate::DisksInner::from_vec(disks),
}
}
}
impl<'a> IntoIterator for &'a Disks {
type Item = &'a Disk;
type IntoIter = std::slice::Iter<'a, Disk>;
fn into_iter(self) -> Self::IntoIter {
self.list().iter()
}
}
impl<'a> IntoIterator for &'a mut Disks {
type Item = &'a mut Disk;
type IntoIter = std::slice::IterMut<'a, Disk>;
fn into_iter(self) -> Self::IntoIter {
self.list_mut().iter_mut()
}
}
impl Disks {
pub fn new() -> Self {
Self {
inner: crate::DisksInner::new(),
}
}
pub fn new_with_refreshed_list() -> Self {
Self::new_with_refreshed_list_specifics(DiskRefreshKind::everything())
}
pub fn new_with_refreshed_list_specifics(refreshes: DiskRefreshKind) -> Self {
let mut disks = Self::new();
disks.refresh_specifics(false, refreshes);
disks
}
pub fn list(&self) -> &[Disk] {
self.inner.list()
}
pub fn list_mut(&mut self) -> &mut [Disk] {
self.inner.list_mut()
}
pub fn refresh(&mut self, remove_not_listed_disks: bool) {
self.inner
.refresh_specifics(remove_not_listed_disks, DiskRefreshKind::everything());
}
pub fn refresh_specifics(&mut self, remove_not_listed_disks: bool, refreshes: DiskRefreshKind) {
self.inner
.refresh_specifics(remove_not_listed_disks, refreshes);
}
}
impl std::ops::Deref for Disks {
type Target = [Disk];
fn deref(&self) -> &Self::Target {
self.list()
}
}
impl std::ops::DerefMut for Disks {
fn deref_mut(&mut self) -> &mut Self::Target {
self.list_mut()
}
}
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub enum DiskKind {
HDD,
SSD,
Unknown(isize),
}
impl fmt::Display for DiskKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(match *self {
DiskKind::HDD => "HDD",
DiskKind::SSD => "SSD",
_ => "Unknown",
})
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct DiskRefreshKind {
kind: bool,
storage: bool,
io_usage: bool,
}
impl DiskRefreshKind {
pub fn nothing() -> Self {
Self::default()
}
pub fn everything() -> Self {
Self {
kind: true,
storage: true,
io_usage: true,
}
}
impl_get_set!(DiskRefreshKind, kind, with_kind, without_kind);
impl_get_set!(DiskRefreshKind, storage, with_storage, without_storage);
impl_get_set!(DiskRefreshKind, io_usage, with_io_usage, without_io_usage);
}
#[cfg(test)]
mod tests {
#[test]
fn check_if_disks_is_send() {
fn is_send<T: Send>(_: &T) {}
let disks = crate::Disks::new();
is_send(&disks);
}
}