mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
102 lines
5.1 KiB
Rust
102 lines
5.1 KiB
Rust
/// 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 an implement it for read-only wrapper types. */
|
|
#[macro_export] macro_rules! flex_trait (
|
|
($Trait:ident $(<$($A:ident:$T:ident),+>)? $(:$dep:ident $(<$dtt:tt>)? $(+$dep2:ident $(<$dtt2:tt>)?)*)? {
|
|
$(fn $fn:ident (&$self:ident $(, $arg:ident:$ty:ty)*) -> $ret:ty $body:block)*
|
|
}) => {
|
|
pub trait $Trait $(<$($A: $T),+>)? $(:$dep $(<$dtt>)? $(+$dep2 $(<$dtt2>)?)*)? {
|
|
$(fn $fn (&$self $(,$arg:$ty)*) -> $ret $body)*
|
|
}
|
|
impl<$($($A: $T,)+)? _T_: $Trait $(<$($A),+>)?> $Trait $(<$($A),+>)? for &_T_ {
|
|
$(fn $fn (&$self $(,$arg:$ty)*) -> $ret { (*$self).$fn($($arg),*) })*
|
|
}
|
|
impl<$($($A: $T,)+)? _T_: $Trait $(<$($A),+>)?> $Trait $(<$($A),+>)? for &mut _T_ {
|
|
$(fn $fn (&$self $(,$arg:$ty)*) -> $ret { (**$self).$fn($($arg),*) })*
|
|
}
|
|
impl<$($($A: $T,)+)? _T_: $Trait $(<$($A),+>)?> $Trait $(<$($A),+>)? for ::std::sync::Arc<_T_> {
|
|
$(fn $fn (&$self $(,$arg:$ty)*) -> $ret { (*$self).$fn($($arg),*) })*
|
|
}
|
|
//impl<$($($A: $T,)+)? _T_: $Trait $(<$($A),+>)?> $Trait $(<$($A),+>)? for Option<_T_> {
|
|
//$(fn $fn (&$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 (&$self $(,$arg:$ty)*) -> $ret { (*$self).lock().unwrap().$fn($($arg),*) })*
|
|
//}
|
|
//impl<$($($A: $T,)+)? _T_: $Trait $(<$($A),+>)?> $Trait $(<$($A),+>)? for ::std::sync::RwLock<_T_> {
|
|
//$(fn $fn (&$self $(,$arg:$ty)*) -> $ret { $self.read().unwrap().$fn($($arg),*) })*
|
|
//}
|
|
});
|
|
|
|
/// Define a trait an implement it for various mutation-enabled 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 } };);
|