diff --git a/description/src/description.rs b/description/src/description.rs index a8ffbe3..9b183bd 100644 --- a/description/src/description.rs +++ b/description/src/description.rs @@ -1,4 +1,3 @@ -use anyhow::anyhow; use scale_info::{ form::PortableForm, Field, PortableRegistry, Type, TypeDef, TypeDefArray, TypeDefBitSequence, TypeDefCompact, TypeDefPrimitive, TypeDefSequence, TypeDefTuple, TypeDefVariant, Variant, @@ -31,15 +30,15 @@ pub fn type_description( } fn return_type_name_on_cache_hit( - type_id: u32, + _type_id: u32, ty: &Type, cached: &String, - transformer: &Transformer, + _transformer: &Transformer, ) -> Option> { if let Some(type_name) = ty.path.ident() { return Some(Ok(type_name)); } - Some(Ok(cached.clone())) + Some(Ok(cached.to_owned())) } let transformer = Transformer::new( ty_description, diff --git a/description/src/lib.rs b/description/src/lib.rs index c204dfa..139ea2b 100644 --- a/description/src/lib.rs +++ b/description/src/lib.rs @@ -95,7 +95,7 @@ mod tests { corners: u8, radius: u64, }, - MultiShape { + Multi { shapes: Vec>, t: T, operation: Operation, @@ -122,7 +122,7 @@ mod tests { corners: u8, radius: u64 }, - MultiShape { + Multi { shapes: Vec< enum Shape { Inivisible, @@ -132,7 +132,7 @@ mod tests { corners: u8, radius: u64 }, - MultiShape { + Multi { shapes: Vec, t: u64, operation: enum Operation { diff --git a/description/src/transformer.rs b/description/src/transformer.rs index d7a5fec..9d30c81 100644 --- a/description/src/transformer.rs +++ b/description/src/transformer.rs @@ -1,6 +1,6 @@ use std::{cell::RefCell, collections::HashMap}; -use scale_info::{form::PortableForm, PortableRegistry, Type, TypeDef}; +use scale_info::{form::PortableForm, PortableRegistry, Type}; /// The transformer provides an abstraction for traversing a type registry /// given a type_id as a starting point, and **transforming** it into a tree-like structure (type parameter `R`). @@ -27,6 +27,7 @@ pub struct Transformer<'a, R, S = ()> { /// A cache hit is, when the representation of a type has already been computed. /// /// You can return None to sidestep recursion protection and let the transformer continue. + #[allow(clippy::type_complexity)] cache_hit_policy: fn(u32, &Type, &R, &Self) -> Option>, registry: &'a PortableRegistry, } @@ -44,6 +45,7 @@ where R: Clone + std::fmt::Debug, { /// Create a new transformer. + #[allow(clippy::type_complexity)] pub fn new( policy: fn(u32, &Type, &Self) -> anyhow::Result, recurse_policy: fn(u32, &Type, &Self) -> Option>, @@ -72,21 +74,15 @@ where type_id ))?; - match self.cache.borrow().get(&type_id) { - Some(cache_value) => { - let result_or_continue = match cache_value { - Cached::Recursive => (self.recurse_policy)(type_id, ty, self), - Cached::Computed(repr) => (self.cache_hit_policy)(type_id, ty, repr, self), - }; - - if let Some(result) = result_or_continue { - return result; - } + if let Some(cache_value) = self.cache.borrow().get(&type_id) { + let result_or_continue = match cache_value { + Cached::Recursive => (self.recurse_policy)(type_id, ty, self), + Cached::Computed(repr) => (self.cache_hit_policy)(type_id, ty, repr, self), + }; + if let Some(result) = result_or_continue { + return result; } - Some(Cached::Computed(repr)) => {} - _ => {} }; - self.cache.borrow_mut().insert(type_id, Cached::Recursive); let r = (self.policy)(type_id, ty, self)?; self.cache @@ -95,49 +91,3 @@ where Ok(r) } } - -/// Returns true for types where recursion should continue, instead of being stopped when recursion in being detected. -/// -/// ## Background: -/// -/// There is a problem in generating recursive type descriptions: -/// Suppose we have the following setup: -/// ```rust -/// struct A { -/// bees: Vec -/// } -/// -/// struct B { -/// id: u8, -/// others: Vec -/// } -/// ``` -/// This could be described as: -/// ```txt,no_run -/// struct A { -/// bees: Vec -/// }> -/// } -/// ``` -/// But the recursive resolving would get stuck in the middle, reporting recursion. -/// This is because Vec needs to be mapped to different strings, so the simple cache lookup is not viable. -/// The solution to this is, to just let some container types like Vec do recursion while others can't. -/// -/// # Warning -/// -/// The safety of the following logic relies on the assumption that ultimately everything resolves down to a primitive or a struct/enum that is in the cache. -/// It basically just returns true for generic wrapper types. -fn recursion_should_continue(def: &TypeDef) -> bool { - match def { - scale_info::TypeDef::Sequence(_) => true, - scale_info::TypeDef::Array(_) => true, - scale_info::TypeDef::Tuple(_) => true, - scale_info::TypeDef::Compact(_) => true, - scale_info::TypeDef::Composite(_) => false, - scale_info::TypeDef::Primitive(_) => false, - scale_info::TypeDef::Variant(_) => false, - scale_info::TypeDef::BitSequence(_) => false, - } -} diff --git a/description/src/type_example/rust_value.rs b/description/src/type_example/rust_value.rs index 274d71d..8b8c3db 100644 --- a/description/src/type_example/rust_value.rs +++ b/description/src/type_example/rust_value.rs @@ -139,10 +139,10 @@ pub fn example_from_seed( /// Note: because None is returned here, the transformer will just continue its work. fn compute_another_example( - type_id: u32, - ty: &Type, - cached_value: &TokenStream, - transformer: &CodeTransformer, + _type_id: u32, + _ty: &Type, + _cached_value: &TokenStream, + _transformer: &CodeTransformer, ) -> Option> { None } diff --git a/description/src/type_example/scale_value.rs b/description/src/type_example/scale_value.rs index 0ab1d7c..6ae8ccc 100644 --- a/description/src/type_example/scale_value.rs +++ b/description/src/type_example/scale_value.rs @@ -31,10 +31,10 @@ pub fn example_from_seed(id: u32, types: &PortableRegistry, seed: u64) -> anyhow /// Note: because None is returned here, the transformer will just continue its work. fn compute_another_example( - type_id: u32, - ty: &Type, - cached_value: &Value, - transformer: &ValueTransformer, + _type_id: u32, + _ty: &Type, + _cached_value: &Value, + _transformer: &ValueTransformer, ) -> Option> { None }