mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 03:36:42 +01:00
This commit is contained in:
parent
17506726cb
commit
c8827b43c3
9 changed files with 114 additions and 135 deletions
|
|
@ -1,4 +1,4 @@
|
|||
use std::error::Error;
|
||||
pub(crate) use std::error::Error;
|
||||
|
||||
/// Standard result type.
|
||||
pub type Usually<T> = Result<T, Box<dyn Error>>;
|
||||
|
|
@ -16,29 +16,56 @@ pub type Perhaps<T> = Result<Option<T>, Box<dyn Error>>;
|
|||
}
|
||||
|
||||
pub trait Has<T>: Send + Sync {
|
||||
fn get (&self) -> &T;
|
||||
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 (&$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 (&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 (&$self) -> Option<&$T> $x
|
||||
fn get_mut (&mut $self) -> Option<&mut $T> $y
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// May compute a `RetVal` from `Args`.
|
||||
pub trait Eval<Args, RetVal> {
|
||||
/// A custom operation on [Args] that may return [Result::Err] or [Option::None].
|
||||
fn try_eval (&self, args: Args) -> Perhaps<RetVal>;
|
||||
/// Invoke a custom operation, converting a `None` result to a custom `Box<dyn Error>`.
|
||||
fn eval <E: Into<Box<dyn std::error::Error>>> (&self, args: Args, error: impl Fn()->E)
|
||||
-> Usually<RetVal>
|
||||
{
|
||||
match self.try_eval(args)? {
|
||||
Some(value) => Ok(value),
|
||||
_ => Result::Err(format!("Eval: {}", error().into()).into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//impl<S: Eval<I, O>, I, O> Eval<I, O> for &S {
|
||||
//fn try_eval (&self, input: I) -> Perhaps<O> {
|
||||
//(*self).try_eval(input)
|
||||
//}
|
||||
//}
|
||||
|
||||
//impl<S: Eval<I, O>, I: Ast, O: Dsl<S>> Eval<I, O> for S {
|
||||
//fn try_eval (&self, input: I) -> Perhaps<O> {
|
||||
//Dsl::try_provide(self, input)
|
||||
//}
|
||||
//}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue