From eb22b13a0e6cdc803a1eee0531adb47e0607cbe2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B5=D0=BD=D0=B8=D1=81=20=D0=9A=D0=BE=D1=82=D0=BB?= =?UTF-8?q?=D1=8F=D1=80=D0=BE=D0=B2?= Date: Sun, 4 Sep 2022 19:45:59 +0300 Subject: [PATCH] 1. Minor bugs have been fixed --- src/beh/async.rs | 40 +++++++++++++++++++++++++--------------- src/core.rs | 39 ++++++++++++++++++++++++--------------- src/lib.rs | 17 ++++++++++++++--- src/macro_features.rs | 22 +++++++++++++--------- 4 files changed, 76 insertions(+), 42 deletions(-) diff --git a/src/beh/async.rs b/src/beh/async.rs index 2c9a907..556721b 100644 --- a/src/beh/async.rs +++ b/src/beh/async.rs @@ -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)+ } @@ -27,7 +30,7 @@ cfg_async! { $(#[$($addmeta)*])* #[async_trait] - impl<$($left)*> $name_trait for $impl_ty { + impl $(<$($left)*>)? $name_trait for $impl_ty { $($async_code)* $($code)+ @@ -35,19 +38,22 @@ cfg_async! { }; } } -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)+ @@ -58,7 +64,9 @@ cfg_not_async! { async_or_sync_impltraitcode! { impl['a, T: Send] SyncPointBeh for &'a Mutex { - #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 @@ -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!(); @@ -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 diff --git a/src/core.rs b/src/core.rs index bec7e33..2bd6932 100644 --- a/src/core.rs +++ b/src/core.rs @@ -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)+ } @@ -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)+ } @@ -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; @@ -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; @@ -143,14 +153,13 @@ impl SyncPoint 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)] @@ -166,7 +175,7 @@ impl SyncPoint where T: SyncPointBeh { } } - cfg_async! { + cfg_only_async! { /// Create a new hold lock. #[inline(always)] pub async fn new_lock(&self) -> T::LockType { diff --git a/src/lib.rs b/src/lib.rs index 3e4138e..fa709c3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; //} @@ -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 @@ -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; +} diff --git a/src/macro_features.rs b/src/macro_features.rs index 18169e1..5e9b5c5 100644 --- a/src/macro_features.rs +++ b/src/macro_features.rs @@ -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( @@ -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( @@ -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( @@ -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( @@ -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)+