Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Construct PortableRegistry dynamically at runtime #164

Merged
merged 51 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
8345a3c
WIP: allow constructing MetaTypes at runtime
ascjones Aug 11, 2022
3a14663
Remove fmt config options
ascjones Aug 11, 2022
1e03bd3
Add some docs
ascjones Aug 11, 2022
a68f28c
std feature String
ascjones Aug 11, 2022
da92e9d
fully qualified String
ascjones Aug 11, 2022
592736a
Fix features build
ascjones Aug 11, 2022
4500520
Generic string parameter for MetaType
ascjones Aug 15, 2022
a08ef8a
Remove license_template_path
ascjones Aug 15, 2022
3cacf78
Fmt
ascjones Aug 15, 2022
6476671
Fix fmt
ascjones Aug 15, 2022
6a1343b
Replace MetaFormString trait with cfg based string
ascjones Aug 16, 2022
42db0e3
Remove unused into
ascjones Aug 16, 2022
0c525c6
Fix more errors
ascjones Aug 16, 2022
7dfe55b
Now string types are the same across Meta/Portable, no longer necessa…
ascjones Aug 17, 2022
54ff5fc
Remove custom MetaType and TypeId
ascjones Aug 22, 2022
16e4e79
EXPERIMENT: make all type def fields public to allow construting Port…
ascjones Aug 22, 2022
c46186d
Rename test
ascjones Aug 22, 2022
5b47b1b
Fix up type ids in test
ascjones Aug 22, 2022
40b6047
Fix ui test
ascjones Aug 22, 2022
8255c99
*TEMPORARILY* pub registry for PortableType
ascjones Aug 23, 2022
79c19a9
add constructor for portableregistry
xermicus Aug 28, 2022
d8c3b5e
constructor for PortableType
xermicus Aug 28, 2022
0e4737f
implement remaining constructors for generic Form
xermicus Aug 28, 2022
8319875
make Type::new constructor public
xermicus Aug 28, 2022
ef4c3ee
make remaining constructor public
xermicus Aug 28, 2022
9e48542
add a new custom constructor for path
xermicus Sep 22, 2022
b900f54
Merge branch 'master' into aj/custom-meta-types
xermicus Sep 22, 2022
0b45a89
use new_custom in a test
xermicus Sep 22, 2022
6694dc0
Remove not required builder fn for MetaType
ascjones Sep 22, 2022
83a80ef
WIP propogate Form through builders
ascjones Oct 4, 2022
ae22be7
Fix up path builders
ascjones Oct 4, 2022
ababa57
More fixes
ascjones Oct 4, 2022
383399c
Fix up generated From impls
ascjones Oct 4, 2022
221c2f9
Fix up field builders
ascjones Oct 4, 2022
6caa3d6
Fix up field builder methods for MetaForm
ascjones Oct 4, 2022
a36540a
Rename constructor
ascjones Oct 4, 2022
b027ae8
Hide pub fields and add portable builder helper methods
ascjones Oct 4, 2022
1de0508
Fmt
ascjones Oct 4, 2022
f04d4ef
Explicity export PortableType
ascjones Oct 4, 2022
23c6cb9
Clippy
ascjones Oct 4, 2022
590bc58
Move PortableRegistry to own file, introduce PortableRegistryBuilder
ascjones Oct 5, 2022
b5b5a6f
Remove some stray `Str` type params
ascjones Oct 5, 2022
3c9d8dd
Implement PortableRegistryBuilder to manage ids
ascjones Oct 5, 2022
a2668a9
Default impl for PortableRegistryBuilder
ascjones Oct 5, 2022
220a59d
implement getter for registered types in PortableRegistryBuilder
xermicus Oct 6, 2022
07057ce
Rename new path constructor
ascjones Oct 7, 2022
07aaef8
Rename new_custom methods to new_portable
ascjones Oct 7, 2022
192d12c
Revert reordering of MetaForm
ascjones Oct 7, 2022
403c720
Move path construction back to MetaForm for non-breaking changes
ascjones Oct 7, 2022
1044c8d
Update path tests
ascjones Oct 7, 2022
c9149c2
TypeParameter::new_portable for non breaking change
ascjones Oct 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove custom MetaType and TypeId
  • Loading branch information
ascjones committed Aug 22, 2022
commit 54ff5fc2b26310850427b074ed02220d478ebb88
23 changes: 4 additions & 19 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,12 @@ impl<T> FieldsBuilder<T> {
self.fields
}

fn push_field(&mut self, field: Field) {
fn push_field(mut self, field: Field) -> Self {
// filter out fields of PhantomData
if !field.ty().is_phantom() {
self.fields.push(field);
}
self
}
}

