Skip to content

Commit

Permalink
ty: Make type fields public (#176)
Browse files Browse the repository at this point in the history
* portable: Expose types as mutable from the PortableRegistry

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* ty: Make type fields public

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* portable: Make `PortableType` public

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Deprecate getters

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Adjust codebase to use inner fields

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* portable: Deprecate PortableType id() and ty()

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* portable: Don't use deprecated getters

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

---------

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
  • Loading branch information
lexnv committed Mar 29, 2023
1 parent 842114e commit 78ff15a
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 49 deletions.
2 changes: 1 addition & 1 deletion src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ impl<F: Form, T> FieldsBuilder<F, T> {
impl<T> FieldsBuilder<MetaForm, T> {
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
Expand Down
6 changes: 5 additions & 1 deletion src/interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,17 @@ use serde::{
pub struct UntrackedSymbol<T> {
/// The index to the symbol in the interner table.
#[codec(compact)]
id: u32,
pub id: u32,
#[cfg_attr(feature = "serde", serde(skip))]
marker: PhantomData<fn() -> T>,
}

impl<T> UntrackedSymbol<T> {
/// 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
}
Expand Down
57 changes: 33 additions & 24 deletions src/portable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PortableType>,
/// The types contained by the [`PortableRegistry`].
pub types: Vec<PortableType>,
}

impl From<Registry> for PortableRegistry {
Expand All @@ -56,7 +57,7 @@ impl From<Registry> for PortableRegistry {
.types()
.map(|(k, v)| {
PortableType {
id: k.id(),
id: k.id,
ty: v.clone(),
}
})
Expand All @@ -68,10 +69,14 @@ impl From<Registry> 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<PortableForm>> {
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
}
Expand Down Expand Up @@ -121,31 +126,27 @@ 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) = &param.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);
}

// make sure any types inside this type are also retained and update the IDs:
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();
}
}
TypeDef::Variant(variant) => {
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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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<PortableForm>,
pub ty: Type<PortableForm>,
}

impl PortableType {
Expand All @@ -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<PortableForm> {
&self.ty
}
Expand All @@ -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<PortableForm>) -> 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.
Expand Down Expand Up @@ -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);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,9 @@ mod tests {
let type_id = registry.register_type(&meta_type::<RecursiveRefs>());

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")
Expand Down
6 changes: 5 additions & 1 deletion src/ty/composite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub struct TypeDefComposite<T: Form = MetaForm> {
feature = "serde",
serde(skip_serializing_if = "Vec::is_empty", default)
)]
pub(crate) fields: Vec<Field<T>>,
pub fields: Vec<Field<T>>,
}

impl IntoPortable for TypeDefComposite {
Expand Down Expand Up @@ -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<T>] {
&self.fields
}
Expand Down
24 changes: 20 additions & 4 deletions src/ty/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,22 @@ pub struct Field<T: Form = MetaForm> {
feature = "serde",
serde(skip_serializing_if = "Option::is_none", default)
)]
pub(crate) name: Option<T::String>,
pub name: Option<T::String>,
/// 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<T::String>,
pub type_name: Option<T::String>,
/// Documentation
#[cfg_attr(
feature = "serde",
serde(skip_serializing_if = "Vec::is_empty", default)
)]
pub(crate) docs: Vec<T::String>,
pub docs: Vec<T::String>,
}

impl IntoPortable for Field {
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down

0 comments on commit 78ff15a

Please sign in to comment.