mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
add has!, MaybeHas, maybe_has!
This commit is contained in:
parent
4ff4ea8173
commit
b25977d878
1 changed files with 24 additions and 1 deletions
|
|
@ -19,3 +19,26 @@ pub trait Has<T>: Send + Sync {
|
||||||
fn get (&self) -> &T;
|
fn get (&self) -> &T;
|
||||||
fn get_mut (&mut self) -> &mut 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
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue