From 78ff15a190115a187e8ff049a1e77c9e37aaea2a Mon Sep 17 00:00:00 2001 From: Alexandru Vasile <60601340+lexnv@users.noreply.github.com> Date: Wed, 29 Mar 2023 19:58:22 +0300 Subject: [PATCH] ty: Make type fields public (#176) * portable: Expose types as mutable from the PortableRegistry Signed-off-by: Alexandru Vasile * ty: Make type fields public Signed-off-by: Alexandru Vasile * portable: Make `PortableType` public Signed-off-by: Alexandru Vasile * Deprecate getters Signed-off-by: Alexandru Vasile * Adjust codebase to use inner fields Signed-off-by: Alexandru Vasile * portable: Deprecate PortableType id() and ty() Signed-off-by: Alexandru Vasile * portable: Don't use deprecated getters Signed-off-by: Alexandru Vasile --------- Signed-off-by: Alexandru Vasile --- src/build.rs | 2 +- src/interner.rs | 6 +++- src/portable.rs | 57 +++++++++++++++++++-------------- src/registry.rs | 6 ++-- src/ty/composite.rs | 6 +++- src/ty/fields.rs | 24 +++++++++++--- src/ty/mod.rs | 78 +++++++++++++++++++++++++++++++++++++-------- src/ty/path.rs | 6 +++- src/ty/variant.rs | 22 ++++++++++++- 9 files changed, 158 insertions(+), 49 deletions(-) diff --git a/src/build.rs b/src/build.rs index 9cbecb54..75129e98 100644 --- a/src/build.rs +++ b/src/build.rs @@ -303,7 +303,7 @@ impl FieldsBuilder { impl FieldsBuilder { fn push_field(mut self, field: Field) -> Self { // filter out fields of PhantomData - if !field.ty().is_phantom() { + if !field.ty.is_phantom() { self.fields.push(field); } self diff --git a/src/interner.rs b/src/interner.rs index 65ae7d26..1d7b3d1c 100644 --- a/src/interner.rs +++ b/src/interner.rs @@ -49,13 +49,17 @@ use serde::{ pub struct UntrackedSymbol { /// The index to the symbol in the interner table. #[codec(compact)] - id: u32, + pub id: u32, #[cfg_attr(feature = "serde", serde(skip))] marker: PhantomData T>, } impl UntrackedSymbol { /// Returns the index to the symbol in the interner table. + #[deprecated( + since = "2.5.0", + note = "Prefer to access the fields directly; this getter will be removed in the next major version" + )] pub fn id(&self) -> u32 { self.id } diff --git a/src/portable.rs b/src/portable.rs index 74aae9e0..ca0b51de 100644 --- a/src/portable.rs +++ b/src/portable.rs @@ -46,7 +46,8 @@ use scale::Encode; #[cfg_attr(any(feature = "std", feature = "decode"), derive(scale::Decode))] #[derive(Clone, Debug, PartialEq, Eq, Encode)] pub struct PortableRegistry { - types: Vec, + /// The types contained by the [`PortableRegistry`]. + pub types: Vec, } impl From for PortableRegistry { @@ -56,7 +57,7 @@ impl From for PortableRegistry { .types() .map(|(k, v)| { PortableType { - id: k.id(), + id: k.id, ty: v.clone(), } }) @@ -68,10 +69,14 @@ impl From for PortableRegistry { impl PortableRegistry { /// Returns the type definition for the given identifier, `None` if no type found for that ID. pub fn resolve(&self, id: u32) -> Option<&Type> { - self.types.get(id as usize).map(|ty| ty.ty()) + self.types.get(id as usize).map(|ty| &ty.ty) } /// Returns all types with their associated identifiers. + #[deprecated( + since = "2.5.0", + note = "Prefer to access the fields directly; this getter will be removed in the next major version" + )] pub fn types(&self) -> &[PortableType] { &self.types } @@ -121,10 +126,10 @@ impl PortableRegistry { // Make sure any type params are also retained: for param in ty.ty.type_params.iter_mut() { - let Some(ty) = param.ty() else { + let Some(ty) = ¶m.ty else { continue }; - let new_id = retain_type(ty.id(), types, new_types, retained_mappings); + let new_id = retain_type(ty.id, types, new_types, retained_mappings); param.ty = Some(new_id).map(Into::into); } @@ -132,12 +137,8 @@ impl PortableRegistry { match &mut ty.ty.type_def { TypeDef::Composite(composite) => { for field in composite.fields.iter_mut() { - let new_id = retain_type( - field.ty.id(), - types, - new_types, - retained_mappings, - ); + let new_id = + retain_type(field.ty.id, types, new_types, retained_mappings); field.ty = new_id.into(); } } @@ -145,7 +146,7 @@ impl PortableRegistry { for var in variant.variants.iter_mut() { for field in var.fields.iter_mut() { let new_id = retain_type( - field.ty.id(), + field.ty.id, types, new_types, retained_mappings, @@ -156,7 +157,7 @@ impl PortableRegistry { } TypeDef::Sequence(sequence) => { let new_id = retain_type( - sequence.type_param.id(), + sequence.type_param.id, types, new_types, retained_mappings, @@ -165,7 +166,7 @@ impl PortableRegistry { } TypeDef::Array(array) => { let new_id = retain_type( - array.type_param.id(), + array.type_param.id, types, new_types, retained_mappings, @@ -175,14 +176,14 @@ impl PortableRegistry { TypeDef::Tuple(tuple) => { for ty in tuple.fields.iter_mut() { let new_id = - retain_type(ty.id(), types, new_types, retained_mappings); + retain_type(ty.id, types, new_types, retained_mappings); *ty = new_id.into(); } } TypeDef::Primitive(_) => (), TypeDef::Compact(compact) => { let new_id = retain_type( - compact.type_param().id(), + compact.type_param.id, types, new_types, retained_mappings, @@ -191,13 +192,13 @@ impl PortableRegistry { } TypeDef::BitSequence(bit_seq) => { let bit_store_id = retain_type( - bit_seq.bit_store_type().id(), + bit_seq.bit_store_type.id, types, new_types, retained_mappings, ); let bit_order_id = retain_type( - bit_seq.bit_order_type().id(), + bit_seq.bit_order_type.id, types, new_types, retained_mappings, @@ -236,9 +237,9 @@ impl PortableRegistry { #[derive(Clone, Debug, PartialEq, Eq, Encode)] pub struct PortableType { #[codec(compact)] - id: u32, + pub id: u32, #[cfg_attr(feature = "serde", serde(rename = "type"))] - ty: Type, + pub ty: Type, } impl PortableType { @@ -248,11 +249,19 @@ impl PortableType { } /// Returns the index of the [`PortableType`]. + #[deprecated( + since = "2.5.0", + note = "Prefer to access the fields directly; this getter will be removed in the next major version" + )] pub fn id(&self) -> u32 { self.id } /// Returns the type of the [`PortableType`]. + #[deprecated( + since = "2.5.0", + note = "Prefer to access the fields directly; this getter will be removed in the next major version" + )] pub fn ty(&self) -> &Type { &self.ty } @@ -278,7 +287,7 @@ impl PortableRegistryBuilder { /// /// If the type is already registered it will return the existing ID. pub fn register_type(&mut self, ty: Type) -> u32 { - self.types.intern_or_get(ty).1.into_untracked().id() + self.types.intern_or_get(ty).1.into_untracked().id } /// Returns the type id that would be assigned to a newly registered type. @@ -632,10 +641,10 @@ mod tests { let readonly: PortableRegistry = registry.into(); - assert_eq!(4, readonly.types().len()); + assert_eq!(4, readonly.types.len()); - for (expected, ty) in readonly.types().iter().enumerate() { - assert_eq!(expected as u32, ty.id()); + for (expected, ty) in readonly.types.iter().enumerate() { + assert_eq!(expected as u32, ty.id); } } diff --git a/src/registry.rs b/src/registry.rs index bda7993f..a5304b71 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -215,9 +215,9 @@ mod tests { let type_id = registry.register_type(&meta_type::()); let recursive = registry.types.get(&type_id).unwrap(); - if let TypeDef::Composite(composite) = recursive.type_def() { - for field in composite.fields() { - assert_eq!(*field.ty(), type_id) + if let TypeDef::Composite(composite) = &recursive.type_def { + for field in &composite.fields { + assert_eq!(field.ty, type_id) } } else { panic!("Should be a composite type definition") diff --git a/src/ty/composite.rs b/src/ty/composite.rs index 26d8852a..de18b503 100644 --- a/src/ty/composite.rs +++ b/src/ty/composite.rs @@ -76,7 +76,7 @@ pub struct TypeDefComposite { feature = "serde", serde(skip_serializing_if = "Vec::is_empty", default) )] - pub(crate) fields: Vec>, + pub fields: Vec>, } impl IntoPortable for TypeDefComposite { @@ -109,6 +109,10 @@ where T: Form, { /// Returns the fields of the composite type. + #[deprecated( + since = "2.5.0", + note = "Prefer to access the fields directly; this getter will be removed in the next major version" + )] pub fn fields(&self) -> &[Field] { &self.fields } diff --git a/src/ty/fields.rs b/src/ty/fields.rs index 5d8d0029..9e944e79 100644 --- a/src/ty/fields.rs +++ b/src/ty/fields.rs @@ -78,22 +78,22 @@ pub struct Field { feature = "serde", serde(skip_serializing_if = "Option::is_none", default) )] - pub(crate) name: Option, + pub name: Option, /// The type of the field. #[cfg_attr(feature = "serde", serde(rename = "type"))] - pub(crate) ty: T::Type, + pub ty: T::Type, /// The name of the type of the field as it appears in the source code. #[cfg_attr( feature = "serde", serde(skip_serializing_if = "Option::is_none", default) )] - pub(crate) type_name: Option, + pub type_name: Option, /// Documentation #[cfg_attr( feature = "serde", serde(skip_serializing_if = "Vec::is_empty", default) )] - pub(crate) docs: Vec, + pub docs: Vec, } impl IntoPortable for Field { @@ -141,11 +141,19 @@ where T: Form, { /// Returns the name of the field. None for unnamed fields. + #[deprecated( + since = "2.5.0", + note = "Prefer to access the fields directly; this getter will be removed in the next major version" + )] pub fn name(&self) -> Option<&T::String> { self.name.as_ref() } /// Returns the type of the field. + #[deprecated( + since = "2.5.0", + note = "Prefer to access the fields directly; this getter will be removed in the next major version" + )] pub fn ty(&self) -> &T::Type { &self.ty } @@ -155,11 +163,19 @@ where /// name are not specified, but in practice will be the name of any valid /// type for a field. This is intended for informational and diagnostic /// purposes only. + #[deprecated( + since = "2.5.0", + note = "Prefer to access the fields directly; this getter will be removed in the next major version" + )] pub fn type_name(&self) -> Option<&T::String> { self.type_name.as_ref() } /// Returns the documentation of the field. + #[deprecated( + since = "2.5.0", + note = "Prefer to access the fields directly; this getter will be removed in the next major version" + )] pub fn docs(&self) -> &[T::String] { &self.docs } diff --git a/src/ty/mod.rs b/src/ty/mod.rs index 56bf4c71..08375ead 100644 --- a/src/ty/mod.rs +++ b/src/ty/mod.rs @@ -68,22 +68,22 @@ pub struct Type { feature = "serde", serde(skip_serializing_if = "Path::is_empty", default) )] - pub(crate) path: Path, + pub path: Path, /// The generic type parameters of the type in use. Empty for non generic types #[cfg_attr( feature = "serde", serde(rename = "params", skip_serializing_if = "Vec::is_empty", default) )] - pub(crate) type_params: Vec>, + pub type_params: Vec>, /// The actual type definition #[cfg_attr(feature = "serde", serde(rename = "def"))] - pub(crate) type_def: TypeDef, + pub type_def: TypeDef, /// Documentation #[cfg_attr( feature = "serde", serde(skip_serializing_if = "Vec::is_empty", default) )] - pub(crate) docs: Vec, + pub docs: Vec, } impl IntoPortable for Type { @@ -161,21 +161,37 @@ where T: Form, { /// Returns the path of the type + #[deprecated( + since = "2.5.0", + note = "Prefer to access the fields directly; this getter will be removed in the next major version" + )] pub fn path(&self) -> &Path { &self.path } /// Returns the generic type parameters of the type + #[deprecated( + since = "2.5.0", + note = "Prefer to access the fields directly; this getter will be removed in the next major version" + )] pub fn type_params(&self) -> &[TypeParameter] { &self.type_params } /// Returns the definition of the type + #[deprecated( + since = "2.5.0", + note = "Prefer to access the fields directly; this getter will be removed in the next major version" + )] pub fn type_def(&self) -> &TypeDef { &self.type_def } /// Returns the documentation of the type + #[deprecated( + since = "2.5.0", + note = "Prefer to access the fields directly; this getter will be removed in the next major version" + )] pub fn docs(&self) -> &[T::String] { &self.docs } @@ -194,12 +210,12 @@ where #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, From, Debug, Encode)] pub struct TypeParameter { /// The name of the generic type parameter e.g. "T". - pub(crate) name: T::String, + pub name: T::String, /// The concrete type for the type parameter. /// /// `None` if the type parameter is skipped. #[cfg_attr(feature = "serde", serde(rename = "type"))] - pub(crate) ty: Option, + pub ty: Option, } impl IntoPortable for TypeParameter { @@ -240,11 +256,19 @@ where /// Get the type of the parameter. /// /// `None` if the parameter is skipped. + #[deprecated( + since = "2.5.0", + note = "Prefer to access the fields directly; this getter will be removed in the next major version" + )] pub fn ty(&self) -> Option<&T::Type> { self.ty.as_ref() } /// Get the name of the parameter. + #[deprecated( + since = "2.5.0", + note = "Prefer to access the fields directly; this getter will be removed in the next major version" + )] pub fn name(&self) -> &T::String { &self.name } @@ -400,10 +424,10 @@ pub enum TypeDefPrimitive { #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Debug)] pub struct TypeDefArray { /// The length of the array type. - pub(crate) len: u32, + pub len: u32, /// The element type of the array type. #[cfg_attr(feature = "serde", serde(rename = "type"))] - pub(crate) type_param: T::Type, + pub type_param: T::Type, } impl IntoPortable for TypeDefArray { @@ -428,11 +452,19 @@ where } /// Returns the length of the array type. + #[deprecated( + since = "2.5.0", + note = "Prefer to access the fields directly; this getter will be removed in the next major version" + )] pub fn len(&self) -> u32 { self.len } /// Returns the element type of the array type. + #[deprecated( + since = "2.5.0", + note = "Prefer to access the fields directly; this getter will be removed in the next major version" + )] pub fn type_param(&self) -> &T::Type { &self.type_param } @@ -452,7 +484,7 @@ where #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Debug)] pub struct TypeDefTuple { /// The types of the tuple fields. - pub(crate) fields: Vec, + pub fields: Vec, } impl IntoPortable for TypeDefTuple { @@ -502,6 +534,10 @@ where T: Form, { /// Returns the types of the tuple fields. + #[deprecated( + since = "2.5.0", + note = "Prefer to access the fields directly; this getter will be removed in the next major version" + )] pub fn fields(&self) -> &[T::Type] { &self.fields } @@ -514,7 +550,7 @@ where pub struct TypeDefSequence { /// The element type of the sequence type. #[cfg_attr(feature = "serde", serde(rename = "type"))] - pub(crate) type_param: T::Type, + pub type_param: T::Type, } impl IntoPortable for TypeDefSequence { @@ -552,6 +588,10 @@ where } /// Returns the element type of the sequence type. + #[deprecated( + since = "2.5.0", + note = "Prefer to access the fields directly; this getter will be removed in the next major version" + )] pub fn type_param(&self) -> &T::Type { &self.type_param } @@ -564,7 +604,7 @@ where pub struct TypeDefCompact { /// The type wrapped in [`Compact`], i.e. the `T` in `Compact`. #[cfg_attr(feature = "serde", serde(rename = "type"))] - pub(crate) type_param: T::Type, + pub type_param: T::Type, } impl IntoPortable for TypeDefCompact { @@ -587,6 +627,10 @@ where } /// Returns the [`Compact`] wrapped type, i.e. the `T` in `Compact`. + #[deprecated( + since = "2.5.0", + note = "Prefer to access the fields directly; this getter will be removed in the next major version" + )] pub fn type_param(&self) -> &T::Type { &self.type_param } @@ -603,9 +647,9 @@ where #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Debug)] pub struct TypeDefBitSequence { /// The type implementing [`bitvec::store::BitStore`]. - pub(crate) bit_store_type: T::Type, + pub bit_store_type: T::Type, /// The type implementing [`bitvec::order::BitOrder`]. - pub(crate) bit_order_type: T::Type, + pub bit_order_type: T::Type, } impl IntoPortable for TypeDefBitSequence { @@ -624,11 +668,19 @@ where T: Form, { /// Returns the type of the bit ordering of the [`::bitvec::vec::BitVec`]. + #[deprecated( + since = "2.5.0", + note = "Prefer to access the fields directly; this getter will be removed in the next major version" + )] pub fn bit_order_type(&self) -> &T::Type { &self.bit_order_type } /// Returns underlying type used to store the [`::bitvec::vec::BitVec`]. + #[deprecated( + since = "2.5.0", + note = "Prefer to access the fields directly; this getter will be removed in the next major version" + )] pub fn bit_store_type(&self) -> &T::Type { &self.bit_store_type } diff --git a/src/ty/path.rs b/src/ty/path.rs index 564f2a86..872a5774 100644 --- a/src/ty/path.rs +++ b/src/ty/path.rs @@ -59,7 +59,7 @@ use serde::{ #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Encode)] pub struct Path { /// The segments of the namespace. - segments: Vec, + pub segments: Vec, } impl Default for Path @@ -158,6 +158,10 @@ where } /// Returns the segments of the Path + #[deprecated( + since = "2.5.0", + note = "Prefer to access the fields directly; this getter will be removed in the next major version" + )] pub fn segments(&self) -> &[T::String] { &self.segments } diff --git a/src/ty/variant.rs b/src/ty/variant.rs index fb4944c2..a7f72de4 100644 --- a/src/ty/variant.rs +++ b/src/ty/variant.rs @@ -88,7 +88,7 @@ pub struct TypeDefVariant { feature = "serde", serde(skip_serializing_if = "Vec::is_empty", default) )] - pub(crate) variants: Vec>, + pub variants: Vec>, } impl IntoPortable for TypeDefVariant { @@ -121,6 +121,10 @@ where T: Form, { /// Returns the variants of a variant type + #[deprecated( + since = "2.5.0", + note = "Prefer to access the fields directly; this getter will be removed in the next major version" + )] pub fn variants(&self) -> &[Variant] { &self.variants } @@ -212,21 +216,37 @@ where T: Form, { /// Returns the name of the variant. + #[deprecated( + since = "2.5.0", + note = "Prefer to access the fields directly; this getter will be removed in the next major version" + )] pub fn name(&self) -> &T::String { &self.name } /// Returns the fields of the struct variant. + #[deprecated( + since = "2.5.0", + note = "Prefer to access the fields directly; this getter will be removed in the next major version" + )] pub fn fields(&self) -> &[Field] { &self.fields } /// Returns the index of the variant. + #[deprecated( + since = "2.5.0", + note = "Prefer to access the fields directly; this getter will be removed in the next major version" + )] pub fn index(&self) -> u8 { self.index } /// Returns the documentation of the variant. + #[deprecated( + since = "2.5.0", + note = "Prefer to access the fields directly; this getter will be removed in the next major version" + )] pub fn docs(&self) -> &[T::String] { &self.docs }