mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-07 04:06:48 +01:00
core, input: add flex_trait
This commit is contained in:
parent
360b404b69
commit
8cbd7dd8e8
7 changed files with 201 additions and 98 deletions
74
core/src/core_macros.rs
Normal file
74
core/src/core_macros.rs
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
use crate::*;
|
||||
|
||||
/// Define and reexport submodules.
|
||||
#[macro_export] macro_rules! modules(
|
||||
($($($feat:literal?)? $name:ident),* $(,)?) => { $(
|
||||
$(#[cfg(feature=$feat)])? mod $name;
|
||||
$(#[cfg(feature=$feat)])? pub use self::$name::*;
|
||||
)* };
|
||||
($($($feat:literal?)? $name:ident $body:block),* $(,)?) => { $(
|
||||
$(#[cfg(feature=$feat)])? mod $name $body
|
||||
$(#[cfg(feature=$feat)])? pub use self::$name::*;
|
||||
)* };
|
||||
);
|
||||
|
||||
/// Define a trait for various wrapper types. */
|
||||
#[macro_export] macro_rules! flex_trait_mut (
|
||||
($Trait:ident $(<$($A:ident:$T:ident),+>)? {
|
||||
$(fn $fn:ident (&mut $self:ident $(, $arg:ident:$ty:ty)*) -> $ret:ty $body:block)*
|
||||
})=>{
|
||||
pub trait $Trait $(<$($A: $T),+>)? {
|
||||
$(fn $fn (&mut $self $(,$arg:$ty)*) -> $ret $body)*
|
||||
}
|
||||
impl<$($($A: $T,)+)? _T_: $Trait $(<$($A),+>)?> $Trait $(<$($A),+>)? for &mut _T_ {
|
||||
$(fn $fn (&mut $self $(,$arg:$ty)*) -> $ret { (*$self).$fn($($arg),*) })*
|
||||
}
|
||||
impl<$($($A: $T,)+)? _T_: $Trait $(<$($A),+>)?> $Trait $(<$($A),+>)? for Option<_T_> {
|
||||
$(fn $fn (&mut $self $(,$arg:$ty)*) -> $ret {
|
||||
if let Some(this) = $self { this.$fn($($arg),*) } else { Ok(None) }
|
||||
})*
|
||||
}
|
||||
impl<$($($A: $T,)+)? _T_: $Trait $(<$($A),+>)?> $Trait $(<$($A),+>)? for ::std::sync::Mutex<_T_> {
|
||||
$(fn $fn (&mut $self $(,$arg:$ty)*) -> $ret { $self.get_mut().unwrap().$fn($($arg),*) })*
|
||||
}
|
||||
impl<$($($A: $T,)+)? _T_: $Trait $(<$($A),+>)?> $Trait $(<$($A),+>)? for ::std::sync::Arc<::std::sync::Mutex<_T_>> {
|
||||
$(fn $fn (&mut $self $(,$arg:$ty)*) -> $ret { $self.lock().unwrap().$fn($($arg),*) })*
|
||||
}
|
||||
impl<$($($A: $T,)+)? _T_: $Trait $(<$($A),+>)?> $Trait $(<$($A),+>)? for ::std::sync::RwLock<_T_> {
|
||||
$(fn $fn (&mut $self $(,$arg:$ty)*) -> $ret { $self.write().unwrap().$fn($($arg),*) })*
|
||||
}
|
||||
impl<$($($A: $T,)+)? _T_: $Trait $(<$($A),+>)?> $Trait $(<$($A),+>)? for ::std::sync::Arc<::std::sync::RwLock<_T_>> {
|
||||
$(fn $fn (&mut $self $(,$arg:$ty)*) -> $ret { $self.write().unwrap().$fn($($arg),*) })*
|
||||
}
|
||||
};
|
||||
);
|
||||
|
||||
/// Implement [`Debug`] in bulk.
|
||||
#[macro_export] macro_rules! impl_debug(($($S:ty|$self:ident,$w:ident|$body:block)*)=>{
|
||||
$(impl std::fmt::Debug for $S {
|
||||
fn fmt (&$self, $w: &mut std::fmt::Formatter) -> std::fmt::Result $body
|
||||
})* });
|
||||
|
||||
/// Implement [`From`] in bulk.
|
||||
#[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 }}};
|
||||
($($Struct:ty { $(
|
||||
$(<$($l:lifetime),* $($T:ident$(:$U:ident)?),*>)? ($source:ident: $From:ty) $expr:expr
|
||||
);+ $(;)? })*) => { $(
|
||||
$(impl $(<$($l),* $($T$(:$U)?),*>)? From<$From> for $Struct {
|
||||
fn from ($source: $From) -> Self { $expr } })+ )* }; );
|
||||
|
||||
/// Implement [Has].
|
||||
#[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 } } };);
|
||||
|
||||
/// Implement [MaybeHas].
|
||||
#[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