-
Notifications
You must be signed in to change notification settings - Fork 30
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
Add StaticTypeInfo
convenience trait
#91
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great. Gut reaction is "Why on earth didn't we do this sooner?", but maybe there was a reason? One since overcome?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it also makes sense to me
One con is that this design would disallow support for Another solution is to introduce another trait for dependencies that has pub trait StaticTypeInfo: scale_info::TypeInfo + 'static {}
impl<T> StaticTypeInfo for T
where
T: scale_info::TypeInfo + 'static,
{} In my opinion it is a good design principle to keep traits as open and non-restricted as possible. Introducing |
got convinced by Robbepop message above, didn't tought about type with lifetimes (I only thought about dyn stuff)
How so? Do you mean e.g. #[derive(TypeInfo)]
struct S<'a, T> {
f: &'a T,
} If so, that case is already handled because we restrict all lifetimes to #[allow(unused)]
struct S<'a, T> {
f: &'a T,
}
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
const _: () = {
impl<'a, T> ::scale_info::TypeInfo for S<'a, T>
where
'a: 'static, // <-------------------------------- lifetime bound to 'static
&'a T: ::scale_info::TypeInfo,
T: ::scale_info::TypeInfo,
{
type Identity = Self;
fn type_info() -> ::scale_info::Type {
::scale_info::Type::builder()
.path(::scale_info::Path::new("S", "derive"))
.type_params(<[_]>::into_vec(box [::scale_info::meta_type::<T>()]))
.composite(
::scale_info::build::Fields::named()
.field_of::<&'static T>("f", "&\'static T"),
)
}
};
}; Note that the current restriction to
That could work, it would leave |
This is just an implementation detail of how we currently implement this. If we go down the path to support references we could do much better than we do at the moment. The question is: Is it worthwhile. I think it isn't but our design should also not prevent future use cases in my opinion. Since this PR tackles a quality of life improvement I doubt that it will be a really good design decision in the long run. Rather I'd prefer the mentioned solution to be implemented by dependencies if they think it is useful to them but keep the base functionality clean and simple. If we stick to the notion of |
I think this PR is a little more than QoL and reducing noise (which is not bad per se). As @ascjones example illustrates there is an underlying assumption of things being That said, I do agree with the design principle of keeping traits as open as they can be, but I don't think that this should trump the ergonomics of the library. If we can make We don't have to make a 1.0 release that stays backwards compatible indefinitely, it is ok to make breaking changes when the circumstances warrant it, so I'm not overly concerned by "wrong" design decisions tbf. |
This is not an assumption resulting from the essence of what this trait represents but a mere implementation detail that could change. For example this is induced by the fact that the current implementaiton of fn meta_type_ref<'a, T>(ty: &'a T) -> TypeInfo
where
T: 'static, // Required for Rust's TypeInfo
{} With Rust specialization we wouldn't even need such a new function. |
'static
bound to TypeInfo
traitStaticTypeInfo
convenience trait
@Robbepop changed PR to just add this |
This reverts commit 73ea38f
What is the gain of adding this trait in |
I think a lot of dependency which implements TypeInfo on some types will like to make use of it, no ? |
Yeah just convenience really, because at the moment you need it everywhere (just search Also imagine if in the future we remove the |
I am not convinced to put this convenience trait into this crate but it sounds like a compromise to me to move forward. So I approve. |
Co-authored-by: David <dvdplm@gmail.com>
Adds a convenience trait
StaticTypeInfo
to avoid requiring the user to addTypeInfo + 'static
to generic bounds everywhere.This is done instead of adding it directly to
TypeInfo
to leave that trait open for a future where the'static
constraint is no longer required.