diff --git a/README.md b/README.md index b17fb68..ae5b95a 100644 --- a/README.md +++ b/README.md @@ -177,7 +177,7 @@ fn main() { let result0 = synchronized! ((->COMB_SYNC) { println!("SyncCode, name_point: {}", COMB_SYNC.get_sync_point_name()); unsafe { - POINT+= 1; + POINT += 1; POINT } @@ -195,7 +195,7 @@ fn main() { let result1 = synchronized! ((->COMB_SYNC) { println!("SyncCode, name_point: {}", COMB_SYNC.get_sync_point_name()); unsafe { - POINT+= 1; + POINT += 1; POINT } diff --git a/examples/point.rs b/examples/point.rs index 4ce3b37..bfd8d0f 100644 --- a/examples/point.rs +++ b/examples/point.rs @@ -26,7 +26,7 @@ fn main() { let result0 = synchronized! ((->COMB_SYNC) { println!("SyncCode, name_point: {}", COMB_SYNC.get_sync_point_name()); unsafe { - POINT+= 1; + POINT += 1; POINT } @@ -44,7 +44,7 @@ fn main() { let result1 = synchronized! ((->COMB_SYNC) { println!("SyncCode, name_point: {}", COMB_SYNC.get_sync_point_name()); unsafe { - POINT+= 1; + POINT += 1; POINT } diff --git a/examples/point_let.rs b/examples/point_let.rs index 409fafb..55f2bc1 100644 --- a/examples/point_let.rs +++ b/examples/point_let.rs @@ -24,7 +24,7 @@ fn main() { let result0 = synchronized! ((->COMB_SYNC) { println!("SyncCode, name_point: {}", COMB_SYNC.get_sync_point_name()); unsafe { - POINT+= 1; + POINT += 1; POINT } @@ -51,7 +51,7 @@ fn main() { let result2 = synchronized! ((->COMB_SYNC) { println!("SyncCode, name_point: {}", COMB_SYNC.get_sync_point_name()); unsafe { - POINT+= 1; + POINT += 1; POINT } diff --git a/src/beh/pl.rs b/src/beh/pl.rs index 9bb927f..440e2cc 100644 --- a/src/beh/pl.rs +++ b/src/beh/pl.rs @@ -61,7 +61,7 @@ macro_rules! __synchronized_beh { // Defining a new synchronization point, usually implements static variables used during synchronization. { #new_point<$t: ty : [$t_make:expr]>: $v_point_name:ident } => { - $crate::__make_name!( _HIDDEN_NAME -> stringify!($v_point_name) ); + $crate::__make_name!( #new_name<_HIDDEN_NAME>: stringify!($v_point_name) ); #[allow(dead_code)] static CONST_MUTEX: $crate::beh::pl::Mutex<$t> = $crate::beh::pl::const_mutex( @@ -72,7 +72,8 @@ macro_rules! __synchronized_beh { #[allow(dead_code)] #[allow(non_upper_case_globals)] pub static $v_point_name: $crate::core::SyncPoint< - &'static $crate::beh::pl::Mutex<$t>, _HIDDEN_NAME + &'static $crate::beh::pl::Mutex<$t>, + $crate::__make_name!(#get_name<_HIDDEN_NAME>) > = $crate::core::SyncPoint::new(&CONST_MUTEX); }; // Creates a new lock on an already created sync point (#new_point) diff --git a/src/beh/std.rs b/src/beh/std.rs index c799ff5..c52e409 100644 --- a/src/beh/std.rs +++ b/src/beh/std.rs @@ -66,7 +66,7 @@ macro_rules! __synchronized_beh { // Defining a new synchronization point, usually implements static variables used during synchronization. { #new_point<$t: ty : [$t_make:expr]>: $v_point_name:ident } => { - $crate::__make_name!( _HIDDEN_NAME -> stringify!($v_point_name) ); + $crate::__make_name!( #new_name<_HIDDEN_NAME>: stringify!($v_point_name) ); #[allow(dead_code)] static CONST_MUTEX: $crate::beh::std::Mutex<$t> = $crate::beh::std::Mutex::new( @@ -77,7 +77,8 @@ macro_rules! __synchronized_beh { #[allow(dead_code)] #[allow(non_upper_case_globals)] pub static $v_point_name: $crate::core::SyncPoint< - &'static $crate::beh::std::Mutex<$t>, _HIDDEN_NAME + &'static $crate::beh::std::Mutex<$t>, + $crate::__make_name!(#get_name<_HIDDEN_NAME>) > = $crate::core::SyncPoint::new(&CONST_MUTEX); }; // Creates a new lock on an already created sync point (#new_point) diff --git a/src/core.rs b/src/core.rs index f3eeba3..2114990 100644 --- a/src/core.rs +++ b/src/core.rs @@ -57,7 +57,7 @@ pub struct SyncPoint { mutex_builder: T, /// The phantom used to implement `SyncPointName`. - _pp2: PhantomData, + phantom_name: PhantomData, } impl SyncPoint where T: SyncPointBeh { @@ -67,7 +67,7 @@ impl SyncPoint where T: SyncPointBeh { Self { mutex_builder, - _pp2: PhantomData, + phantom_name: PhantomData, } } @@ -102,31 +102,48 @@ impl SyncPoint where T: SyncPointBeh { #[cfg_attr(docsrs, doc(cfg(feature = "get_point_name")))] #[cfg( feature = "get_point_name" )] -impl SyncPoint where T: SyncPointBeh, N: SyncPointName { +impl SyncPoint where N: SyncPointName { /// Getting the sync point name - #[cfg_attr(docsrs, doc(cfg(feature = "get_point_name")))] - #[cfg( feature = "get_point_name" )] #[inline(always)] pub const fn get_sync_point_name(&self) -> &'static str { N::NAME } /// Getting the sync point name - #[cfg_attr(docsrs, doc(cfg(feature = "get_point_name")))] - #[cfg( feature = "get_point_name" )] #[inline(always)] pub const fn get_name() -> &'static str { N::NAME } } +#[cfg( not(feature = "get_point_name") )] +impl SyncPoint { + /// Getting the sync point name + /// + /// Warning since `get_point_name` is disabled, + /// "" will always be returned. + #[inline(always)] + pub const fn get_sync_point_name(&self) -> &'static str { + "" + } + + /// Getting the sync point name + /// + /// Warning since `get_point_name` is disabled, + /// "" will always be returned. + #[inline(always)] + pub const fn get_name() -> &'static str { + "" + } +} + /// Generic macro that generates and implements a `SyncPointName`. #[cfg_attr(docsrs, doc(cfg(feature = "get_point_name")))] #[cfg( feature = "get_point_name" )] #[macro_export] #[doc(hidden)] macro_rules! __make_name { - [ $name:ident -> $expr:expr $(; $($unk:tt)*)? ] => { + [ #new_name<$name:ident>: $expr:expr $(; $($unk:tt)*)? ] => { /// An automatically generated enum for /// the type-based implementation of SyncPointName. pub enum $name {} @@ -142,6 +159,8 @@ macro_rules! __make_name { )? }; + [ #get_name<$name:ident> ] => { $name }; + [] => {} } @@ -151,11 +170,15 @@ macro_rules! __make_name { #[macro_export] #[doc(hidden)] macro_rules! __make_name { - [ $name:ident -> $expr:expr $(; $($unk:tt)*)? ] => { - /// An automatically generated enum for - /// the type-based implementation of SyncPointName. - /// (Just a stub.) - pub enum $name {} + // The `get_point_name` function is disabled, + // only a stub is used. + [ #new_name<$name:ident>: $expr:expr $(; $($unk:tt)*)? ] => {}; + + // The `get_point_name` function is disabled, + // only a stub is used. + [ #get_name<$name:ident> ] => { + () }; + [] => {} } diff --git a/src/lib.rs b/src/lib.rs index 4f1291f..a2e2641 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -190,7 +190,7 @@ fn main() { let result0 = synchronized! ((->COMB_SYNC) { println!("SyncCode, name_point: {}", COMB_SYNC.get_sync_point_name()); unsafe { - POINT+= 1; + POINT += 1; POINT } @@ -208,7 +208,7 @@ fn main() { let result1 = synchronized! ((->COMB_SYNC) { println!("SyncCode, name_point: {}", COMB_SYNC.get_sync_point_name()); unsafe { - POINT+= 1; + POINT += 1; POINT } @@ -306,7 +306,13 @@ pub mod beh { /// ``` #[macro_export] macro_rules! synchronized { - { ->$sync_point_name:ident ($v_point_name: ident) $($all:tt)* } => {{ // synchronized point + { + // Named `$sync_point_name` synchronized block with mutable value + // of synchronized name `$v_point_name`, type `$ty` and value when + // `$expr` is created. + // (Use only with `synchronized_point`.) + ->$sync_point_name:ident ($v_point_name: ident) $($all:tt)* + } => {{ // synchronized point $crate::__synchronized_beh!(#new_lock(__lock): $sync_point_name); let ref mut $v_point_name = *__lock; @@ -320,30 +326,65 @@ macro_rules! synchronized { result }}; - { $sync_point_name:ident ($v_point_name: ident: $ty: ty = $expr:expr ) $($all:tt)* } => {{ // synchronized point + { + // Named `$sync_point_name` synchronized block with mutable value + // of synchronized name `$v_point_name`, type `$ty` and value when + // `$expr` is created. + $sync_point_name:ident ($v_point_name: ident: $ty: ty = $expr:expr ) $($all:tt)* + } => {{ // synchronized point $crate::__synchronized_beh!(#new_point<$ty: [$expr]>: $sync_point_name); $crate::synchronized! { ->$sync_point_name ($v_point_name) $($all)* } }}; - { (->$v_point_name: ident) $($all:tt)* } => {{ // sync point + { + // Named sync block named `$v_point_name`. + // (Use only with `synchronized_point`.) + (->$v_point_name: ident) $($all:tt)* + } => {{ // sync point $crate::synchronized! { ->$v_point_name (__empty_value) $($all)* } }}; - { ($v_point_name: ident) $($all:tt)* } => {{ + { + // Named sync block named `$v_point_name`. + ($v_point_name: ident) $($all:tt)* + } => {{ $crate::synchronized! { $v_point_name (__empty_value: () = ()) $($all)* } }}; - { ( $v_point_name: ident: $ty: ty = $expr:expr ) $($all:tt)* } => {{ // sync value + { + // Anonymous synchronized block with mutable synchronized name value + // `$v_point_name`, type `$ty` and value when `$expr` is created. + ( $v_point_name: ident: $ty: ty = $expr:expr ) $($all:tt)* + } => {{ // sync value $crate::synchronized! { __PL_SYNC_POINT ($v_point_name: $ty = $expr) $($all)* } }}; - { $($all:tt)* } => {{ // nohead synchronized block + + { + // COMPILE_ERROR + $(->$_ident1:ident)? /* OR */ $($_ident2:ident)? ($($unk_in:tt)*) $($unk:tt)+ + } => { + compile_error!(concat!( + "Error writing macro `synchronized`, incode: ", + $(stringify!(->$_ident1),)? + $(stringify!($_ident2),)? + + stringify!(($($unk_in)*)), + + stringify!($($unk)+), + )); + }; + + { + // Anonymous synchronized block + $($all:tt)* + } => {{ // nohead synchronized block $crate::synchronized! { __PL_SYNC_POINT (__empty_value: () = ()) $($all)* } @@ -368,7 +409,7 @@ macro_rules! synchronized { /// let result0 = synchronized! ((->COMB_SYNC) { /// println!("SyncCode, name_point: {}", COMB_SYNC.get_sync_point_name()); /// unsafe { -/// POINT+= 1; +/// POINT += 1; /// /// POINT /// } @@ -381,7 +422,7 @@ macro_rules! synchronized { /// let result1 = synchronized! ((->COMB_SYNC) { /// println!("SyncCode, name_point: {}", COMB_SYNC.get_sync_point_name()); /// unsafe { -/// POINT+= 1; +/// POINT += 1; /// /// POINT /// } @@ -401,7 +442,7 @@ macro_rules! synchronized { /// let result0 = synchronized! ((->COMB_SYNC) { /// println!("SyncCode, name_point: {}", COMB_SYNC.get_sync_point_name()); /// unsafe { -/// POINT+= 1; +/// POINT += 1; /// /// POINT /// } @@ -423,7 +464,7 @@ macro_rules! synchronized { /// let result2 = synchronized! ((->COMB_SYNC) { /// println!("SyncCode, name_point: {}", COMB_SYNC.get_sync_point_name()); /// unsafe { -/// POINT+= 1; +/// POINT += 1; /// /// POINT /// } @@ -431,7 +472,13 @@ macro_rules! synchronized { /// }} #[macro_export] macro_rules! synchronized_point { - { $sync_point_name:ident ($ty: ty = $expr:expr ) {$($all:tt)*} $(; $($unk:tt)*)? } => { + { + // Named sync point named `$sync_point_name`. + // + // With a mutable synchronized variable of type `$ty` + // with a default value of `$expr`. + $sync_point_name:ident ($ty: ty = $expr:expr ) {$($all:tt)*} $(; $($unk:tt)*)? + } => { { $crate::__synchronized_beh!(#new_point<$ty: [$expr]>: $sync_point_name); @@ -442,7 +489,10 @@ macro_rules! synchronized_point { $($unk)* })? }; - { ($sync_point_name:ident) {$($all:tt)*} $(; $($unk:tt)*)? } => { + { + // Named sync point named `$sync_point_name` + ($sync_point_name:ident) {$($all:tt)*} $(; $($unk:tt)*)? + } => { $crate::synchronized_point! { $sync_point_name (() = ()) { $($all)* } @@ -450,10 +500,35 @@ macro_rules! synchronized_point { } }; + { + // COMPILE_ERROR + + $($unk:tt)+ + } => { + compile_error!(concat!( + "Error writing macro `synchronized_point`, incode: ", + stringify!($($unk)+), + )); + }; + [] => {} } /// Describes the selected default lock for the `synchronized` macro. Currently it is ` -#[doc = crate::__synchronized_beh!{ #name }] +#[doc = crate::__synchronized_beh!( #name )] /// ` by default. -pub const CURRENT_DEF_BEH: &'static str = crate::__synchronized_beh!{ #name }; +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 +/// label name can be determined at run time. +pub const IS_GET_POINT_NAME_SUPPORT: bool = { + #[cfg( not(feature = "get_point_name") )] { + false + } + + #[cfg( feature = "get_point_name" )] { + true + } +};