From 18b6803912105cc6a23217641a68967695a2166b Mon Sep 17 00:00:00 2001 From: unspeaker Date: Sun, 7 Sep 2025 23:27:35 +0300 Subject: [PATCH] core: add as_ref --- core/src/lib.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/core/src/lib.rs b/core/src/lib.rs index 0c63c05..3a51258 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -1,13 +1,18 @@ mod core_macros; pub(crate) use std::error::Error; + /// Standard result type. pub type Usually = Result>; + /// Standard optional result type. pub type Perhaps = Result, Box>; + /// Type-dispatched `get` and `get_mut`. pub trait Has: 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: Send + Sync { fn get (&self) -> Option<&T>; fn get_mut (&mut self) -> Option<&mut T>; } + /// May compute a `RetVal` from `Args`. pub trait Eval { /// A custom operation on [Args] that may return [Result::Err] or [Option::None]. @@ -22,3 +27,11 @@ pub trait Eval { } } } + +#[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 } + } + }; +}