use std::fmt::Display;
use futures::Future;
#[macro_export]
macro_rules! location {
() => {{
macro_rules! fn_name {
() => {{
fn type_name_of_val<T: ?Sized>(_: &T) -> &'static str {
core::any::type_name::<T>()
}
type_name_of_val(&|| {})
.strip_suffix("::{{closure}}")
.unwrap()
}};
}
$crate::Location::from_components(fn_name!(), &(file!(), line!(), column!()))
}};
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Location {
name: Option<&'static str>,
rest: &'static (&'static str, u32, u32),
}
impl Location {
#[doc(hidden)]
#[inline(always)]
pub const fn from_components(
name: &'static str,
rest: &'static (&'static str, u32, u32),
) -> Self {
Self {
name: Some(name),
rest,
}
}
pub fn frame<F>(self, f: F) -> impl Future<Output = F::Output>
where
F: Future,
{
crate::Framed::new(f, self)
}
pub const fn name(&self) -> Option<&str> {
self.name
}
pub const fn file(&self) -> &str {
self.rest.0
}
pub const fn line(&self) -> u32 {
self.rest.1
}
pub const fn column(&self) -> u32 {
self.rest.2
}
}
impl Display for Location {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let file = self.file();
let line = self.line();
let column = self.column();
if let Some(name) = self.name() {
f.write_fmt(format_args!("{name} at {file}:{line}:{column}"))
} else {
f.write_fmt(format_args!("{file}:{line}:{column}"))
}
}
}