use std::{convert, fmt, process};
use crate::Flavor;
#[derive(Clone)]
pub struct Guard {
last_pid: u32,
}
impl fmt::Debug for Guard {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
f.debug_struct("Guard")
.field("flavor()", &self.flavor())
.field("last_pid", &self.last_pid)
.finish()
}
}
impl Default for Guard {
fn default() -> Self {
let last_pid = process::id();
Self { last_pid }
}
}
impl Guard {
pub const FLAVOR: Flavor = Flavor::Pid;
pub fn try_new() -> Result<Self, convert::Infallible> {
Ok(Default::default())
}
#[inline(always)]
pub fn detected_fork(&mut self) -> bool {
let current_pid = process::id();
if self.last_pid == current_pid {
false
} else {
self.set_pid(current_pid);
true
}
}
#[cold]
fn set_pid(&mut self, value: u32) {
self.last_pid = value;
}
pub fn flavor(&self) -> Flavor {
Self::FLAVOR.clone()
}
}