Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add StaticTypeInfo trait
  • Loading branch information
ascjones committed May 19, 2021
commit 39901fe16de4f6024978152398d62b2e70b50217
18 changes: 16 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,24 @@ 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.
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 + StaticTypeInfo,
{
MetaType::new::<T>()
}