mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 03:36:42 +01:00
core: add as_ref
This commit is contained in:
parent
d6dcf137a8
commit
18b6803912
1 changed files with 13 additions and 0 deletions
|
|
@ -1,13 +1,18 @@
|
||||||
mod core_macros;
|
mod core_macros;
|
||||||
pub(crate) use std::error::Error;
|
pub(crate) use std::error::Error;
|
||||||
|
|
||||||
/// Standard result type.
|
/// Standard result type.
|
||||||
pub type Usually<T> = Result<T, Box<dyn Error>>;
|
pub type Usually<T> = Result<T, Box<dyn Error>>;
|
||||||
|
|
||||||
/// Standard optional result type.
|
/// Standard optional result type.
|
||||||
pub type Perhaps<T> = Result<Option<T>, Box<dyn Error>>;
|
pub type Perhaps<T> = Result<Option<T>, Box<dyn Error>>;
|
||||||
|
|
||||||
/// Type-dispatched `get` and `get_mut`.
|
/// Type-dispatched `get` and `get_mut`.
|
||||||
pub trait Has<T>: Send + Sync { fn get (&self) -> &T; fn get_mut (&mut self) -> &mut T; }
|
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.
|
/// 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>; }
|
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`.
|
/// May compute a `RetVal` from `Args`.
|
||||||
pub trait Eval<Args, RetVal> {
|
pub trait Eval<Args, RetVal> {
|
||||||
/// A custom operation on [Args] that may return [Result::Err] or [Option::None].
|
/// 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 }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue