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

Add StaticTypeInfo convenience trait #91

Merged
merged 9 commits into from
May 19, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ struct Foo<T> {

impl<T> TypeInfo for Foo<T>
where
T: TypeInfo,
T: TypeInfo + 'static,
{
type Identity = Self;

Expand Down Expand Up @@ -146,7 +146,7 @@ enum Foo<T>{

impl<T> TypeInfo for Foo<T>
where
T: TypeInfo,
T: TypeInfo + 'static,
{
type Identity = Self;

Expand Down
9 changes: 5 additions & 4 deletions derive/src/trait_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use syn::{

use crate::utils;

/// Generates a where clause for a `TypeInfo` impl, adding `TypeInfo` bounds to all
/// Generates a where clause for a `TypeInfo` impl, adding `TypeInfo + 'static` bounds to all
/// relevant generic types including associated types (e.g. `T::A: TypeInfo`), correctly dealing
/// with self-referential types.
pub fn make_where_clause<'a>(
Expand Down Expand Up @@ -66,25 +66,26 @@ pub fn make_where_clause<'a>(

types.into_iter().for_each(|(ty, is_compact)| {
// Compact types need extra bounds, T: HasCompact and <T as
// HasCompact>::Type: TypeInfo
// HasCompact>::Type: TypeInfo + 'static
if is_compact {
where_clause
.predicates
.push(parse_quote!(#ty : :: #parity_scale_codec ::HasCompact));
where_clause
.predicates
.push(parse_quote!(<#ty as :: #parity_scale_codec ::HasCompact>::Type : :: #scale_info ::TypeInfo));
.push(parse_quote!(<#ty as :: #parity_scale_codec ::HasCompact>::Type : :: #scale_info ::TypeInfo + 'static));
} else {
where_clause
.predicates
.push(parse_quote!(#ty : :: #scale_info ::TypeInfo));
.push(parse_quote!(#ty : :: #scale_info ::TypeInfo + 'static));
}
});

generics.type_params().into_iter().for_each(|type_param| {
let ident = type_param.ident.clone();
let mut bounds = type_param.bounds.clone();
bounds.push(parse_quote!(:: #scale_info ::TypeInfo));
bounds.push(parse_quote!('static));
where_clause
.predicates
.push(parse_quote!( #ident : #bounds));
Expand Down
12 changes: 6 additions & 6 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
//!
//! impl<T> TypeInfo for Foo<T>
//! where
//! T: TypeInfo,
//! T: TypeInfo + 'static,
//! {
//! type Identity = Self;
//!
Expand Down Expand Up @@ -74,7 +74,7 @@
//!
//! impl<T> TypeInfo for Foo<T>
//! where
//! T: TypeInfo,
//! T: TypeInfo + 'static,
//! {
//! type Identity = Self;
//!
Expand Down Expand Up @@ -254,7 +254,7 @@ impl FieldsBuilder<NamedFields> {
/// Add a named field with the type of the type parameter `T`
pub fn field_of<T>(mut self, name: &'static str, type_name: &'static str) -> Self
where
T: TypeInfo + ?Sized,
T: TypeInfo + ?Sized + 'static,
{
self.fields.push(Field::named_of::<T>(name, type_name));
self
Expand All @@ -264,7 +264,7 @@ impl FieldsBuilder<NamedFields> {
pub fn compact_of<T>(mut self, name: &'static str, type_name: &'static str) -> Self
where
T: scale::HasCompact,
<T as scale::HasCompact>::Type: TypeInfo,
<T as scale::HasCompact>::Type: TypeInfo + 'static,
{
self.fields
.push(Field::compact_of::<T>(Some(name), type_name));
Expand All @@ -276,7 +276,7 @@ impl FieldsBuilder<UnnamedFields> {
/// Add an unnamed field with the type of the type parameter `T`
pub fn field_of<T>(mut self, type_name: &'static str) -> Self
where
T: TypeInfo + ?Sized,
T: TypeInfo + ?Sized + 'static,
{
self.fields.push(Field::unnamed_of::<T>(type_name));
self
Expand All @@ -286,7 +286,7 @@ impl FieldsBuilder<UnnamedFields> {
pub fn compact_of<T>(mut self, type_name: &'static str) -> Self
where
T: scale::HasCompact,
<T as scale::HasCompact>::Type: TypeInfo,
<T as scale::HasCompact>::Type: TypeInfo + 'static,
{
self.fields.push(Field::compact_of::<T>(None, type_name));
self
Expand Down
36 changes: 18 additions & 18 deletions src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl_metadata_for_primitives!(
i128 => TypeDefPrimitive::I128,
);

impl<T: TypeInfo, const N: usize> TypeInfo for [T; N] {
impl<T: TypeInfo + 'static, const N: usize> TypeInfo for [T; N] {
type Identity = Self;

fn type_info() -> Type {
Expand All @@ -81,7 +81,7 @@ macro_rules! impl_metadata_for_tuple {
impl<$($ty),*> TypeInfo for ($($ty,)*)
where
$(
$ty: TypeInfo,
$ty: TypeInfo+ 'static,
)*
{
type Identity = Self;
Expand Down Expand Up @@ -113,7 +113,7 @@ impl_metadata_for_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P);

impl<T> TypeInfo for Vec<T>
where
T: TypeInfo,
T: TypeInfo + 'static,
{
type Identity = [T];

Expand All @@ -124,7 +124,7 @@ where

impl<T> TypeInfo for Option<T>
where
T: TypeInfo,
T: TypeInfo + 'static,
{
type Identity = Self;

Expand All @@ -142,8 +142,8 @@ where

impl<T, E> TypeInfo for Result<T, E>
where
T: TypeInfo,
E: TypeInfo,
T: TypeInfo + 'static,
E: TypeInfo + 'static,
{
type Identity = Self;

Expand All @@ -161,7 +161,7 @@ where

impl<T> TypeInfo for Cow<'static, T>
where
T: ToOwned + TypeInfo + ?Sized,
T: ToOwned + TypeInfo + ?Sized + 'static,
{
type Identity = Self;

Expand All @@ -175,8 +175,8 @@ where

impl<K, V> TypeInfo for BTreeMap<K, V>
where
K: TypeInfo,
V: TypeInfo,
K: TypeInfo + 'static,
V: TypeInfo + 'static,
{
type Identity = Self;

Expand All @@ -190,7 +190,7 @@ where

impl<T> TypeInfo for BTreeSet<T>
where
T: TypeInfo,
T: TypeInfo + 'static,
{
type Identity = Self;

Expand All @@ -204,7 +204,7 @@ where

impl<T> TypeInfo for Box<T>
where
T: TypeInfo + ?Sized,
T: TypeInfo + ?Sized + 'static,
{
type Identity = T;

Expand All @@ -213,9 +213,9 @@ where
}
}

impl<T> TypeInfo for &'static T
impl<T> TypeInfo for &T
where
T: TypeInfo + ?Sized,
T: TypeInfo + ?Sized + 'static,
{
type Identity = T;

Expand All @@ -224,9 +224,9 @@ where
}
}

impl<T> TypeInfo for &'static mut T
impl<T> TypeInfo for &mut T
where
T: TypeInfo + ?Sized,
T: TypeInfo + ?Sized + 'static,
{
type Identity = T;

Expand All @@ -237,7 +237,7 @@ where

impl<T> TypeInfo for [T]
where
T: TypeInfo,
T: TypeInfo + 'static,
{
type Identity = Self;

Expand All @@ -264,7 +264,7 @@ impl TypeInfo for String {

impl<T> TypeInfo for PhantomData<T>
where
T: TypeInfo + ?Sized,
T: TypeInfo + ?Sized + 'static,
{
type Identity = Self;

Expand All @@ -275,7 +275,7 @@ where

impl<T> TypeInfo for scale::Compact<T>
where
T: TypeInfo,
T: TypeInfo + 'static,
{
type Identity = Self;
fn type_info() -> Type {
Expand Down
15 changes: 13 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ pub use self::{
pub use scale_info_derive::TypeInfo;

/// Implementors return their meta type information.
pub trait TypeInfo: 'static {
pub trait TypeInfo {
/// The type identifying for which type info is provided.
///
/// # Note
Expand All @@ -143,10 +143,21 @@ pub trait TypeInfo: 'static {
fn type_info() -> Type;
}

/// Convenience trait for implementors, combining `TypeInfo` and `'static` bounds.
///
/// # Note
///
/// Currently because of the `'static` constraint on [`std::any::TypeId::of`] (see [`MetaType`]),
/// `TypeInfo` constraints must also be accompanied by a `'static` bound. This trait is useful to
/// for implementors so only a single constraint is required.
ascjones marked this conversation as resolved.
Show resolved Hide resolved
pub trait StaticTypeInfo: TypeInfo + 'static {}

impl<T> StaticTypeInfo for T where T: TypeInfo + 'static {}

/// Returns the runtime bridge to the types compile-time type information.
pub fn meta_type<T>() -> MetaType
where
T: ?Sized + TypeInfo,
T: ?Sized + TypeInfo + 'static,
{
MetaType::new::<T>()
}
2 changes: 1 addition & 1 deletion src/meta_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl MetaType {
/// Creates a new meta type from the given compile-time known type.
pub fn new<T>() -> Self
where
T: TypeInfo + ?Sized,
T: TypeInfo + ?Sized + 'static,
{
Self {
fn_type_info: <T as TypeInfo>::type_info,
Expand Down
4 changes: 2 additions & 2 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ fn struct_with_generics() {

impl<T> TypeInfo for MyStruct<T>
where
T: TypeInfo,
T: TypeInfo + 'static,
{
type Identity = Self;

Expand Down Expand Up @@ -193,7 +193,7 @@ fn basic_struct_with_phantoms() {

impl<T> TypeInfo for SomeStruct<T>
where
T: TypeInfo,
T: TypeInfo + 'static,
{
type Identity = Self;

Expand Down
6 changes: 3 additions & 3 deletions src/ty/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl Field {
/// compile-time type.
pub fn named_of<T>(name: &'static str, type_name: &'static str) -> Field
where
T: TypeInfo + ?Sized,
T: TypeInfo + ?Sized + 'static,
{
Self::new(Some(name), MetaType::new::<T>(), type_name)
}
Expand All @@ -126,7 +126,7 @@ impl Field {
/// given compile-time type.
pub fn unnamed_of<T>(type_name: &'static str) -> Field
where
T: TypeInfo + ?Sized,
T: TypeInfo + ?Sized + 'static,
{
Self::new(None, MetaType::new::<T>(), type_name)
}
Expand All @@ -135,7 +135,7 @@ impl Field {
pub fn compact_of<T>(name: Option<&'static str>, type_name: &'static str) -> Field
where
T: HasCompact,
<T as HasCompact>::Type: TypeInfo,
<T as HasCompact>::Type: TypeInfo + 'static,
{
Self::new(name, MetaType::new::<<T as HasCompact>::Type>(), type_name)
}
Expand Down
2 changes: 1 addition & 1 deletion src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ impl TypeDefSequence {
/// compile-time type.
pub fn of<T>() -> Self
where
T: TypeInfo,
T: TypeInfo + 'static,
{
Self::new(MetaType::new::<T>())
}
Expand Down
16 changes: 0 additions & 16 deletions test_suite/tests/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,22 +231,6 @@ fn fields_with_type_alias() {
assert_type!(S, ty);
}

#[test]
fn basic_reference_with_lifetime() {
#[allow(unused)]
#[derive(TypeInfo)]
struct S<'a, T> {
f: &'a T,
}

let struct_type = Type::builder()
.path(Path::new("S", "derive"))
.type_params(tuple_meta_type!(u32))
.composite(Fields::named().field_of::<&'static u32>("f", "&'static T"));

assert_type!(S<u32>, struct_type);
}

#[test]
fn associated_types_derive_without_bounds() {
trait Types {
Expand Down
2 changes: 1 addition & 1 deletion test_suite/tests/ui/fail_missing_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ struct Cat<Tail, Ear, Paw> {
paws: PawType<Paw>,
}

fn assert_type_info<T: TypeInfo>() {}
fn assert_type_info<T: TypeInfo + 'static>() {}

fn main() {
assert_type_info::<Cat<bool, u8, u16>>();
Expand Down
2 changes: 1 addition & 1 deletion test_suite/tests/ui/fail_missing_derive.stderr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
error[E0277]: the trait bound `PawType<u16>: TypeInfo` is not satisfied
--> $DIR/fail_missing_derive.rs:17:5
|
14 | fn assert_type_info<T: TypeInfo>() {}
14 | fn assert_type_info<T: TypeInfo + 'static>() {}
| -------- required by this bound in `assert_type_info`
...
17 | assert_type_info::<Cat<bool, u8, u16>>();
Expand Down
2 changes: 1 addition & 1 deletion test_suite/tests/ui/fail_non_static_lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ struct Me<'a> {
me: &'a Me<'a>,
}

fn assert_type_info<T: TypeInfo>() {}
fn assert_type_info<T: TypeInfo + 'static>() {}

fn main() {
assert_type_info::<Me>();
Expand Down
Loading