core: add as_ref

This commit is contained in:
🪞👃🪞 2025-09-07 23:27:35 +03:00
parent d6dcf137a8
commit 18b6803912

View file

@ -1,13 +1,18 @@
mod core_macros;
pub(crate) use std::error::Error;
/// Standard result type.
pub type Usually<T> = Result<T, Box<dyn Error>>;
/// Standard optional result type.
pub type Perhaps<T> = Result<Option<T>, Box<dyn Error>>;
/// Type-dispatched `get` and `get_mut`.
pub trait Has<T>: Send + Sync { fn get (&self) -> &T; fn get_mut (&mut self) -> &mut T; }
/// Type-dispatched `get` and `get_mut` that return an [Option]-wrapped result.
pub trait MaybeHas<T>: Send + Sync { fn get (&self) -> Option<&T>; fn get_mut (&mut self) -> Option<&mut T>; }
/// May compute a `RetVal` from `Args`.
pub trait Eval<Args, RetVal> {
/// A custom operation on [Args] that may return [Result::Err] or [Option::None].
@ -22,3 +27,11 @@ pub trait Eval<Args, RetVal> {
}
}
}
#[macro_export] macro_rules! as_ref {
($T:ty: |$self:ident : $S:ty| $x:expr) => {
impl AsRef<$T> for $S {
fn as_ref (&$self) -> &$T { &$x }
}
};
}