Skip to content

Commit

Permalink
1. Minor bugs have been fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
denisandroid committed Sep 4, 2022
1 parent 5ef1553 commit eb22b13
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 42 deletions.
40 changes: 25 additions & 15 deletions src/beh/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@ extern crate tokio;
pub use tokio::sync::Mutex;
pub use tokio::sync::MutexGuard;
use crate::core::SyncPointBeh;
use crate::cfg_not_async;
use crate::cfg_async;
use crate::cfg_not_only_async;
use crate::cfg_only_async;

cfg_async! {
cfg_only_async! {
/// A macro that determines whether to add asynchronous fn support for traits.
macro_rules! async_or_sync_impltraitcode {
[
$(#[$($addmeta:tt)*])*
impl[$($left:tt)*] $name_trait: ident for $impl_ty: ty {
#async { $($async_code:tt)* }
#sync { $($sync_code:tt)* }
impl $([$($left:tt)*])? $name_trait: ident for $impl_ty: ty {
$(#[$doc_hide0:meta])* // doc hidden
#only_async { $($async_code:tt)* }
$(#[$doc_hide1:meta])* // doc hidden
#only_sync { $($sync_code:tt)* }

$($code:tt)+
}
Expand All @@ -27,27 +30,30 @@ cfg_async! {

$(#[$($addmeta)*])*
#[async_trait]
impl<$($left)*> $name_trait for $impl_ty {
impl $(<$($left)*>)? $name_trait for $impl_ty {
$($async_code)*

$($code)+
}
};
}
}
cfg_not_async! {
cfg_not_only_async! {
/// A macro that determines whether to add asynchronous fn support for traits.
macro_rules! async_or_sync_impltraitcode {
[
$(#[$($addmeta:tt)*])*
impl[$($left:tt)*] $name_trait: ident for $impl_ty: ty {
#async { $($async_code:tt)* }
#sync { $($sync_code:tt)* }
impl $([$($left:tt)*])? $name_trait: ident for $impl_ty: ty {
$(#[$doc_hide0:meta])* // doc hidden
#only_async { $($async_code:tt)* }
$(#[$doc_hide1:meta])* // doc hidden
#only_sync { $($sync_code:tt)* }

$($code:tt)+
}
] => {
$(#[$($addmeta)*])*
impl<$($left)*> $name_trait for $impl_ty {
impl $(<$($left)*>)? $name_trait for $impl_ty {
$($sync_code)*

$($code)+
Expand All @@ -58,7 +64,9 @@ cfg_not_async! {

async_or_sync_impltraitcode! {
impl['a, T: Send] SyncPointBeh for &'a Mutex<T> {
#async {
/// This section of code is connected only if
/// the current library is asynchronous.
#only_async {
#[inline(always)]
async fn new_lock(&self) -> Self::LockType {
Mutex::lock(self).await
Expand All @@ -77,7 +85,9 @@ async_or_sync_impltraitcode! {
drop(lock_type)
}
}
#sync {
/// This section of code is connected only if
/// the current library is synchronous.
#only_sync {
#[inline(always)]
fn new_lock(&self) -> Self::LockType {
unimplemented!();
Expand Down Expand Up @@ -123,7 +133,7 @@ macro_rules! __synchronized_beh {
{
// Definition of the current implementation
#name
} => { "async(tokio+parking_lot)" };
} => { "async(tokio+parking_lot+async_trait)" };

{
// Defining a new synchronization point, usually implements static
Expand Down
39 changes: 24 additions & 15 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ use core::marker::PhantomData;

use core::ops::Deref;
use core::ops::DerefMut;
use crate::cfg_async;
use crate::cfg_not_async;
use crate::cfg_only_async;
use crate::cfg_not_only_async;

cfg_async! {
cfg_only_async! {
/// A macro that determines whether to add asynchronous fn support for traits.
macro_rules! async_or_sync_newtraitcode {
[
$(#[$($addmeta:tt)*])*
pub trait $name_trait: ident {
#async { $($async_code:tt)* }
#sync { $($sync_code:tt)* }
$(#[$doc_hide0:meta])* // doc hidden
#only_async { $($async_code:tt)* }
$(#[$doc_hide1:meta])* // doc hidden
#only_sync { $($sync_code:tt)* }

$($code:tt)+
}
Expand All @@ -26,27 +29,30 @@ cfg_async! {
$(#[$($addmeta)*])*
#[async_trait]
pub trait $name_trait {
$($async_code)*
$($async_code)* // < -- ONLY ASYNC CODE

$($code)+
}
};
}
}
cfg_not_async! {
cfg_not_only_async! {
/// A macro that determines whether to add asynchronous fn support for traits.
macro_rules! async_or_sync_newtraitcode {
[
$(#[$($addmeta:tt)*])*
pub trait $name_trait: ident {
#async { $($async_code:tt)* }
#sync { $($sync_code:tt)* }
$(#[$doc_hide0:meta])* // doc hidden
#only_async { $($async_code:tt)* }
$(#[$doc_hide1:meta])* // doc hidden
#only_sync { $($sync_code:tt)* }

$($code:tt)+
}
] => {
$(#[$($addmeta)*])*
pub trait $name_trait {
$($sync_code)*
$($sync_code)* // < -- ONLY SYNC CODE

$($code)+
}
Expand All @@ -57,7 +63,9 @@ cfg_not_async! {
async_or_sync_newtraitcode! {
/// Implementation of the behavior for the used synchronization structure.
pub trait SyncPointBeh {
#async {
/// This section of code is connected only if
/// the current library is asynchronous.
#only_async {
/// Create a new hold lock.
async fn new_lock(&self) -> Self::LockType;

Expand All @@ -69,7 +77,9 @@ async_or_sync_newtraitcode! {
/// the lock (usually always involves just a drop)
async fn unlock(&self, lock_type: Self::LockType);
}
#sync {
/// This section of code is connected only if
/// the current library is synchronous.
#only_sync {
/// Create a new hold lock.
fn new_lock(&self) -> Self::LockType;

Expand Down Expand Up @@ -143,14 +153,13 @@ impl<T, N> SyncPoint<T, N> where T: SyncPointBeh {
}
}

cfg_not_async! {
cfg_not_only_async! {
/// Create a new hold lock.
#[inline(always)]
pub fn new_lock(&self) -> T::LockType {
T::new_lock(&self.mutex_builder)
}


/// If the lock exists and is not released, then return None,
/// if there is no lock, then create it and return Some.
#[inline(always)]
Expand All @@ -166,7 +175,7 @@ impl<T, N> SyncPoint<T, N> where T: SyncPointBeh {
}
}

cfg_async! {
cfg_only_async! {
/// Create a new hold lock.
#[inline(always)]
pub async fn new_lock(&self) -> T::LockType {
Expand Down
17 changes: 14 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ pub mod beh {

#[cfg_attr(docsrs, doc(cfg( feature = "async" )))]
#[cfg( feature = "async" )]
//cfg_async! {
//cfg_only_async! {
pub mod r#async;
//}

Expand Down Expand Up @@ -473,10 +473,9 @@ macro_rules! synchronized {

/// Describes the selected default lock for the `synchronized` macro. Currently it is `
#[doc = crate::__synchronized_beh!( #name )]
/// ` by default.
/// `.
pub const CURRENT_DEF_BEH: &'static str = crate::__synchronized_beh!( #name );


/// Whether `get_point_name` was enabled in this build.
///
/// The `get_point_name` feature determines whether the connection
Expand Down Expand Up @@ -504,3 +503,15 @@ pub const IS_SYNC_POINT_SUPPORT: bool = {
true
}
};

cfg_only_async! {
/// Determines if the library code and its locks are fully asynchronous.
/// Currently it is `true`.
pub const IS_ALWAYS_ASYNC: bool = true;
}

cfg_not_only_async! {
/// Determines if the library code and its locks are fully asynchronous.
/// Currently it is `false`.
pub const IS_ALWAYS_ASYNC: bool = false;
}
22 changes: 13 additions & 9 deletions src/macro_features.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

// # cfg_async
//! Additional macros for code generation depending on the `features` used.
// # cfg_only_async

/// Code is passed from the macro only if the `async` function is clearly defined.
#[doc(hidden)]
#[macro_export]
#[cfg(all(
Expand All @@ -9,13 +12,14 @@
not(feature = "parking_lot"),
not(feature = "std"),
))]
macro_rules! cfg_async {
macro_rules! cfg_only_async {
[ $($code:tt)+ ] => {
#[cfg_attr(docsrs, doc(cfg( feature = "async" )))]
$($code)+
}
}

/// Code is passed from the macro only if the `async` function is clearly defined.
#[doc(hidden)]
#[macro_export]
#[cfg(not(all(
Expand All @@ -24,12 +28,13 @@ macro_rules! cfg_async {
not(feature = "parking_lot"),
not(feature = "std"),
)))]
macro_rules! cfg_async {
macro_rules! cfg_only_async {
[ $($code:tt)+ ] => {}
}

// # cfg_not_async
// # cfg_not_only_async

/// Code is not passed from a macro only if the asynchronous function is clearly defined.
#[doc(hidden)]
#[macro_export]
#[cfg(all(
Expand All @@ -38,12 +43,11 @@ macro_rules! cfg_async {
not(feature = "parking_lot"),
not(feature = "std"),
))]
macro_rules! cfg_not_async {
[ $($code:tt)+ ] => {

}
macro_rules! cfg_not_only_async {
[ $($code:tt)+ ] => {}
}

/// Code is not passed from a macro only if the asynchronous function is clearly defined.
#[doc(hidden)]
#[macro_export]
#[cfg(not(all(
Expand All @@ -52,7 +56,7 @@ macro_rules! cfg_not_async {
not(feature = "parking_lot"),
not(feature = "std"),
)))]
macro_rules! cfg_not_async {
macro_rules! cfg_not_only_async {
[ $($code:tt)+ ] => {
#[cfg_attr(docsrs, doc(cfg( not(feature = "async") )))]
$($code)+
Expand Down

0 comments on commit eb22b13

Please sign in to comment.