mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
44 lines
1.2 KiB
Rust
44 lines
1.2 KiB
Rust
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>>;
|
|
|
|
/// Implement the `From` trait.
|
|
#[macro_export] macro_rules! from {
|
|
($(<$($lt:lifetime),+>)?|$state:ident:$Source:ty|$Target:ty=$cb:expr) => {
|
|
impl $(<$($lt),+>)? From<$Source> for $Target {
|
|
fn from ($state:$Source) -> Self { $cb }
|
|
}
|
|
};
|
|
}
|
|
|
|
pub trait Has<T>: Send + Sync {
|
|
fn get (&self) -> &T;
|
|
fn get_mut (&mut self) -> &mut T;
|
|
}
|
|
|
|
#[macro_export] macro_rules! has {
|
|
($T:ty: |$self:ident : $S:ty| $x:expr) => {
|
|
impl Has<$T> for $S {
|
|
fn get (&$self) -> &$T { &$x }
|
|
fn get_mut (&mut $self) -> &mut $T { &mut $x }
|
|
}
|
|
};
|
|
}
|
|
|
|
pub trait MaybeHas<T>: Send + Sync {
|
|
fn get (&self) -> Option<&T>;
|
|
fn get_mut (&mut self) -> Option<&mut T>;
|
|
}
|
|
|
|
#[macro_export] macro_rules! maybe_has {
|
|
($T:ty: |$self:ident : $S:ty| $x:block; $y:block $(;)?) => {
|
|
impl MaybeHas<$T> for $S {
|
|
fn get (&$self) -> Option<&$T> $x
|
|
fn get_mut (&mut $self) -> Option<&mut $T> $y
|
|
}
|
|
};
|
|
}
|