Skip to content

Commit

Permalink
clippy::all,correctness,suspicious,style,complexity,perf,pedantic,nur…
Browse files Browse the repository at this point in the history
…sery,cargo
  • Loading branch information
hanepjiv committed Nov 30, 2024
1 parent eaf1528 commit be1189a
Show file tree
Hide file tree
Showing 16 changed files with 865 additions and 864 deletions.
22 changes: 10 additions & 12 deletions elicit_macro/src/aelicit_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::find_field_attr::find_field_attr;
// ////////////////////////////////////////////////////////////////////////////
// ============================================================================
/// fn expand
pub(crate) fn expand(ast: DeriveInput) -> Result<TokenStream2> {
pub fn expand(ast: DeriveInput) -> Result<TokenStream2> {
let mut aelicit_mod_author = Option::<TokenStream2>::default();
let mut aelicit_from_self_field = Option::<TokenStream2>::default();

Expand Down Expand Up @@ -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
}
}

Expand Down
257 changes: 127 additions & 130 deletions elicit_macro/src/elicit_define.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ use crate::include::{
// ////////////////////////////////////////////////////////////////////////////
// ============================================================================
/// fn expand
pub(crate) fn expand(
mod_ident: &Ident,
item: ItemTrait,
) -> Result<TokenStream2> {
pub fn expand(mod_ident: &Ident, item: ItemTrait) -> Result<TokenStream2> {
let define = quote_define(mod_ident, &item)?;
let mut ret = item.into_token_stream();
ret.extend(define);
Expand All @@ -33,29 +30,29 @@ fn quote_define(mod_ident: &Ident, item: &ItemTrait) -> Result<TokenStream2> {
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;
}
}
})
}
Expand All @@ -68,35 +65,35 @@ fn quote_inner(a_orig: &Ident) -> Result<TokenStream2> {
// ////////////////////////////////////////////////////////////////////
// ====================================================================
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 };
// ////////////////////////////////////////////////////////////////////
// ====================================================================
/// 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<T: 'static + Unpin + Debug + #orig + ElicitFromSelf + WeakAssign>
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
}
}
// ////////////////////////////////////////////////////////////////////
// ====================================================================
Expand All @@ -118,123 +115,123 @@ fn quote_inner(a_orig: &Ident) -> Result<TokenStream2> {
pub struct WeakElicit(WeakElicitInner);
// ====================================================================
impl WeakElicit {
// ================================================================
/// fn upgrade
pub fn upgrade(&self) -> Option<Elicit> {
self.0.upgrade().map(Elicit)
}
// ================================================================
/// fn upgrade
pub fn upgrade(&self) -> Option<Elicit> {
self.0.upgrade().map(Elicit)
}
}
// ====================================================================
impl From<Elicit> 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>;
/// elicit_from_self
fn elicit_from_self(&self) -> Option<Elicit>;
}
// ====================================================================
/// 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<WeakElicitInner>,
/// _weak
_weak: OnceCell<WeakElicitInner>,
}
// ====================================================================
impl ElicitFromSelf for ElicitFromSelfField {
fn elicit_from_self(&self) -> Option<Elicit> {
self._weak.get()?.upgrade().map(Elicit)
}
fn elicit_from_self(&self) -> Option<Elicit> {
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<T>(val: T) -> ElicitResult<Self>
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<T, E>(
&self,
f: impl FnOnce(&dyn ElicitBase) -> StdResult<T, E>,
) -> StdResult<T, E>
{
f(& *self.0.borrow().as_ref())
}
// ================================================================
/// try_with
pub fn try_with<T, E>(
&self,
f: impl FnOnce(&dyn ElicitBase) -> StdResult<T, E>,
) -> StdResult<T, E>
where
E: From<ElicitError>
{
f(& *self.0.try_borrow().map_err(ElicitError::from)?.as_ref())
}
// ================================================================
/// with_mut
pub fn with_mut<T, E>(
&self,
f: impl FnOnce(&mut dyn ElicitBase) -> StdResult<T, E>,
) -> StdResult<T, E>
{
f(&mut *self.0.borrow_mut().as_mut())
}
// ================================================================
/// try_with_mut
pub fn try_with_mut<T, E>(
&self,
f: impl FnOnce(&mut dyn ElicitBase) -> StdResult<T, E>,
) -> StdResult<T, E>
where
E: From<ElicitError>,
{
f(&mut *self.0.try_borrow_mut().map_err(ElicitError::from)?
.as_mut())
}
// ================================================================
/// new
#[allow(trivial_casts)]
pub fn new<T>(val: T) -> ElicitResult<Self>
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<T, E>(
&self,
f: impl FnOnce(&dyn ElicitBase) -> StdResult<T, E>,
) -> StdResult<T, E>
{
f(& *self.0.borrow().as_ref())
}
// ================================================================
/// try_with
pub fn try_with<T, E>(
&self,
f: impl FnOnce(&dyn ElicitBase) -> StdResult<T, E>,
) -> StdResult<T, E>
where
E: From<ElicitError>
{
f(& *self.0.try_borrow().map_err(ElicitError::from)?.as_ref())
}
// ================================================================
/// with_mut
pub fn with_mut<T, E>(
&self,
f: impl FnOnce(&mut dyn ElicitBase) -> StdResult<T, E>,
) -> StdResult<T, E>
{
f(&mut *self.0.borrow_mut().as_mut())
}
// ================================================================
/// try_with_mut
pub fn try_with_mut<T, E>(
&self,
f: impl FnOnce(&mut dyn ElicitBase) -> StdResult<T, E>,
) -> StdResult<T, E>
where
E: From<ElicitError>,
{
f(&mut *self.0.try_borrow_mut().map_err(ElicitError::from)?
.as_mut())
}
}
})
}
Expand Down
Loading

0 comments on commit be1189a

Please sign in to comment.