diff --git a/core/src/lib.rs b/core/src/lib.rs index f3cf313..b512ae9 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -16,6 +16,29 @@ pub type Perhaps = Result, Box>; } pub trait Has: 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_mut (&mut $self) -> &mut $T { &mut $x } + } + }; +} + +pub trait MaybeHas: 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 + } + }; +}