You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
dirstat/src/data/mod.rs

84 lines
1.9 KiB

//! Datatypes for the program
use super::*;
use std::fs::Metadata;
mod cache; pub use cache::*;
/// A raw file or directory inode number
///
/// Ususally created from the `.inode()` extension method on `fs::Metadata` found in prelude.
/// Can also be created with `new()` from a `fs::Metadata` reference, or created unsafely from an arbitrary `u64` with `new_unchecked`.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
#[repr(transparent)]
pub struct INode(u64);
impl INode
{
/// Create a new `INode` wrapper from any `u64` without checking if it is a real inode.
#[inline] pub const unsafe fn new_unchecked(ino: u64) -> Self
{
Self(ino)
}
/// Get `ino` from an `fs::Metadata` object.
#[inline] pub fn new(meta: &Metadata) -> Self
{
use std::os::unix::fs::MetadataExt as _;
Self(meta.ino())
}
/// Convert into raw `u64` inode number.
#[inline] pub const fn into_inner(self) -> u64
{
self.0
}
}
impl<'a> From<&'a Metadata> for INode
{
#[inline] fn from(from: &'a Metadata) -> Self
{
from.inode()
}
}
impl From<INode> for u64
{
#[inline] fn from(from: INode) -> Self
{
from.0
}
}
/// A valid file system info
///
/// # Note
/// This does not contains the INode of the fs object itself, that is considered that key to the table.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum FsInfo
{
File(u64, INode), //Size, parent dir inode
Directory,
}
#[cfg(test)]
mod tests
{
use super::*;
#[tokio::test]
async fn cache_insert_and_consume()
{
let mut cache = Cache::new();
for x in 0..500
{
cache.insert(unsafe { INode::new_unchecked(x) }, FsInfo::Directory).await;
}
let output = cache.try_complete().await.unwrap();
assert_eq!(output.len(), 500);
for x in 0..500
{
assert_eq!(output.get(&unsafe { INode::new_unchecked(x) }).unwrap(), &FsInfo::Directory);
}
}
}