use core::fmt::Display;
const UNDEFINED_LOCATION: &str = "<undefined location>";
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[non_exhaustive]
pub struct Location {
pub file: &'static str,
pub line: u32,
}
impl Location {
#[track_caller]
pub fn caller() -> Self {
let loc = ::core::panic::Location::caller();
Self {
file: loc.file(),
line: loc.line(),
}
}
pub fn undefined() -> Location {
Self { file: "", line: 0 }
}
#[inline]
pub fn is_undefined(&self) -> bool {
self.file.is_empty()
}
}
impl Display for Location {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
if self.is_undefined() {
write!(f, "{}", UNDEFINED_LOCATION)
} else {
write!(f, "{}:{}", self.file, self.line)
}
}
}