Expand All @@ -316,21 +317,7 @@ impl FieldsBuilder<NamedFields> {
-> FieldBuilder<field_state::NameAssigned, field_state::TypeAssigned>,
{
let builder = builder(FieldBuilder::new());
let mut this = self;
this.push_field(builder.finalize());
this
}

/// Add a named field constructed using the builder.
pub fn field_mut<F>(&mut self, builder: F)
where
F: Fn(
FieldBuilder,
)
-> FieldBuilder<field_state::NameAssigned, field_state::TypeAssigned>,
{
let builder = builder(FieldBuilder::new());
self.push_field(builder.finalize());
self.push_field(builder.finalize())
}
}

Expand All @@ -344,9 +331,7 @@ impl FieldsBuilder<UnnamedFields> {
-> FieldBuilder<field_state::NameNotAssigned, field_state::TypeAssigned>,
{
let builder = builder(FieldBuilder::new());
let mut this = self;
this.push_field(builder.finalize());
this
self.push_field(builder.finalize())
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@
//! Other forms, such as a portable form that is still bound to the registry
//! (also via lifetime tracking) are possible but current not needed.

use crate::prelude::fmt::Debug;
use crate::prelude::{
any::TypeId,
fmt::Debug,
};

use crate::{
interner::UntrackedSymbol,
meta_type::MetaType,
TypeId,
};

#[cfg(feature = "serde")]
Expand Down
19 changes: 0 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,25 +355,6 @@ pub use self::{
ty::*,
};

/// todo: docs
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub enum TypeId {
/// todo: docs
Rust(core::any::TypeId),
/// todo: docs
Custom(u64),
}

impl TypeId {
/// todo: docs
pub fn of<T>() -> Self
where
T: ?Sized + 'static,
{
Self::Rust(core::any::TypeId::of::<T>())
}
}

#[cfg(feature = "derive")]
pub use scale_info_derive::TypeInfo;

Expand Down
12 changes: 1 addition & 11 deletions src/meta_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

use crate::prelude::{
any::TypeId,
cmp::Ordering,
fmt::{
Debug,
Expand All @@ -28,7 +29,6 @@ use crate::prelude::{
use crate::{
form::MetaForm,
Type,
TypeId,
TypeInfo,
};

Expand Down Expand Up @@ -96,16 +96,6 @@ impl MetaType {
}
}

/// Creates a new meta type from the user supplied type id and type info function.
///
/// NOTE: It is the responsibility of the caller to ensure unique type ids per custom type.
pub fn new_custom(type_id: u64, fn_type_info: fn() -> Type<MetaForm>) -> Self {
Self {
fn_type_info,
type_id: TypeId::Custom(type_id),
}
}

/// Returns the meta type information.
pub fn type_info(&self) -> Type<MetaForm> {
(self.fn_type_info)()
Expand Down
4 changes: 2 additions & 2 deletions src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
//! prelude types that live in the so-called root namespace which is empty.

use crate::prelude::{
any::TypeId,
collections::BTreeMap,
fmt::Debug,
vec::Vec,
Expand All @@ -37,7 +38,6 @@ use crate::{
},
meta_type::MetaType,
Type,
TypeId,
};
use scale::Encode;

Expand Down Expand Up @@ -73,7 +73,7 @@ pub struct Registry {
/// The database where registered types reside.
///
/// The contents herein is used for serlialization.
types: BTreeMap<UntrackedSymbol<TypeId>, Type<PortableForm>>,
types: BTreeMap<UntrackedSymbol<core::any::TypeId>, Type<PortableForm>>,
}

impl Default for Registry {
Expand Down
38 changes: 25 additions & 13 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,17 +366,29 @@ fn basic_enum_with_index() {

#[test]
fn runtime_meta_type() {
let mut fields = Fields::named();
let custom_u32_id = 1;
let custom_u32_type_info = <u32 as TypeInfo>::type_info;
for i in 0..3 {
fields.field_mut(|f| {
f.ty_meta(MetaType::new_custom(custom_u32_id, custom_u32_type_info))
.name(i.to_string())
.type_name("custom_u32".to_string())
})
}
let _ty = TypeBuilder::default()
.path(Path::from_segments(vec!["MyCustomStruct".to_string()]).unwrap())
.composite(fields);
// let mut fields = Fields::named();
// let custom_u32_id = 1;
// let custom_u32_type_info = <u32 as TypeInfo>::type_info;
// let u32_meta_type = MetaType::new_custom(custom_u32_id, custom_u32_type_info);
// for i in 0..3 {
// fields.field_mut(|f| {
// f.ty_meta(u32_meta_type.clone())
// .name(i.to_string())
// .type_name("custom_u32".to_string())
// })
// }
// // add array type
// let arr_type_id = 2;
// let arr_type_info = || Type::from(TypeDefArray::new(32, u32_meta_type.clone()));
// let arr_meta_type = MetaType::new_custom(arr_type_id, arr_type_info);
//
// fields.field_mut(|f| {
// f.ty_meta(arr_meta_type)
// .name("arr")
// .type_name("custom_arr".to_string())
// });
//
// let _ty = TypeBuilder::default()
// .path(Path::from_segments(vec!["MyCustomStruct".to_string()]).unwrap())
// .composite(fields);
}