use std::fmt::Debug;
use std::fmt::Formatter;
use std::fmt::Result as FmtResult;
use std::path::PathBuf;
use crate::Pid;
#[derive(Clone)]
pub struct Elf {
pub path: PathBuf,
#[doc(hidden)]
pub _non_exhaustive: (),
}
impl Elf {
#[inline]
pub fn new(path: impl Into<PathBuf>) -> Self {
Self {
path: path.into(),
_non_exhaustive: (),
}
}
}
impl From<Elf> for Cache {
#[inline]
fn from(elf: Elf) -> Self {
Self::Elf(elf)
}
}
impl Debug for Elf {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
let Self {
path,
_non_exhaustive: (),
} = self;
f.debug_tuple(stringify!(Elf)).field(path).finish()
}
}
#[derive(Clone)]
pub struct Process {
pub pid: Pid,
pub cache_vmas: bool,
#[doc(hidden)]
pub _non_exhaustive: (),
}
impl Process {
#[inline]
pub fn new(pid: Pid) -> Self {
Self {
pid,
cache_vmas: true,
_non_exhaustive: (),
}
}
}
impl Debug for Process {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
let Self {
pid,
cache_vmas: _,
_non_exhaustive: (),
} = self;
f.debug_tuple(stringify!(Process))
.field(&format_args!("{pid}"))
.finish()
}
}
impl From<Process> for Cache {
#[inline]
fn from(process: Process) -> Self {
Self::Process(process)
}
}
#[derive(Clone)]
#[non_exhaustive]
pub enum Cache {
Elf(Elf),
Process(Process),
}
impl Debug for Cache {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
match self {
Self::Elf(elf) => Debug::fmt(elf, f),
Self::Process(process) => Debug::fmt(process, f),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn debug_repr() {
let elf = Elf::new("/foobar/baz");
assert_eq!(format!("{elf:?}"), "Elf(\"/foobar/baz\")");
let cache = Cache::from(elf);
assert_eq!(format!("{cache:?}"), "Elf(\"/foobar/baz\")");
let process = Process::new(Pid::Slf);
assert_eq!(format!("{process:?}"), "Process(self)");
let process = Process::new(Pid::from(1234));
assert_eq!(format!("{process:?}"), "Process(1234)");
let cache = Cache::from(process);
assert_eq!(format!("{cache:?}"), "Process(1234)");
}
}