From be1189a6a4e60642f4dbe697b5c7b08534d13213 Mon Sep 17 00:00:00 2001 From: hanepjiv Date: Sat, 30 Nov 2024 23:45:16 +0900 Subject: [PATCH] clippy::all,correctness,suspicious,style,complexity,perf,pedantic,nursery,cargo --- elicit_macro/src/aelicit_derive.rs | 22 +- elicit_macro/src/elicit_define.rs | 257 +++++++------- elicit_macro/src/elicit_derive.rs | 38 +- elicit_macro/src/feature_default.rs | 6 +- .../src/feature_default/aelicit_define.rs | 331 +++++++++--------- .../src/feature_default/melicit_define.rs | 315 +++++++++-------- elicit_macro/src/feature_parking_lot.rs | 6 +- .../src/feature_parking_lot/aelicit_define.rs | 331 +++++++++--------- .../src/feature_parking_lot/melicit_define.rs | 313 +++++++++-------- elicit_macro/src/find_field_attr.rs | 2 +- elicit_macro/src/lib.rs | 14 +- elicit_macro/src/melicit_derive.rs | 38 +- examples/aelicit.rs | 14 +- examples/elicit.rs | 10 +- examples/melicit.rs | 14 +- src/error.rs | 18 +- 16 files changed, 865 insertions(+), 864 deletions(-) diff --git a/elicit_macro/src/aelicit_derive.rs b/elicit_macro/src/aelicit_derive.rs index b304286..8c23d12 100644 --- a/elicit_macro/src/aelicit_derive.rs +++ b/elicit_macro/src/aelicit_derive.rs @@ -18,7 +18,7 @@ use crate::find_field_attr::find_field_attr; // //////////////////////////////////////////////////////////////////////////// // ============================================================================ /// fn expand -pub(crate) fn expand(ast: DeriveInput) -> Result { +pub fn expand(ast: DeriveInput) -> Result { let mut aelicit_mod_author = Option::::default(); let mut aelicit_from_self_field = Option::::default(); @@ -61,23 +61,21 @@ struct Derived {} } let ident = ast.ident; - let aelicit_impl = if let Some(ref x) = aelicit_from_self_field { - quote! {self.#x.aelicit_from_self()} - } else { - quote! { None } - }; - let _weak_assign_impl = if let Some(ref x) = aelicit_from_self_field { - quote! {self.#x._weak_assign(_weak)} - } else { - quote! { Ok(()) } - }; + let aelicit_impl = aelicit_from_self_field.as_ref().map_or_else( + || quote! { None }, + |x| quote! {self.#x.aelicit_from_self()}, + ); + let _weak_assign_impl = aelicit_from_self_field.as_ref().map_or_else( + || quote! { Ok(()) }, + |x| quote! {self.#x._weak_assign(_weak)}, + ); Ok(quote! { #[automatically_derived] impl #aelicit_mod_author :: AelicitFromSelf for #ident { fn aelicit_from_self(&self) -> Option<#aelicit_mod_author :: Aelicit> { - #aelicit_impl + #aelicit_impl } } diff --git a/elicit_macro/src/elicit_define.rs b/elicit_macro/src/elicit_define.rs index 62e58f6..53fc77c 100644 --- a/elicit_macro/src/elicit_define.rs +++ b/elicit_macro/src/elicit_define.rs @@ -16,10 +16,7 @@ use crate::include::{ // //////////////////////////////////////////////////////////////////////////// // ============================================================================ /// fn expand -pub(crate) fn expand( - mod_ident: &Ident, - item: ItemTrait, -) -> Result { +pub fn expand(mod_ident: &Ident, item: ItemTrait) -> Result { let define = quote_define(mod_ident, &item)?; let mut ret = item.into_token_stream(); ret.extend(define); @@ -33,29 +30,29 @@ fn quote_define(mod_ident: &Ident, item: &ItemTrait) -> Result { Ok(quote! { #[allow(box_pointers)] mod #mod_ident { - mod _inner { #inner } + mod _inner { #inner } - mod _common { - pub use super::_inner::{ - Elicit, ElicitBase, ElicitFromSelf, - }; - } + mod _common { + pub use super::_inner::{ + Elicit, ElicitBase, ElicitFromSelf, + }; + } - /// mod author - pub mod author { - pub use super::_common::*; - pub use super::_inner::{ - WeakAssign, - WeakElicitInner, - ElicitFromSelfField - }; - } + /// mod author + pub mod author { + pub use super::_common::*; + pub use super::_inner::{ + WeakAssign, + WeakElicitInner, + ElicitFromSelfField + }; + } - /// mod user - pub mod user { - pub use super::_common::*; - pub use super::_inner::WeakElicit; - } + /// mod user + pub mod user { + pub use super::_common::*; + pub use super::_inner::WeakElicit; + } } }) } @@ -68,13 +65,13 @@ fn quote_inner(a_orig: &Ident) -> Result { // //////////////////////////////////////////////////////////////////// // ==================================================================== use std::{ - cell::{OnceCell, RefCell}, - convert::From, - fmt::Debug, - marker::Unpin, - pin::Pin, - rc::{Rc, Weak}, - result::Result as StdResult, + cell::{OnceCell, RefCell}, + convert::From, + fmt::Debug, + marker::Unpin, + pin::Pin, + rc::{Rc, Weak}, + result::Result as StdResult, }; // -------------------------------------------------------------------- pub use elicit::{ Result as ElicitResult, Error as ElicitError }; @@ -82,21 +79,21 @@ fn quote_inner(a_orig: &Ident) -> Result { // ==================================================================== /// trait ElicitBase pub trait ElicitBase: 'static + Unpin + Debug - + #orig + ElicitFromSelf + WeakAssign + + #orig + ElicitFromSelf + WeakAssign { - // ================================================================ - /// usizeptr - fn usizeptr(&self) -> usize; + // ================================================================ + /// usizeptr + fn usizeptr(&self) -> usize; } // ==================================================================== impl - ElicitBase for T + ElicitBase for T { - // ================================================================ - #[allow(trivial_casts)] - fn usizeptr(&self) -> usize { - self as *const _ as usize - } + // ================================================================ + #[allow(trivial_casts)] + fn usizeptr(&self) -> usize { + self as *const _ as usize + } } // //////////////////////////////////////////////////////////////////// // ==================================================================== @@ -118,123 +115,123 @@ fn quote_inner(a_orig: &Ident) -> Result { pub struct WeakElicit(WeakElicitInner); // ==================================================================== impl WeakElicit { - // ================================================================ - /// fn upgrade - pub fn upgrade(&self) -> Option { - self.0.upgrade().map(Elicit) - } + // ================================================================ + /// fn upgrade + pub fn upgrade(&self) -> Option { + self.0.upgrade().map(Elicit) + } } // ==================================================================== impl From for WeakElicit { - fn from(x: Elicit) -> WeakElicit { - x.weak() - } + fn from(x: Elicit) -> WeakElicit { + x.weak() + } } // //////////////////////////////////////////////////////////////////// // ==================================================================== /// trait ElicitFromSelf pub trait ElicitFromSelf { - /// elicit_from_self - fn elicit_from_self(&self) -> Option; + /// elicit_from_self + fn elicit_from_self(&self) -> Option; } // ==================================================================== /// trait WeakAssign pub trait WeakAssign { - /// _weak_assign - fn _weak_assign( - &mut self, - weak: WeakElicitInner, - ) -> ElicitResult<()>; + /// _weak_assign + fn _weak_assign( + &mut self, + weak: WeakElicitInner, + ) -> ElicitResult<()>; } // //////////////////////////////////////////////////////////////////// // ==================================================================== /// struct ElicitFromSelfField #[derive(Debug, Clone, Default)] pub struct ElicitFromSelfField { - /// _weak - _weak: OnceCell, + /// _weak + _weak: OnceCell, } // ==================================================================== impl ElicitFromSelf for ElicitFromSelfField { - fn elicit_from_self(&self) -> Option { - self._weak.get()?.upgrade().map(Elicit) - } + fn elicit_from_self(&self) -> Option { + self._weak.get()?.upgrade().map(Elicit) + } } // ==================================================================== impl WeakAssign for ElicitFromSelfField { - fn _weak_assign( - &mut self, - weak: WeakElicitInner, - ) -> ElicitResult<()> { - self._weak.set(weak).map_err( - |_| ElicitError::WeakAlreadyExists) - } + fn _weak_assign( + &mut self, + weak: WeakElicitInner, + ) -> ElicitResult<()> { + self._weak.set(weak).map_err( + |_| ElicitError::WeakAlreadyExists) + } } // //////////////////////////////////////////////////////////////////// // ==================================================================== impl Elicit { - // ================================================================ - /// new - #[allow(trivial_casts)] - pub fn new(val: T) -> ElicitResult - where - T: ElicitBase, - { - let r = Rc::new(RefCell::new(Box::pin(val) as RefCellInner)); - r.borrow_mut().as_mut()._weak_assign(Rc::<_>::downgrade(&r))?; - Ok(Elicit(r)) - } - // ================================================================ - /// weak - pub fn weak(&self) -> WeakElicit { - WeakElicit(Rc::downgrade(&self.0)) - } - // ================================================================ - /// usizeptr - pub fn usizeptr(&self) -> usize { - self.0.borrow().as_ref().usizeptr() - } - // ================================================================ - /// with - pub fn with( - &self, - f: impl FnOnce(&dyn ElicitBase) -> StdResult, - ) -> StdResult - { - f(& *self.0.borrow().as_ref()) - } - // ================================================================ - /// try_with - pub fn try_with( - &self, - f: impl FnOnce(&dyn ElicitBase) -> StdResult, - ) -> StdResult - where - E: From - { - f(& *self.0.try_borrow().map_err(ElicitError::from)?.as_ref()) - } - // ================================================================ - /// with_mut - pub fn with_mut( - &self, - f: impl FnOnce(&mut dyn ElicitBase) -> StdResult, - ) -> StdResult - { - f(&mut *self.0.borrow_mut().as_mut()) - } - // ================================================================ - /// try_with_mut - pub fn try_with_mut( - &self, - f: impl FnOnce(&mut dyn ElicitBase) -> StdResult, - ) -> StdResult - where - E: From, - { - f(&mut *self.0.try_borrow_mut().map_err(ElicitError::from)? - .as_mut()) - } + // ================================================================ + /// new + #[allow(trivial_casts)] + pub fn new(val: T) -> ElicitResult + where + T: ElicitBase, + { + let r = Rc::new(RefCell::new(Box::pin(val) as RefCellInner)); + r.borrow_mut().as_mut()._weak_assign(Rc::<_>::downgrade(&r))?; + Ok(Elicit(r)) + } + // ================================================================ + /// weak + pub fn weak(&self) -> WeakElicit { + WeakElicit(Rc::downgrade(&self.0)) + } + // ================================================================ + /// usizeptr + pub fn usizeptr(&self) -> usize { + self.0.borrow().as_ref().usizeptr() + } + // ================================================================ + /// with + pub fn with( + &self, + f: impl FnOnce(&dyn ElicitBase) -> StdResult, + ) -> StdResult + { + f(& *self.0.borrow().as_ref()) + } + // ================================================================ + /// try_with + pub fn try_with( + &self, + f: impl FnOnce(&dyn ElicitBase) -> StdResult, + ) -> StdResult + where + E: From + { + f(& *self.0.try_borrow().map_err(ElicitError::from)?.as_ref()) + } + // ================================================================ + /// with_mut + pub fn with_mut( + &self, + f: impl FnOnce(&mut dyn ElicitBase) -> StdResult, + ) -> StdResult + { + f(&mut *self.0.borrow_mut().as_mut()) + } + // ================================================================ + /// try_with_mut + pub fn try_with_mut( + &self, + f: impl FnOnce(&mut dyn ElicitBase) -> StdResult, + ) -> StdResult + where + E: From, + { + f(&mut *self.0.try_borrow_mut().map_err(ElicitError::from)? + .as_mut()) + } } }) } diff --git a/elicit_macro/src/elicit_derive.rs b/elicit_macro/src/elicit_derive.rs index a42a496..6a79962 100644 --- a/elicit_macro/src/elicit_derive.rs +++ b/elicit_macro/src/elicit_derive.rs @@ -6,17 +6,19 @@ // @author hanepjiv // @copyright The MIT License (MIT) / Apache License Version 2.0 // @since 2024/04/10 -// @date 2024/11/10 +// @date 2024/11/30 // //////////////////////////////////////////////////////////////////////////// // use ======================================================================= -use crate::include::{DeriveInput, Error, Ident, Result, Span, ToTokens, TokenStream2, quote}; +use crate::include::{ + quote, DeriveInput, Error, Ident, Result, Span, ToTokens, TokenStream2, +}; // ---------------------------------------------------------------------------- use crate::find_field_attr::find_field_attr; // //////////////////////////////////////////////////////////////////////////// // ============================================================================ /// fn expand -pub(crate) fn expand(ast: DeriveInput) -> Result { +pub fn expand(ast: DeriveInput) -> Result { let mut elicit_mod_author = Option::::default(); let mut elicit_from_self_field = Option::::default(); @@ -59,26 +61,32 @@ struct Derived{} } let ident = ast.ident; - let elicit_impl = if let Some(ref x) = elicit_from_self_field { quote! {self.#x.elicit_from_self()} } else { quote! { None } }; - let _weak_assign_impl = if let Some(ref x) = elicit_from_self_field { quote! {self.#x._weak_assign(_weak)} } else { quote! { Ok(()) } }; + let elicit_impl = elicit_from_self_field.as_ref().map_or_else( + || quote! { None }, + |x| quote! {self.#x.elicit_from_self()}, + ); + let _weak_assign_impl = elicit_from_self_field.as_ref().map_or_else( + || quote! { Ok(()) }, + |x| quote! {self.#x._weak_assign(_weak)}, + ); Ok(quote! { #[automatically_derived] impl #elicit_mod_author :: ElicitFromSelf for #ident { - fn elicit_from_self(&self) -> - Option<#elicit_mod_author :: Elicit> { - #elicit_impl - } + fn elicit_from_self(&self) -> + Option<#elicit_mod_author :: Elicit> { + #elicit_impl + } } #[automatically_derived] impl #elicit_mod_author :: WeakAssign for #ident { - fn _weak_assign( - &mut self, - _weak: #elicit_mod_author :: WeakElicitInner, - ) -> elicit::Result<()> { - #_weak_assign_impl - } + fn _weak_assign( + &mut self, + _weak: #elicit_mod_author :: WeakElicitInner, + ) -> elicit::Result<()> { + #_weak_assign_impl + } } }) } diff --git a/elicit_macro/src/feature_default.rs b/elicit_macro/src/feature_default.rs index 8539b53..be66aa2 100644 --- a/elicit_macro/src/feature_default.rs +++ b/elicit_macro/src/feature_default.rs @@ -6,9 +6,9 @@ // @author hanepjiv // @copyright The MIT License (MIT) / Apache License Version 2.0 // @since 2024/04/25 -// @date 2024/04/26 +// @date 2024/11/30 // //////////////////////////////////////////////////////////////////////////// // mod ======================================================================= -pub(crate) mod aelicit_define; -pub(crate) mod melicit_define; +pub mod aelicit_define; +pub mod melicit_define; diff --git a/elicit_macro/src/feature_default/aelicit_define.rs b/elicit_macro/src/feature_default/aelicit_define.rs index d83fb6d..f8bfbaa 100644 --- a/elicit_macro/src/feature_default/aelicit_define.rs +++ b/elicit_macro/src/feature_default/aelicit_define.rs @@ -16,10 +16,7 @@ use crate::include::{ // //////////////////////////////////////////////////////////////////////////// // ============================================================================ /// fn expand -pub(crate) fn expand( - mod_ident: &Ident, - item: ItemTrait, -) -> Result { +pub fn expand(mod_ident: &Ident, item: ItemTrait) -> Result { let define = quote_define(mod_ident, &item)?; let mut ret = item.into_token_stream(); ret.extend(define); @@ -33,34 +30,34 @@ fn quote_define(mod_ident: &Ident, item: &ItemTrait) -> Result { Ok(quote! { #[allow(box_pointers)] mod #mod_ident { - mod _inner { #inner } + mod _inner { #inner } - mod _common { - pub use super::_inner::{ - Aelicit, AelicitBase, AelicitFromSelf, - }; - } + mod _common { + pub use super::_inner::{ + Aelicit, AelicitBase, AelicitFromSelf, + }; + } - /// mod autho - pub mod author { - pub use super::_common::*; - pub use super::_inner::{ - WeakAssign, - WeakAelicitInner, - AelicitFromSelfField - }; - } + /// mod autho + pub mod author { + pub use super::_common::*; + pub use super::_inner::{ + WeakAssign, + WeakAelicitInner, + AelicitFromSelfField + }; + } - /// mod user - pub mod user { - pub use super::_common::*; - pub use super::_inner::{ - WeakAelicit, - LockError, LockResult, - TryLockError, TryLockResult, - ReadGuard, WriteGuard - }; - } + /// mod user + pub mod user { + pub use super::_common::*; + pub use super::_inner::{ + WeakAelicit, + LockError, LockResult, + TryLockError, TryLockResult, + ReadGuard, WriteGuard + }; + } } }) } @@ -73,37 +70,37 @@ fn quote_inner(a_orig: &Ident) -> Result { // //////////////////////////////////////////////////////////////////// // ==================================================================== use std::{ - convert::From, - fmt::Debug, - marker::Unpin, - pin::Pin, - result::Result as StdResult, - sync::{ OnceLock, Arc, Weak, RwLock, }, + convert::From, + fmt::Debug, + marker::Unpin, + pin::Pin, + result::Result as StdResult, + sync::{ OnceLock, Arc, Weak, RwLock, }, }; // -------------------------------------------------------------------- pub use std::sync::{ - LockResult, PoisonError as LockError,TryLockResult, TryLockError, + LockResult, PoisonError as LockError,TryLockResult, TryLockError, }; pub use elicit::{ Result as ElicitResult, Error as ElicitError }; // //////////////////////////////////////////////////////////////////// // ==================================================================== /// trait AelicitBase pub trait AelicitBase: 'static + Unpin + Debug - + #orig + AelicitFromSelf + WeakAssign + + #orig + AelicitFromSelf + WeakAssign { - // ================================================================ - /// usizeptr - fn usizeptr(&self) -> usize; + // ================================================================ + /// usizeptr + fn usizeptr(&self) -> usize; } // ==================================================================== impl - AelicitBase for T + AelicitBase for T { - // ================================================================ - #[allow(trivial_casts)] - fn usizeptr(&self) -> usize { - self as *const _ as usize - } + // ================================================================ + #[allow(trivial_casts)] + fn usizeptr(&self) -> usize { + self as *const _ as usize + } } // //////////////////////////////////////////////////////////////////// // ==================================================================== @@ -132,156 +129,156 @@ fn quote_inner(a_orig: &Ident) -> Result { pub struct WeakAelicit(WeakAelicitInner); // ==================================================================== impl WeakAelicit { - // ================================================================ - /// fn upgrade - pub fn upgrade(&self) -> Option { - self.0.upgrade().map(Aelicit) - } + // ================================================================ + /// fn upgrade + pub fn upgrade(&self) -> Option { + self.0.upgrade().map(Aelicit) + } } // ==================================================================== impl From for WeakAelicit { - fn from(x: Aelicit) -> WeakAelicit { - x.weak() - } + fn from(x: Aelicit) -> WeakAelicit { + x.weak() + } } // //////////////////////////////////////////////////////////////////// // ==================================================================== /// trait AelicitFromSelf pub trait AelicitFromSelf { - /// aelicit_from_self - fn aelicit_from_self(&self) -> Option; + /// aelicit_from_self + fn aelicit_from_self(&self) -> Option; } // //////////////////////////////////////////////////////////////////// // ==================================================================== /// trait WeakAssign pub trait WeakAssign { - /// _weak_assign - fn _weak_assign( - &mut self, - weak: WeakAelicitInner, - ) -> ElicitResult<()>; + /// _weak_assign + fn _weak_assign( + &mut self, + weak: WeakAelicitInner, + ) -> ElicitResult<()>; } // //////////////////////////////////////////////////////////////////// // ==================================================================== /// struct AelicitFromSelfField #[derive(Debug, Clone, Default)] pub struct AelicitFromSelfField { - /// _weak - _weak: OnceLock, + /// _weak + _weak: OnceLock, } // ==================================================================== impl AelicitFromSelf for AelicitFromSelfField { - fn aelicit_from_self(&self) -> Option { - self._weak.get()?.upgrade().map(Aelicit) - } + fn aelicit_from_self(&self) -> Option { + self._weak.get()?.upgrade().map(Aelicit) + } } // ==================================================================== impl WeakAssign for AelicitFromSelfField { - fn _weak_assign( - &mut self, - weak: WeakAelicitInner, - ) -> ElicitResult<()> { - self._weak.set(weak).map_err( - |_| ElicitError::WeakAlreadyExists) - } + fn _weak_assign( + &mut self, + weak: WeakAelicitInner, + ) -> ElicitResult<()> { + self._weak.set(weak).map_err( + |_| ElicitError::WeakAlreadyExists) + } } // //////////////////////////////////////////////////////////////////// // ==================================================================== impl Aelicit { - // ================================================================ - /// new - #[allow(trivial_casts)] - pub fn new(val: T) -> ElicitResult - where - T: AelicitBase, - { - let r = Arc::new(RwLock::new(Box::pin(val) as RwLockInner)); - r.write().expect("Aelicit::new").as_mut() - ._weak_assign(Arc::<_>::downgrade(&r))?; - Ok(Aelicit(r)) - } - // ================================================================ - /// weak - pub fn weak(&self) -> WeakAelicit { - WeakAelicit(Arc::downgrade(&self.0)) - } - // ================================================================ - /// usizeptr - pub fn usizeptr<'s, 'a>(&'s self) -> - StdResult>> - where - 's: 'a, - { - Ok(self.0.read()?.usizeptr()) - } - // ================================================================ - /// read - pub fn read(&self) -> LockResult> { - self.0.read() - } - // ================================================================ - /// try_read - pub fn try_read(&self) -> TryLockResult> { - self.0.try_read() - } - // ================================================================ - /// write - pub fn write(&self) -> LockResult> { - self.0.write() - } - // ================================================================ - /// try_write - pub fn try_write(&self) -> TryLockResult> { - self.0.try_write() - } - // ================================================================ - /// with - pub fn with<'s, 'a, T, E>( - &'s self, - f: impl FnOnce(&dyn AelicitBase) -> StdResult, - ) -> StdResult - where - 's: 'a, - E: From>>, - { - f(self.read()?.as_ref().get_ref()) - } - // ================================================================ - /// try_with - pub fn try_with<'s, 'a, T, E>( - &'s self, - f: impl FnOnce(&dyn AelicitBase) -> StdResult, - ) -> StdResult - where - 's: 'a, - E: From>>, - { - f(self.try_read()?.as_ref().get_ref()) - } - // ================================================================ - /// with_mut - pub fn with_mut<'s, 'a, T, E>( - &'s self, - f: impl FnOnce(&mut dyn AelicitBase) -> StdResult, - ) -> StdResult - where - 's: 'a, - E: From>>, - { - f(self.write()?.as_mut().get_mut()) - } - // ================================================================ - /// try_with_mut - pub fn try_with_mut<'s, 'a, T, E>( - &'s self, - f: impl FnOnce(&mut dyn AelicitBase) -> StdResult, - ) -> StdResult - where - 's: 'a, - E: From>>, - { - f(self.try_write()?.as_mut().get_mut()) - } + // ================================================================ + /// new + #[allow(trivial_casts)] + pub fn new(val: T) -> ElicitResult + where + T: AelicitBase, + { + let r = Arc::new(RwLock::new(Box::pin(val) as RwLockInner)); + r.write().expect("Aelicit::new").as_mut() + ._weak_assign(Arc::<_>::downgrade(&r))?; + Ok(Aelicit(r)) + } + // ================================================================ + /// weak + pub fn weak(&self) -> WeakAelicit { + WeakAelicit(Arc::downgrade(&self.0)) + } + // ================================================================ + /// usizeptr + pub fn usizeptr<'s, 'a>(&'s self) -> + StdResult>> + where + 's: 'a, + { + Ok(self.0.read()?.usizeptr()) + } + // ================================================================ + /// read + pub fn read(&self) -> LockResult> { + self.0.read() + } + // ================================================================ + /// try_read + pub fn try_read(&self) -> TryLockResult> { + self.0.try_read() + } + // ================================================================ + /// write + pub fn write(&self) -> LockResult> { + self.0.write() + } + // ================================================================ + /// try_write + pub fn try_write(&self) -> TryLockResult> { + self.0.try_write() + } + // ================================================================ + /// with + pub fn with<'s, 'a, T, E>( + &'s self, + f: impl FnOnce(&dyn AelicitBase) -> StdResult, + ) -> StdResult + where + 's: 'a, + E: From>>, + { + f(self.read()?.as_ref().get_ref()) + } + // ================================================================ + /// try_with + pub fn try_with<'s, 'a, T, E>( + &'s self, + f: impl FnOnce(&dyn AelicitBase) -> StdResult, + ) -> StdResult + where + 's: 'a, + E: From>>, + { + f(self.try_read()?.as_ref().get_ref()) + } + // ================================================================ + /// with_mut + pub fn with_mut<'s, 'a, T, E>( + &'s self, + f: impl FnOnce(&mut dyn AelicitBase) -> StdResult, + ) -> StdResult + where + 's: 'a, + E: From>>, + { + f(self.write()?.as_mut().get_mut()) + } + // ================================================================ + /// try_with_mut + pub fn try_with_mut<'s, 'a, T, E>( + &'s self, + f: impl FnOnce(&mut dyn AelicitBase) -> StdResult, + ) -> StdResult + where + 's: 'a, + E: From>>, + { + f(self.try_write()?.as_mut().get_mut()) + } } }) } diff --git a/elicit_macro/src/feature_default/melicit_define.rs b/elicit_macro/src/feature_default/melicit_define.rs index 038fab5..6e9eab7 100644 --- a/elicit_macro/src/feature_default/melicit_define.rs +++ b/elicit_macro/src/feature_default/melicit_define.rs @@ -16,10 +16,7 @@ use crate::include::{ // //////////////////////////////////////////////////////////////////////////// // ============================================================================ /// fn expand -pub(crate) fn expand( - mod_ident: &Ident, - item: ItemTrait, -) -> Result { +pub fn expand(mod_ident: &Ident, item: ItemTrait) -> Result { let define = quote_define(mod_ident, &item)?; let mut ret = item.into_token_stream(); ret.extend(define); @@ -33,34 +30,34 @@ fn quote_define(mod_ident: &Ident, item: &ItemTrait) -> Result { Ok(quote! { #[allow(box_pointers)] mod #mod_ident { - mod _inner { #inner } + mod _inner { #inner } - mod _common { - pub use super::_inner::{ - Melicit, MelicitBase, MelicitFromSelf, - }; - } + mod _common { + pub use super::_inner::{ + Melicit, MelicitBase, MelicitFromSelf, + }; + } - /// mod author - pub mod author { - pub use super::_common::*; - pub use super::_inner::{ - WeakAssign, - WeakMelicitInner, - MelicitFromSelfField - }; - } + /// mod author + pub mod author { + pub use super::_common::*; + pub use super::_inner::{ + WeakAssign, + WeakMelicitInner, + MelicitFromSelfField + }; + } - /// mod user - pub mod user { - pub use super::_common::*; - pub use super::_inner::{ - WeakMelicit, - LockError, LockResult, - TryLockError, TryLockResult, - Guard, - }; - } + /// mod user + pub mod user { + pub use super::_common::*; + pub use super::_inner::{ + WeakMelicit, + LockError, LockResult, + TryLockError, TryLockResult, + Guard, + }; + } } }) } @@ -73,38 +70,38 @@ fn quote_inner(a_orig: &Ident) -> Result { // //////////////////////////////////////////////////////////////////// // ==================================================================== use std::{ - convert::From, - fmt::Debug, - marker::Unpin, - pin::Pin, - result::Result as StdResult, - sync::{ OnceLock, Arc, Mutex, Weak }, + convert::From, + fmt::Debug, + marker::Unpin, + pin::Pin, + result::Result as StdResult, + sync::{ OnceLock, Arc, Mutex, Weak }, }; // -------------------------------------------------------------------- pub use std::sync::{ - LockResult, PoisonError as LockError, - TryLockResult, TryLockError, + LockResult, PoisonError as LockError, + TryLockResult, TryLockError, }; pub use elicit::{ Result as ElicitResult, Error as ElicitError }; // //////////////////////////////////////////////////////////////////// // ==================================================================== /// trait MelicitBase pub trait MelicitBase: 'static + Unpin + Debug - + #orig + MelicitFromSelf + WeakAssign + + #orig + MelicitFromSelf + WeakAssign { - // ================================================================ - /// usizeptr - fn usizeptr(&self) -> usize; + // ================================================================ + /// usizeptr + fn usizeptr(&self) -> usize; } // ==================================================================== impl - MelicitBase for T + MelicitBase for T { - // ================================================================ - #[allow(trivial_casts)] - fn usizeptr(&self) -> usize { - self as *const _ as usize - } + // ================================================================ + #[allow(trivial_casts)] + fn usizeptr(&self) -> usize { + self as *const _ as usize + } } // //////////////////////////////////////////////////////////////////// // ==================================================================== @@ -130,147 +127,147 @@ fn quote_inner(a_orig: &Ident) -> Result { pub struct WeakMelicit(WeakMelicitInner); // ==================================================================== impl WeakMelicit { - // ================================================================ - /// fn upgrade - pub fn upgrade(&self) -> Option { - self.0.upgrade().map(Melicit) - } + // ================================================================ + /// fn upgrade + pub fn upgrade(&self) -> Option { + self.0.upgrade().map(Melicit) + } } // ==================================================================== impl From for WeakMelicit { - fn from(x: Melicit) -> WeakMelicit { - x.weak() - } + fn from(x: Melicit) -> WeakMelicit { + x.weak() + } } // //////////////////////////////////////////////////////////////////// // ==================================================================== /// trait MelicitFromSelf pub trait MelicitFromSelf { - /// melicit_from_self - fn melicit_from_self(&self) -> Option; + /// melicit_from_self + fn melicit_from_self(&self) -> Option; } // //////////////////////////////////////////////////////////////////// // ==================================================================== /// trait WeakAssign pub trait WeakAssign { - /// _weak_assign - fn _weak_assign( - &mut self, - weak: WeakMelicitInner, - ) -> ElicitResult<()>; + /// _weak_assign + fn _weak_assign( + &mut self, + weak: WeakMelicitInner, + ) -> ElicitResult<()>; } // //////////////////////////////////////////////////////////////////// // ==================================================================== /// struct MelicitFromSelfField #[derive(Debug, Clone, Default)] pub struct MelicitFromSelfField { - /// _weak - _weak: OnceLock, + /// _weak + _weak: OnceLock, } // ==================================================================== impl MelicitFromSelf for MelicitFromSelfField { - fn melicit_from_self(&self) -> Option { - self._weak.get()?.upgrade().map(Melicit) - } + fn melicit_from_self(&self) -> Option { + self._weak.get()?.upgrade().map(Melicit) + } } // ==================================================================== impl WeakAssign for MelicitFromSelfField { - fn _weak_assign( - &mut self, - weak: WeakMelicitInner, - ) -> ElicitResult<()> { - self._weak.set(weak).map_err( - |_| ElicitError::WeakAlreadyExists) - } + fn _weak_assign( + &mut self, + weak: WeakMelicitInner, + ) -> ElicitResult<()> { + self._weak.set(weak).map_err( + |_| ElicitError::WeakAlreadyExists) + } } // //////////////////////////////////////////////////////////////////// // ==================================================================== impl Melicit { - // ================================================================ - /// new - #[allow(trivial_casts)] - pub fn new(val: T) -> ElicitResult - where - T: MelicitBase, - { - let r = Arc::new(Mutex::new(Box::pin(val) as MutexInner)); - r.lock().expect("Melicit::new").as_mut() - ._weak_assign(Arc::<_>::downgrade(&r))?; - Ok(Melicit(r)) - } - // ================================================================ - /// weak - pub fn weak(&self) -> WeakMelicit { - WeakMelicit(Arc::downgrade(&self.0)) - } - // ================================================================ - /// usizeptr - pub fn usizeptr<'s, 'a>(&'s self) -> - StdResult>> - where - 's: 'a, - { - Ok(self.0.lock()?.as_ref().usizeptr()) - } - // ================================================================ - /// lock - pub fn lock(&self) -> LockResult> - { - self.0.lock() - } - // ================================================================ - /// try_lock - pub fn try_lock(&self) -> TryLockResult> { - self.0.try_lock() - } - // ================================================================ - /// with - pub fn with<'s, 'a, T, E>( - &'s self, - f: impl FnOnce(&dyn MelicitBase) -> StdResult, - ) -> StdResult - where - 's: 'a, - E: From>>, - { - f(self.lock()?.as_ref().get_ref()) - } - // ================================================================ - /// try_with - pub fn try_with<'s, 'a, T, E>( - &'s self, - f: impl FnOnce(&dyn MelicitBase) -> StdResult, - ) -> StdResult - where - 's: 'a, - E: From>>, - { - f(self.try_lock()?.as_ref().get_ref()) - } - // ================================================================ - /// with_mut - pub fn with_mut<'s, 'a, T, E>( - &'s self, - f: impl FnOnce(&mut dyn MelicitBase) -> StdResult, - ) -> StdResult - where - 's: 'a, - E: From>>, - { - f(self.lock()?.as_mut().get_mut()) - } - // ================================================================ - /// try_with_mut - pub fn try_with_mut<'s, 'a, T, E>( - &'s self, - f: impl FnOnce(&mut dyn MelicitBase) -> StdResult, - ) -> StdResult - where - 's: 'a, - E: From>>, - { - f(self.try_lock()?.as_mut().get_mut()) - } + // ================================================================ + /// new + #[allow(trivial_casts)] + pub fn new(val: T) -> ElicitResult + where + T: MelicitBase, + { + let r = Arc::new(Mutex::new(Box::pin(val) as MutexInner)); + r.lock().expect("Melicit::new").as_mut() + ._weak_assign(Arc::<_>::downgrade(&r))?; + Ok(Melicit(r)) + } + // ================================================================ + /// weak + pub fn weak(&self) -> WeakMelicit { + WeakMelicit(Arc::downgrade(&self.0)) + } + // ================================================================ + /// usizeptr + pub fn usizeptr<'s, 'a>(&'s self) -> + StdResult>> + where + 's: 'a, + { + Ok(self.0.lock()?.as_ref().usizeptr()) + } + // ================================================================ + /// lock + pub fn lock(&self) -> LockResult> + { + self.0.lock() + } + // ================================================================ + /// try_lock + pub fn try_lock(&self) -> TryLockResult> { + self.0.try_lock() + } + // ================================================================ + /// with + pub fn with<'s, 'a, T, E>( + &'s self, + f: impl FnOnce(&dyn MelicitBase) -> StdResult, + ) -> StdResult + where + 's: 'a, + E: From>>, + { + f(self.lock()?.as_ref().get_ref()) + } + // ================================================================ + /// try_with + pub fn try_with<'s, 'a, T, E>( + &'s self, + f: impl FnOnce(&dyn MelicitBase) -> StdResult, + ) -> StdResult + where + 's: 'a, + E: From>>, + { + f(self.try_lock()?.as_ref().get_ref()) + } + // ================================================================ + /// with_mut + pub fn with_mut<'s, 'a, T, E>( + &'s self, + f: impl FnOnce(&mut dyn MelicitBase) -> StdResult, + ) -> StdResult + where + 's: 'a, + E: From>>, + { + f(self.lock()?.as_mut().get_mut()) + } + // ================================================================ + /// try_with_mut + pub fn try_with_mut<'s, 'a, T, E>( + &'s self, + f: impl FnOnce(&mut dyn MelicitBase) -> StdResult, + ) -> StdResult + where + 's: 'a, + E: From>>, + { + f(self.try_lock()?.as_mut().get_mut()) + } } }) } diff --git a/elicit_macro/src/feature_parking_lot.rs b/elicit_macro/src/feature_parking_lot.rs index 639de92..8a7c1dd 100644 --- a/elicit_macro/src/feature_parking_lot.rs +++ b/elicit_macro/src/feature_parking_lot.rs @@ -6,9 +6,9 @@ // @author hanepjiv // @copyright The MIT License (MIT) / Apache License Version 2.0 // @since 2024/04/25 -// @date 2024/04/26 +// @date 2024/11/30 // //////////////////////////////////////////////////////////////////////////// // mod ======================================================================= -pub(crate) mod aelicit_define; -pub(crate) mod melicit_define; +pub mod aelicit_define; +pub mod melicit_define; diff --git a/elicit_macro/src/feature_parking_lot/aelicit_define.rs b/elicit_macro/src/feature_parking_lot/aelicit_define.rs index aa32803..a9e3400 100644 --- a/elicit_macro/src/feature_parking_lot/aelicit_define.rs +++ b/elicit_macro/src/feature_parking_lot/aelicit_define.rs @@ -10,14 +10,13 @@ // //////////////////////////////////////////////////////////////////////////// // use ======================================================================= -use crate::include::{Ident, ItemTrait, Result, ToTokens, TokenStream2, quote}; +use crate::include::{ + quote, Ident, ItemTrait, Result, ToTokens, TokenStream2, +}; // //////////////////////////////////////////////////////////////////////////// // ============================================================================ /// fn expand -pub(crate) fn expand( - mod_ident: &Ident, - item: ItemTrait, -) -> Result { +pub fn expand(mod_ident: &Ident, item: ItemTrait) -> Result { let define = quote_define(mod_ident, &item)?; let mut ret = item.into_token_stream(); ret.extend(define); @@ -31,32 +30,32 @@ fn quote_define(mod_ident: &Ident, item: &ItemTrait) -> Result { Ok(quote! { #[allow(box_pointers)] mod #mod_ident { - mod _inner { #inner } + mod _inner { #inner } - mod _common { - pub use super::_inner::{ - Aelicit, AelicitBase, AelicitFromSelf, - }; - } + mod _common { + pub use super::_inner::{ + Aelicit, AelicitBase, AelicitFromSelf, + }; + } - /// mod autho - pub mod author { - pub use super::_common::*; - pub use super::_inner::{ - WeakAssign, - WeakAelicitInner, - AelicitFromSelfField - }; - } + /// mod autho + pub mod author { + pub use super::_common::*; + pub use super::_inner::{ + WeakAssign, + WeakAelicitInner, + AelicitFromSelfField + }; + } - /// mod user - pub mod user { - pub use super::_common::*; - pub use super::_inner::{ - WeakAelicit, - ReadGuard, WriteGuard - }; - } + /// mod user + pub mod user { + pub use super::_common::*; + pub use super::_inner::{ + WeakAelicit, + ReadGuard, WriteGuard + }; + } } }) } @@ -69,12 +68,12 @@ fn quote_inner(a_orig: &Ident) -> Result { // //////////////////////////////////////////////////////////////////// // ==================================================================== use std::{ - convert::From, - fmt::Debug, - marker::Unpin, - pin::Pin, - result::Result as StdResult, - sync::{ OnceLock, Arc, Weak, }, + convert::From, + fmt::Debug, + marker::Unpin, + pin::Pin, + result::Result as StdResult, + sync::{ OnceLock, Arc, Weak, }, }; use elicit::RwLock; // -------------------------------------------------------------------- @@ -83,21 +82,21 @@ fn quote_inner(a_orig: &Ident) -> Result { // ==================================================================== /// trait AelicitBase pub trait AelicitBase: 'static + Unpin + Debug - + #orig + AelicitFromSelf + WeakAssign + + #orig + AelicitFromSelf + WeakAssign { - // ================================================================ - /// usizeptr - fn usizeptr(&self) -> usize; + // ================================================================ + /// usizeptr + fn usizeptr(&self) -> usize; } // ==================================================================== impl - AelicitBase for T + AelicitBase for T { - // ================================================================ - #[allow(trivial_casts)] - fn usizeptr(&self) -> usize { - self as *const _ as usize - } + // ================================================================ + #[allow(trivial_casts)] + fn usizeptr(&self) -> usize { + self as *const _ as usize + } } // //////////////////////////////////////////////////////////////////// // ==================================================================== @@ -126,157 +125,157 @@ fn quote_inner(a_orig: &Ident) -> Result { pub struct WeakAelicit(WeakAelicitInner); // ==================================================================== impl WeakAelicit { - // ================================================================ - /// fn upgrade - pub fn upgrade(&self) -> Option { - self.0.upgrade().map(Aelicit) - } + // ================================================================ + /// fn upgrade + pub fn upgrade(&self) -> Option { + self.0.upgrade().map(Aelicit) + } } // ==================================================================== impl From for WeakAelicit { - fn from(x: Aelicit) -> WeakAelicit { - x.weak() - } + fn from(x: Aelicit) -> WeakAelicit { + x.weak() + } } // //////////////////////////////////////////////////////////////////// // ==================================================================== /// trait AelicitFromSelf pub trait AelicitFromSelf { - /// aelicit_from_self - fn aelicit_from_self(&self) -> Option; + /// aelicit_from_self + fn aelicit_from_self(&self) -> Option; } // //////////////////////////////////////////////////////////////////// // ==================================================================== /// trait WeakAssign pub trait WeakAssign { - /// _weak_assign - fn _weak_assign( - &mut self, - weak: WeakAelicitInner, - ) -> ElicitResult<()>; + /// _weak_assign + fn _weak_assign( + &mut self, + weak: WeakAelicitInner, + ) -> ElicitResult<()>; } // //////////////////////////////////////////////////////////////////// // ==================================================================== /// struct AelicitFromSelfField #[derive(Debug, Clone, Default)] pub struct AelicitFromSelfField { - /// _weak - _weak: OnceLock, + /// _weak + _weak: OnceLock, } // ==================================================================== impl AelicitFromSelf for AelicitFromSelfField { - fn aelicit_from_self(&self) -> Option { - self._weak.get()?.upgrade().map(Aelicit) - } + fn aelicit_from_self(&self) -> Option { + self._weak.get()?.upgrade().map(Aelicit) + } } // ==================================================================== impl WeakAssign for AelicitFromSelfField { - fn _weak_assign( - &mut self, - weak: WeakAelicitInner, - ) -> ElicitResult<()> { - self._weak.set(weak).map_err( - |_| ElicitError::WeakAlreadyExists) - } + fn _weak_assign( + &mut self, + weak: WeakAelicitInner, + ) -> ElicitResult<()> { + self._weak.set(weak).map_err( + |_| ElicitError::WeakAlreadyExists) + } } // //////////////////////////////////////////////////////////////////// // ==================================================================== impl Aelicit { - // ================================================================ - /// new - #[allow(trivial_casts)] - pub fn new(val: T) -> ElicitResult - where - T: AelicitBase, - { - let r = Arc::new(RwLock::new(Box::pin(val) as RwLockInner)); - r.write().as_mut()._weak_assign(Arc::<_>::downgrade(&r))?; - Ok(Aelicit(r)) - } - // ================================================================ - /// weak - pub fn weak(&self) -> WeakAelicit { - WeakAelicit(Arc::downgrade(&self.0)) - } - // ================================================================ - /// usizeptr - pub fn usizeptr(&self) -> usize { - self.0.read().as_ref().usizeptr() - } - // ================================================================ - /// read - pub fn read(&self) -> ReadGuard<'_> { - self.0.read() - } - // ================================================================ - /// try_read - pub fn try_read(&self) -> Option> { - self.0.try_read() - } - // ================================================================ - /// write - pub fn write(&self) -> WriteGuard<'_> { - self.0.write() - } - // ================================================================ - /// try_write - pub fn try_write(&self) -> Option> { - self.0.try_write() - } - // ================================================================ - /// with - pub fn with<'s, 'a, T, E>( - &'s self, - f: impl FnOnce(&dyn AelicitBase) -> StdResult, - ) -> StdResult - where - 's: 'a, - { - f(self.read().as_ref().get_ref()) - } - // ================================================================ - /// try_with - pub fn try_with<'s, 'a, T, E>( - &'s self, - f: impl FnOnce(&dyn AelicitBase) -> StdResult, - ) -> StdResult - where - 's: 'a, - E: From, - { - if let Some(x) = self.try_read() { - f(x.as_ref().get_ref()) - } else { - Err(ElicitError::WouldBlock.into()) - } - } - // ================================================================ - /// with_mut - pub fn with_mut<'s, 'a, T, E>( - &'s self, - f: impl FnOnce(&mut dyn AelicitBase) -> StdResult, - ) -> StdResult - where - 's: 'a, - { - f(self.write().as_mut().get_mut()) - } - // ================================================================ - /// try_with_mut - pub fn try_with_mut<'s, 'a, T, E>( - &'s self, - f: impl FnOnce(&mut dyn AelicitBase) -> StdResult, - ) -> StdResult - where - 's: 'a, - E: From, - { - if let Some(mut x) = self.try_write() { - f(x.as_mut().get_mut()) - } else { - Err(ElicitError::WouldBlock.into()) - } - } + // ================================================================ + /// new + #[allow(trivial_casts)] + pub fn new(val: T) -> ElicitResult + where + T: AelicitBase, + { + let r = Arc::new(RwLock::new(Box::pin(val) as RwLockInner)); + r.write().as_mut()._weak_assign(Arc::<_>::downgrade(&r))?; + Ok(Aelicit(r)) + } + // ================================================================ + /// weak + pub fn weak(&self) -> WeakAelicit { + WeakAelicit(Arc::downgrade(&self.0)) + } + // ================================================================ + /// usizeptr + pub fn usizeptr(&self) -> usize { + self.0.read().as_ref().usizeptr() + } + // ================================================================ + /// read + pub fn read(&self) -> ReadGuard<'_> { + self.0.read() + } + // ================================================================ + /// try_read + pub fn try_read(&self) -> Option> { + self.0.try_read() + } + // ================================================================ + /// write + pub fn write(&self) -> WriteGuard<'_> { + self.0.write() + } + // ================================================================ + /// try_write + pub fn try_write(&self) -> Option> { + self.0.try_write() + } + // ================================================================ + /// with + pub fn with<'s, 'a, T, E>( + &'s self, + f: impl FnOnce(&dyn AelicitBase) -> StdResult, + ) -> StdResult + where + 's: 'a, + { + f(self.read().as_ref().get_ref()) + } + // ================================================================ + /// try_with + pub fn try_with<'s, 'a, T, E>( + &'s self, + f: impl FnOnce(&dyn AelicitBase) -> StdResult, + ) -> StdResult + where + 's: 'a, + E: From, + { + if let Some(x) = self.try_read() { + f(x.as_ref().get_ref()) + } else { + Err(ElicitError::WouldBlock.into()) + } + } + // ================================================================ + /// with_mut + pub fn with_mut<'s, 'a, T, E>( + &'s self, + f: impl FnOnce(&mut dyn AelicitBase) -> StdResult, + ) -> StdResult + where + 's: 'a, + { + f(self.write().as_mut().get_mut()) + } + // ================================================================ + /// try_with_mut + pub fn try_with_mut<'s, 'a, T, E>( + &'s self, + f: impl FnOnce(&mut dyn AelicitBase) -> StdResult, + ) -> StdResult + where + 's: 'a, + E: From, + { + if let Some(mut x) = self.try_write() { + f(x.as_mut().get_mut()) + } else { + Err(ElicitError::WouldBlock.into()) + } + } } }) } diff --git a/elicit_macro/src/feature_parking_lot/melicit_define.rs b/elicit_macro/src/feature_parking_lot/melicit_define.rs index 0b872d6..7bde80e 100644 --- a/elicit_macro/src/feature_parking_lot/melicit_define.rs +++ b/elicit_macro/src/feature_parking_lot/melicit_define.rs @@ -10,14 +10,13 @@ // //////////////////////////////////////////////////////////////////////////// // use ======================================================================= -use crate::include::{Ident, ItemTrait, Result, ToTokens, TokenStream2, quote}; +use crate::include::{ + quote, Ident, ItemTrait, Result, ToTokens, TokenStream2, +}; // //////////////////////////////////////////////////////////////////////////// // ============================================================================ /// fn expand -pub(crate) fn expand( - mod_ident: &Ident, - item: ItemTrait, -) -> Result { +pub fn expand(mod_ident: &Ident, item: ItemTrait) -> Result { let define = quote_define(mod_ident, &item)?; let mut ret = item.into_token_stream(); ret.extend(define); @@ -31,32 +30,32 @@ fn quote_define(mod_ident: &Ident, item: &ItemTrait) -> Result { Ok(quote! { #[allow(box_pointers)] mod #mod_ident { - mod _inner { #inner } + mod _inner { #inner } - mod _common { - pub use super::_inner::{ - Melicit, MelicitBase, MelicitFromSelf, - }; - } + mod _common { + pub use super::_inner::{ + Melicit, MelicitBase, MelicitFromSelf, + }; + } - /// mod author - pub mod author { - pub use super::_common::*; - pub use super::_inner::{ - WeakAssign, - WeakMelicitInner, - MelicitFromSelfField - }; - } + /// mod author + pub mod author { + pub use super::_common::*; + pub use super::_inner::{ + WeakAssign, + WeakMelicitInner, + MelicitFromSelfField + }; + } - /// mod user - pub mod user { - pub use super::_common::*; - pub use super::_inner::{ - WeakMelicit, - Guard, - }; - } + /// mod user + pub mod user { + pub use super::_common::*; + pub use super::_inner::{ + WeakMelicit, + Guard, + }; + } } }) } @@ -69,12 +68,12 @@ fn quote_inner(a_orig: &Ident) -> Result { // //////////////////////////////////////////////////////////////////// // ==================================================================== use std::{ - convert::From, - fmt::Debug, - marker::Unpin, - pin::Pin, - result::Result as StdResult, - sync::{ OnceLock, Arc, Weak }, + convert::From, + fmt::Debug, + marker::Unpin, + pin::Pin, + result::Result as StdResult, + sync::{ OnceLock, Arc, Weak }, }; use elicit::Mutex; // -------------------------------------------------------------------- @@ -83,21 +82,21 @@ fn quote_inner(a_orig: &Ident) -> Result { // ==================================================================== /// trait MelicitBase pub trait MelicitBase: 'static + Unpin + Debug - + #orig + MelicitFromSelf + WeakAssign + + #orig + MelicitFromSelf + WeakAssign { - // ================================================================ - /// usizeptr - fn usizeptr(&self) -> usize; + // ================================================================ + /// usizeptr + fn usizeptr(&self) -> usize; } // ==================================================================== impl - MelicitBase for T + MelicitBase for T { - // ================================================================ - #[allow(trivial_casts)] - fn usizeptr(&self) -> usize { - self as *const _ as usize - } + // ================================================================ + #[allow(trivial_casts)] + fn usizeptr(&self) -> usize { + self as *const _ as usize + } } // //////////////////////////////////////////////////////////////////// // ==================================================================== @@ -123,148 +122,148 @@ fn quote_inner(a_orig: &Ident) -> Result { pub struct WeakMelicit(WeakMelicitInner); // ==================================================================== impl WeakMelicit { - // ================================================================ - /// fn upgrade - pub fn upgrade(&self) -> Option { - self.0.upgrade().map(Melicit) - } + // ================================================================ + /// fn upgrade + pub fn upgrade(&self) -> Option { + self.0.upgrade().map(Melicit) + } } // ==================================================================== impl From for WeakMelicit { - fn from(x: Melicit) -> WeakMelicit { - x.weak() - } + fn from(x: Melicit) -> WeakMelicit { + x.weak() + } } // //////////////////////////////////////////////////////////////////// // ==================================================================== /// trait MelicitFromSelf pub trait MelicitFromSelf { - /// melicit_from_self - fn melicit_from_self(&self) -> Option; + /// melicit_from_self + fn melicit_from_self(&self) -> Option; } // //////////////////////////////////////////////////////////////////// // ==================================================================== /// trait WeakAssign pub trait WeakAssign { - /// _weak_assign - fn _weak_assign( - &mut self, - weak: WeakMelicitInner, - ) -> ElicitResult<()>; + /// _weak_assign + fn _weak_assign( + &mut self, + weak: WeakMelicitInner, + ) -> ElicitResult<()>; } // //////////////////////////////////////////////////////////////////// // ==================================================================== /// struct MelicitFromSelfField #[derive(Debug, Clone, Default)] pub struct MelicitFromSelfField { - /// _weak - _weak: OnceLock, + /// _weak + _weak: OnceLock, } // ==================================================================== impl MelicitFromSelf for MelicitFromSelfField { - fn melicit_from_self(&self) -> Option { - self._weak.get()?.upgrade().map(Melicit) - } + fn melicit_from_self(&self) -> Option { + self._weak.get()?.upgrade().map(Melicit) + } } // ==================================================================== impl WeakAssign for MelicitFromSelfField { - fn _weak_assign( - &mut self, - weak: WeakMelicitInner, - ) -> ElicitResult<()> { - self._weak.set(weak).map_err( - |_| ElicitError::WeakAlreadyExists) - } + fn _weak_assign( + &mut self, + weak: WeakMelicitInner, + ) -> ElicitResult<()> { + self._weak.set(weak).map_err( + |_| ElicitError::WeakAlreadyExists) + } } // //////////////////////////////////////////////////////////////////// // ==================================================================== impl Melicit { - // ================================================================ - /// new - #[allow(trivial_casts)] - pub fn new(val: T) -> ElicitResult - where - T: MelicitBase, - { - let r = Arc::new(Mutex::new(Box::pin(val) as MutexInner)); - r.lock().as_mut()._weak_assign(Arc::<_>::downgrade(&r))?; - Ok(Melicit(r)) - } - // ================================================================ - /// weak - pub fn weak(&self) -> WeakMelicit { - WeakMelicit(Arc::downgrade(&self.0)) - } - // ================================================================ - /// usizeptr - pub fn usizeptr(&self) -> usize { - self.0.lock().as_ref().usizeptr() - } - // ================================================================ - /// lock - pub fn lock(&self) -> Guard<'_> - { - self.0.lock() - } - // ================================================================ - /// try_lock - pub fn try_lock(&self) -> Option> { - self.0.try_lock() - } - // ================================================================ - /// with - pub fn with<'s, 'a, T, E>( - &'s self, - f: impl FnOnce(&dyn MelicitBase) -> StdResult, - ) -> StdResult - where - 's: 'a, - { - f(self.lock().as_ref().get_ref()) - } - // ================================================================ - /// try_with - pub fn try_with<'s, 'a, T, E>( - &'s self, - f: impl FnOnce(&dyn MelicitBase) -> StdResult, - ) -> StdResult - where - 's: 'a, - E: From, - { - if let Some(x) = self.try_lock() { - f(x.as_ref().get_ref()) - } else { - Err(ElicitError::WouldBlock.into()) - } - } - // ================================================================ - /// with_mut - pub fn with_mut<'s, 'a, T, E>( - &'s self, - f: impl FnOnce(&mut dyn MelicitBase) -> StdResult, - ) -> StdResult - where - 's: 'a, - { - f(self.lock().as_mut().get_mut()) - } - // ================================================================ - /// try_with_mut - pub fn try_with_mut<'s, 'a, T, E>( - &'s self, - f: impl FnOnce(&mut dyn MelicitBase) -> StdResult, - ) -> StdResult - where - 's: 'a, - E: From, - { - if let Some(mut x) = self.try_lock() { - f(x.as_mut().get_mut()) - } else { - Err(ElicitError::WouldBlock.into()) - } - } + // ================================================================ + /// new + #[allow(trivial_casts)] + pub fn new(val: T) -> ElicitResult + where + T: MelicitBase, + { + let r = Arc::new(Mutex::new(Box::pin(val) as MutexInner)); + r.lock().as_mut()._weak_assign(Arc::<_>::downgrade(&r))?; + Ok(Melicit(r)) + } + // ================================================================ + /// weak + pub fn weak(&self) -> WeakMelicit { + WeakMelicit(Arc::downgrade(&self.0)) + } + // ================================================================ + /// usizeptr + pub fn usizeptr(&self) -> usize { + self.0.lock().as_ref().usizeptr() + } + // ================================================================ + /// lock + pub fn lock(&self) -> Guard<'_> + { + self.0.lock() + } + // ================================================================ + /// try_lock + pub fn try_lock(&self) -> Option> { + self.0.try_lock() + } + // ================================================================ + /// with + pub fn with<'s, 'a, T, E>( + &'s self, + f: impl FnOnce(&dyn MelicitBase) -> StdResult, + ) -> StdResult + where + 's: 'a, + { + f(self.lock().as_ref().get_ref()) + } + // ================================================================ + /// try_with + pub fn try_with<'s, 'a, T, E>( + &'s self, + f: impl FnOnce(&dyn MelicitBase) -> StdResult, + ) -> StdResult + where + 's: 'a, + E: From, + { + if let Some(x) = self.try_lock() { + f(x.as_ref().get_ref()) + } else { + Err(ElicitError::WouldBlock.into()) + } + } + // ================================================================ + /// with_mut + pub fn with_mut<'s, 'a, T, E>( + &'s self, + f: impl FnOnce(&mut dyn MelicitBase) -> StdResult, + ) -> StdResult + where + 's: 'a, + { + f(self.lock().as_mut().get_mut()) + } + // ================================================================ + /// try_with_mut + pub fn try_with_mut<'s, 'a, T, E>( + &'s self, + f: impl FnOnce(&mut dyn MelicitBase) -> StdResult, + ) -> StdResult + where + 's: 'a, + E: From, + { + if let Some(mut x) = self.try_lock() { + f(x.as_mut().get_mut()) + } else { + Err(ElicitError::WouldBlock.into()) + } + } } }) } diff --git a/elicit_macro/src/find_field_attr.rs b/elicit_macro/src/find_field_attr.rs index d9e0adf..ce3eed7 100644 --- a/elicit_macro/src/find_field_attr.rs +++ b/elicit_macro/src/find_field_attr.rs @@ -11,7 +11,7 @@ // //////////////////////////////////////////////////////////////////////////// use crate::include::{Error, Ident, Result, Span, ToTokens, TokenStream2}; // use ======================================================================= -pub(crate) fn find_field_attr( +pub fn find_field_attr( data: &syn::Data, ident: &T, ret: &mut Option, diff --git a/elicit_macro/src/lib.rs b/elicit_macro/src/lib.rs index 3d591f4..9a03e41 100644 --- a/elicit_macro/src/lib.rs +++ b/elicit_macro/src/lib.rs @@ -23,21 +23,19 @@ mod find_field_attr; /// pub(crate) mod include { // common ---------------------------------------------------------------- - pub(crate) use proc_macro2::{Span, TokenStream as TokenStream2}; - pub(crate) use quote::{quote, ToTokens}; - pub(crate) use syn::{parse_macro_input, Error}; + pub use proc_macro2::{Span, TokenStream as TokenStream2}; + pub use quote::{quote, ToTokens}; + pub use syn::{parse_macro_input, Error}; - pub(crate) type Result = std::result::Result; + pub type Result = std::result::Result; #[inline] - pub(crate) fn into_tokens( - res: Result, - ) -> proc_macro::TokenStream { + pub fn into_tokens(res: Result) -> proc_macro::TokenStream { res.unwrap_or_else(Error::into_compile_error).into() } // for elicit_macro ------------------------------------------------------ - pub(crate) use syn::{DeriveInput, Ident, ItemTrait}; + pub use syn::{DeriveInput, Ident, ItemTrait}; } // ============================================================================ mod aelicit_derive; diff --git a/elicit_macro/src/melicit_derive.rs b/elicit_macro/src/melicit_derive.rs index 230daa9..1e6ee83 100644 --- a/elicit_macro/src/melicit_derive.rs +++ b/elicit_macro/src/melicit_derive.rs @@ -6,17 +6,19 @@ // @author hanepjiv // @copyright The MIT License (MIT) / Apache License Version 2.0 // @since 2024/04/14 -// @date 2024/11/10 +// @date 2024/11/30 // //////////////////////////////////////////////////////////////////////////// // use ======================================================================= -use crate::include::{DeriveInput, Error, Ident, Result, Span, ToTokens, TokenStream2, quote}; +use crate::include::{ + quote, DeriveInput, Error, Ident, Result, Span, ToTokens, TokenStream2, +}; // ---------------------------------------------------------------------------- use crate::find_field_attr::find_field_attr; // //////////////////////////////////////////////////////////////////////////// // ============================================================================ /// fn expand -pub(crate) fn expand(ast: DeriveInput) -> Result { +pub fn expand(ast: DeriveInput) -> Result { let mut melicit_mod_author = Option::::default(); let mut melicit_from_self_field = Option::::default(); @@ -59,26 +61,32 @@ struct Derived{} } let ident = ast.ident; - let melicit_impl = if let Some(ref x) = melicit_from_self_field { quote! { self.#x.melicit_from_self() } } else { quote! { None } }; - let _weak_assign_impl = if let Some(ref x) = melicit_from_self_field { quote! { self.#x._weak_assign(_weak) } } else { quote! { Ok(()) } }; + let melicit_impl = melicit_from_self_field.as_ref().map_or_else( + || quote! { None }, + |x| quote! { self.#x.melicit_from_self() }, + ); + let _weak_assign_impl = melicit_from_self_field.as_ref().map_or_else( + || quote! { Ok(()) }, + |x| quote! { self.#x._weak_assign(_weak) }, + ); Ok(quote! { #[automatically_derived] impl #melicit_mod_author :: MelicitFromSelf for #ident { - fn melicit_from_self(&self) -> - Option<#melicit_mod_author :: Melicit> { - #melicit_impl - } + fn melicit_from_self(&self) -> + Option<#melicit_mod_author :: Melicit> { + #melicit_impl + } } #[automatically_derived] impl #melicit_mod_author :: WeakAssign for #ident { - fn _weak_assign( - &mut self, - _weak: #melicit_mod_author :: WeakMelicitInner, - ) -> elicit::Result<()> { - #_weak_assign_impl - } + fn _weak_assign( + &mut self, + _weak: #melicit_mod_author :: WeakMelicitInner, + ) -> elicit::Result<()> { + #_weak_assign_impl + } } }) } diff --git a/examples/aelicit.rs b/examples/aelicit.rs index 1510371..c0d0ebc 100644 --- a/examples/aelicit.rs +++ b/examples/aelicit.rs @@ -19,16 +19,16 @@ use parking_lot as _; pub(crate) mod mine { use elicit::{aelicit_define, Aelicit}; #[aelicit_define(mine_aelicit)] - pub(crate) trait Mine: Send + Sync { + pub trait Mine: Send + Sync { fn action(&self) -> i32; } // ------------------------------------------------------------------------ // pub(crate) use mine_aelicit::author as aelicit_author; - pub(crate) use mine_aelicit::user as aelicit_user; + pub use mine_aelicit::user as aelicit_user; // ======================================================================== #[derive(Debug, Default, Clone, Aelicit)] #[aelicit_mod_author(mine_aelicit::author)] - pub(crate) struct MineX {} + pub struct MineX {} // ------------------------------------------------------------------------ impl Mine for MineX { fn action(&self) -> i32 { @@ -39,7 +39,7 @@ pub(crate) mod mine { #[derive(Debug, Clone, Aelicit)] #[aelicit_mod_author(mine_aelicit::author)] //#[aelicit_from_self_field(_fsf)] // here - pub(crate) struct MineY { + pub struct MineY { #[aelicit_from_self_field] // or here _fsf: mine_aelicit::author::AelicitFromSelfField, i: i32, @@ -47,7 +47,7 @@ pub(crate) mod mine { // ------------------------------------------------------------------------ impl MineY { pub(crate) fn new(a: i32) -> Self { - MineY { + Self { _fsf: Default::default(), i: a, } @@ -78,7 +78,7 @@ mod error { /// enum Error #[allow(dead_code)] #[derive(Debug)] - pub(crate) enum Error<'a> { + pub enum Error<'a> { /// Elicit Elicit(elicit::Error), @@ -158,7 +158,7 @@ mod error { // //////////////////////////////////////////////////////////////////////// // ======================================================================== /// type Result - pub(crate) type Result<'a, T> = ::std::result::Result>; + pub type Result<'a, T> = ::std::result::Result>; } // //////////////////////////////////////////////////////////////////////////// // ============================================================================ diff --git a/examples/elicit.rs b/examples/elicit.rs index 89e16f0..af5804e 100644 --- a/examples/elicit.rs +++ b/examples/elicit.rs @@ -20,16 +20,16 @@ pub(crate) mod mine { use elicit::{elicit_define, Elicit}; // ======================================================================== #[elicit_define(mine_elicit)] - pub(crate) trait Mine { + pub trait Mine { fn action(&self) -> i32; } // ------------------------------------------------------------------------ // pub(crate) mine_elicit::author as elicit_author; - pub(crate) use mine_elicit::user as elicit_user; + pub use mine_elicit::user as elicit_user; // ======================================================================== #[derive(Debug, Default, Clone, Elicit)] #[elicit_mod_author(mine_elicit::author)] - pub(crate) struct MineX {} + pub struct MineX {} // ------------------------------------------------------------------------ impl Mine for MineX { fn action(&self) -> i32 { @@ -40,7 +40,7 @@ pub(crate) mod mine { #[derive(Debug, Clone, Elicit)] #[elicit_mod_author(mine_elicit::author)] // #[elicit_from_self_field(_fsf)] // here - pub(crate) struct MineY { + pub struct MineY { #[elicit_from_self_field] // or here _fsf: mine_elicit::author::ElicitFromSelfField, i: i32, @@ -48,7 +48,7 @@ pub(crate) mod mine { // ------------------------------------------------------------------------ impl MineY { pub(crate) fn new(a: i32) -> Self { - MineY { + Self { _fsf: Default::default(), i: a, } diff --git a/examples/melicit.rs b/examples/melicit.rs index 01463bb..c94f93d 100644 --- a/examples/melicit.rs +++ b/examples/melicit.rs @@ -20,16 +20,16 @@ pub(crate) mod mine { use elicit::{melicit_define, Melicit}; // ======================================================================== #[melicit_define(mine_melicit)] - pub(crate) trait Mine: Send { + pub trait Mine: Send { fn action(&self) -> i32; } // ------------------------------------------------------------------------ // pub(crate) mine_melicit::author as melicit_author; - pub(crate) use mine_melicit::user as melicit_user; + pub use mine_melicit::user as melicit_user; // ======================================================================== #[derive(Debug, Default, Clone, Melicit)] #[melicit_mod_author(mine_melicit::author)] - pub(crate) struct MineX {} + pub struct MineX {} // ------------------------------------------------------------------------ impl Mine for MineX { fn action(&self) -> i32 { @@ -40,7 +40,7 @@ pub(crate) mod mine { #[derive(Debug, Clone, Melicit)] #[melicit_mod_author(mine_melicit::author)] // #[melicit_from_self_field(_fsf)] here - pub(crate) struct MineY { + pub struct MineY { #[melicit_from_self_field] // or here _fsf: mine_melicit::author::MelicitFromSelfField, i: i32, @@ -48,7 +48,7 @@ pub(crate) mod mine { // ------------------------------------------------------------------------ impl MineY { pub(crate) fn new(a: i32) -> Self { - MineY { + Self { _fsf: Default::default(), i: a, } @@ -79,7 +79,7 @@ mod error { /// enum Error #[allow(dead_code)] #[derive(Debug)] - pub(crate) enum Error<'a> { + pub enum Error<'a> { /// Elicit Elicit(elicit::Error), @@ -135,7 +135,7 @@ mod error { // //////////////////////////////////////////////////////////////////////// // ======================================================================== /// type Result - pub(crate) type Result<'a, T> = ::std::result::Result>; + pub type Result<'a, T> = ::std::result::Result>; } // //////////////////////////////////////////////////////////////////////////// // ============================================================================ diff --git a/src/error.rs b/src/error.rs index 886ad0e..e93de1f 100644 --- a/src/error.rs +++ b/src/error.rs @@ -35,13 +35,13 @@ impl Display for Error { // ============================================================================ impl From for Error { fn from(e: std::cell::BorrowError) -> Self { - Error::Borrow(e) + Self::Borrow(e) } } // ---------------------------------------------------------------------------- impl From for Error { fn from(e: std::cell::BorrowMutError) -> Self { - Error::BorrowMut(e) + Self::BorrowMut(e) } } // ============================================================================ @@ -49,9 +49,9 @@ impl StdError for Error { // ======================================================================== fn source(&self) -> Option<&(dyn StdError + 'static)> { match *self { - Error::WouldBlock | Error::WeakAlreadyExists => None, - Error::Borrow(ref e) => Some(e), - Error::BorrowMut(ref e) => Some(e), + Self::WouldBlock | Self::WeakAlreadyExists => None, + Self::Borrow(ref e) => Some(e), + Self::BorrowMut(ref e) => Some(e), } } } @@ -67,14 +67,14 @@ mod tests { use super::Error; // ======================================================================== #[test] - fn test_send() { - fn assert_send() {} + const fn test_send() { + const fn assert_send() {} assert_send::(); } // ------------------------------------------------------------------------ #[test] - fn test_sync() { - fn assert_sync() {} + const fn test_sync() { + const fn assert_sync() {} assert_sync::(); } }