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

switch fn_type_info to a FnMut #166

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 13 additions & 5 deletions src/meta_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::sync::{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remember that this needs to work inside of Substrate runtimes which are no_std.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A right. Then this won't fly. Not sure how we can pull this off then...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed it is a tricky problem, needs some thinking about...

Copy link
Member Author

@xermicus xermicus Aug 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I can get this to work by implementing it as an n-ary function, I'm experimenting RN

Arc,
Mutex,
};

use crate::prelude::{
cmp::Ordering,
fmt::{
Expand Down Expand Up @@ -39,10 +44,10 @@ use crate::{
///
/// This needs a conversion to another representation of types
/// in order to be serializable.
#[derive(Clone, Copy)]
#[derive(Clone)]
pub struct MetaType {
/// Function pointer to get type information.
fn_type_info: fn() -> Type<MetaForm>,
fn_type_info: Arc<Mutex<dyn FnMut() -> Type<MetaForm>>>,
// The standard type ID (ab)used in order to provide
// cheap implementations of the standard traits
// such as `PartialEq`, `PartialOrd`, `Debug` and `Hash`.
Expand Down Expand Up @@ -91,15 +96,18 @@ impl MetaType {
T: TypeInfo + ?Sized + 'static,
{
Self {
fn_type_info: <T as TypeInfo>::type_info,
fn_type_info: Arc::new(Mutex::new(<T as TypeInfo>::type_info)),
type_id: TypeId::of::<T::Identity>(),
}
}

/// 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 {
pub fn new_custom(
type_id: u64,
fn_type_info: Arc<Mutex<dyn FnMut() -> Type<MetaForm>>>,
) -> Self {
Self {
fn_type_info,
type_id: TypeId::Custom(type_id),
Expand All @@ -108,7 +116,7 @@ impl MetaType {

/// Returns the meta type information.
pub fn type_info(&self) -> Type<MetaForm> {
(self.fn_type_info)()
(self.fn_type_info.lock().unwrap())()
}

/// Returns the type identifier provided by `core::any`.
Expand Down
17 changes: 13 additions & 4 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ use crate::{
};
use core::marker::PhantomData;
use scale::Compact;
use std::num::NonZeroU32;
use std::{
num::NonZeroU32,
sync::{
Arc,
Mutex,
},
};

fn assert_type<T, E>(expected: E)
where
Expand Down Expand Up @@ -371,9 +377,12 @@ fn runtime_meta_type() {
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())
f.ty_meta(MetaType::new_custom(
custom_u32_id,
Arc::new(Mutex::new(custom_u32_type_info)),
))
.name(i.to_string())
.type_name("custom_u32".to_string())
})
}
let _ty = TypeBuilder::default()
Expand Down