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

Derive TypeInfo for own types #72

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
84b3bb4
Don't use a feature in the derive crate
dvdplm Mar 3, 2021
c2f0f06
Review grumbles
dvdplm Mar 3, 2021
b4ba154
Less noise is better: build a TokenStream2 containing the "::" prefix
dvdplm Mar 3, 2021
b1d26c2
Use `crate::` when deriving for `scale-info` itself
dvdplm Mar 3, 2021
22fb440
Derive TypeInfo for UntrackedSymbol
dvdplm Mar 4, 2021
00aae34
wip
dvdplm Mar 4, 2021
5a165b1
Add ref to ticket that blocks deriving Typeinfo for Type
dvdplm Mar 4, 2021
9ac6306
Remove TypeInfo impl for MetaType
dvdplm Mar 4, 2021
39f8459
Clippy is right
dvdplm Mar 4, 2021
7db6778
Cleanup
dvdplm Mar 4, 2021
687c757
Merge branch 'master' into dp-doogfood
dvdplm Mar 4, 2021
7f4fae7
Merge branch 'master' into dp-doogfood
dvdplm Mar 5, 2021
1d8ff8c
Merge branch 'master' into dp-doogfood
dvdplm Jun 25, 2021
5c22f19
Remove `NonZero*` impls
dvdplm Jun 25, 2021
58dc3d2
Cleanup
dvdplm Jun 25, 2021
e73c061
Don't bind <… as HasCompact>::Type
dvdplm Jun 25, 2021
03bf034
Merge remote-tracking branch 'origin/master' into dp-doogfood
dvdplm Jun 28, 2021
c7bfb52
Review grumbles
dvdplm Jun 28, 2021
89d4499
Fix comment
dvdplm Jun 29, 2021
14055e1
Merge branch 'master' into dp-doogfood
dvdplm Jun 29, 2021
c7c19fc
Address review feedback
dvdplm Jun 29, 2021
d3259c5
fmt
dvdplm Jun 29, 2021
54deaf4
Merge branch 'master' into dp-doogfood
dvdplm Aug 24, 2021
5fd7525
cleanup
dvdplm Aug 24, 2021
2d7530d
Merge branch 'master' into dp-doogfood
gilescope Jul 16, 2022
ba2e94a
Fixup after merge
gilescope Jul 16, 2022
3420903
additional support to allow frame-metadata to derive TypeInfo (#163)
gilescope Jul 19, 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
1 change: 1 addition & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ jobs:
- name: check-features
run: |
cargo check --no-default-features --features bit-vec
cargo check --no-default-features --features dogfood
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want to keep this optional crate feature? What purpose does it have?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is a good question. I fail to see the reason to keep this as a feature. @ascjones you ok making it non-optional?

Copy link
Contributor

Choose a reason for hiding this comment

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

Just saw Giles's PR with this stuff in and fwiw I think it doesn't need to be behind a feature flag.

cargo check --no-default-features --features docs
cargo check --no-default-features --features serde
cargo check --no-default-features --features serde,decode
Expand Down
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@ std = [
derive = [
"scale-info-derive"
]
# Derive `TypeInfo` for our own types.
dogfood = [
"derive"
]
# Include rustdoc strings in the type metadata.
docs = [
"scale-info-derive/docs"
]
# enables decoding and deserialization of portable scale-info type metadata
# Enables decoding and deserialization of portable scale-info type metadata.
decode = [
"scale/full"
]
Expand Down
41 changes: 33 additions & 8 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ struct TypeInfoImpl {
impl TypeInfoImpl {
fn parse(input: TokenStream2) -> Result<Self> {
let ast: DeriveInput = syn::parse2(input)?;

let attrs = attr::Attributes::from_ast(&ast)?;

Ok(Self { ast, attrs })
Expand All @@ -98,12 +99,16 @@ impl TypeInfoImpl {
let type_params = self.ast.generics.type_params().map(|tp| {
let ty_ident = &tp.ident;
let ty = if self.attrs.skip_type_params().map_or(true, |skip| !skip.skip(tp)) {
quote! { ::core::option::Option::Some(#scale_info::meta_type::<#ty_ident>()) }

quote! { ::core::option::Option::Some(:: #scale_info ::meta_type::<#ty_ident>()) }

} else {
quote! { ::core::option::Option::None }
};
quote! {
#scale_info::TypeParameter::new(::core::stringify!(#ty_ident), #ty)

:: #scale_info ::TypeParameter::new(::core::stringify!(#ty_ident), #ty)

}
});

Expand All @@ -117,12 +122,14 @@ impl TypeInfoImpl {
let docs = self.generate_docs(&self.ast.attrs);

Ok(quote! {
impl #impl_generics #scale_info::TypeInfo for #ident #ty_generics #where_clause {
impl #impl_generics :: #scale_info ::TypeInfo for #ident #ty_generics #where_clause {

type Identity = Self;

fn type_info() -> #scale_info::Type {
#scale_info::Type::builder()
.path(#scale_info::Path::new(::core::stringify!(#ident), ::core::module_path!()))
.type_params(#scale_info::prelude::vec![ #( #type_params ),* ])
#scale_info ::Type::builder()
.path(#scale_info ::Path::new(::core::stringify!(#ident), ::core::module_path!()))
.type_params(#scale_info ::prelude::vec![ #( #type_params ),* ])
#docs
.#build_type
}
Expand Down Expand Up @@ -152,7 +159,7 @@ impl TypeInfoImpl {
};

quote! {
composite(#scale_info::build::Fields::#fields)
composite(:: #scale_info ::build::Fields::#fields)
}
}

Expand Down Expand Up @@ -252,7 +259,8 @@ impl TypeInfoImpl {
});
quote! {
variant(
#scale_info::build::Variants::new()
:: #scale_info ::build::Variants::new()

#( #variants )*
)
}
Expand Down Expand Up @@ -294,6 +302,23 @@ impl TypeInfoImpl {
}
}

// /// Find the name given to the `scale-info` crate in the context we run in. If scale-info is not
// /// among the dependencies then we must be deriving types for the scale-info crate itself, in which
// /// case we need to refer to it using the reserved word "crate", so the object paths keep working.
// fn scale_info_crate_path() -> Result<syn::Path> {
// use proc_macro_crate::FoundCrate;
// const SCALE_INFO_CRATE_NAME: &str = "scale-info";

// let crate_ident = match proc_macro_crate::crate_name(SCALE_INFO_CRATE_NAME) {
// Ok(FoundCrate::Itself) => parse_quote! { crate },
// Ok(FoundCrate::Name(name)) => {
// let crate_ident = Ident::new(&name, Span::call_site());
// parse_quote! { :: #crate_ident }
// }
// Err(e) => return Err(syn::Error::new(Span::call_site(), e)),
// };
// Ok(crate_ident)

Copy link
Contributor

Choose a reason for hiding this comment

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

Wasn't entirely sure which version of this function to use so went with master. If that's fine we can delete this commented out version.

/// Get the name of a crate, to be robust against renamed dependencies.
fn crate_name_path(name: &str) -> Result<syn::Path> {
proc_macro_crate::crate_name(name)
Expand Down
1 change: 1 addition & 0 deletions src/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub trait Form {
/// through the registry and `IntoPortable`.
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug)]
#[cfg_attr(feature = "dogfood", derive(scale_info_derive::TypeInfo))]
pub enum MetaForm {}

impl Form for MetaForm {
Expand Down
1 change: 1 addition & 0 deletions src/interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ use serde::{
)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
#[cfg_attr(feature = "dogfood", derive(scale_info_derive::TypeInfo))]
pub struct UntrackedSymbol<T> {
/// The index to the symbol in the interner table.
#[codec(compact)]
Expand Down
1 change: 1 addition & 0 deletions src/ty/composite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ use serde::{
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
#[cfg_attr(any(feature = "std", feature = "decode"), derive(scale::Decode))]
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, From, Encode)]
#[cfg_attr(feature = "dogfood", derive(scale_info_derive::TypeInfo))]
pub struct TypeDefComposite<T: Form = MetaForm> {
/// The fields of the composite type.
#[cfg_attr(
Expand Down
1 change: 1 addition & 0 deletions src/ty/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ use serde::{
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[cfg_attr(any(feature = "std", feature = "decode"), derive(scale::Decode))]
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Encode)]
#[cfg_attr(feature = "dogfood", derive(scale_info_derive::TypeInfo))]
pub struct Field<T: Form = MetaForm> {
/// The name of the field. None for unnamed fields.
#[cfg_attr(
Expand Down
9 changes: 8 additions & 1 deletion src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub use self::{

/// A [`Type`] definition with optional metadata.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "dogfood", derive(scale_info_derive::TypeInfo))]
#[cfg_attr(
feature = "serde",
serde(bound(
Expand Down Expand Up @@ -119,7 +120,7 @@ impl_from_type_def_for_type!(
);

impl Type {
/// Create a [`TypeBuilder`](`crate::build::TypeBuilder`) the public API for constructing a [`Type`]
/// Create a [`TypeBuilder`](`crate::build::TypeBuilder`), the public API for constructing a [`Type`]
pub fn builder() -> TypeBuilder {
TypeBuilder::default()
}
Expand Down Expand Up @@ -244,6 +245,7 @@ where
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
#[cfg_attr(any(feature = "std", feature = "decode"), derive(scale::Decode))]
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, From, Debug, Encode)]
#[cfg_attr(feature = "dogfood", derive(scale_info_derive::TypeInfo))]
pub enum TypeDef<T: Form = MetaForm> {
/// A composite type (e.g. a struct or a tuple)
#[codec(index = 0)]
Expand Down Expand Up @@ -297,6 +299,7 @@ impl IntoPortable for TypeDef {
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
#[cfg_attr(any(feature = "std", feature = "decode"), derive(scale::Decode))]
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Debug)]
#[cfg_attr(feature = "dogfood", derive(scale_info_derive::TypeInfo))]
pub enum TypeDefPrimitive {
/// `bool` type
#[codec(index = 0)]
Expand Down Expand Up @@ -349,6 +352,7 @@ pub enum TypeDefPrimitive {
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(any(feature = "std", feature = "decode"), derive(scale::Decode))]
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Debug)]
#[cfg_attr(feature = "dogfood", derive(scale_info_derive::TypeInfo))]
pub struct TypeDefArray<T: Form = MetaForm> {
/// The length of the array type.
len: u32,
Expand Down Expand Up @@ -403,6 +407,7 @@ where
#[cfg_attr(feature = "serde", serde(transparent))]
#[cfg_attr(any(feature = "std", feature = "decode"), derive(scale::Decode))]
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Debug)]
#[cfg_attr(feature = "dogfood", derive(scale_info_derive::TypeInfo))]
pub struct TypeDefTuple<T: Form = MetaForm> {
/// The types of the tuple fields.
fields: Vec<T::Type>,
Expand Down Expand Up @@ -452,6 +457,7 @@ where
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(any(feature = "std", feature = "decode"), derive(scale::Decode))]
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Debug)]
#[cfg_attr(feature = "dogfood", derive(scale_info_derive::TypeInfo))]
pub struct TypeDefSequence<T: Form = MetaForm> {
/// The element type of the sequence type.
#[cfg_attr(feature = "serde", serde(rename = "type"))]
Expand Down Expand Up @@ -502,6 +508,7 @@ where
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(any(feature = "std", feature = "decode"), derive(scale::Decode))]
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Debug)]
#[cfg_attr(feature = "dogfood", derive(scale_info_derive::TypeInfo))]
pub struct TypeDefCompact<T: Form = MetaForm> {
/// The type wrapped in [`Compact`], i.e. the `T` in `Compact<T>`.
#[cfg_attr(feature = "serde", serde(rename = "type"))]
Expand Down
1 change: 1 addition & 0 deletions src/ty/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ use serde::{
#[cfg_attr(feature = "serde", serde(transparent))]
#[cfg_attr(any(feature = "std", feature = "decode"), derive(scale::Decode))]
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Encode)]
#[cfg_attr(feature = "dogfood", derive(scale_info_derive::TypeInfo))]
pub struct Path<T: Form = MetaForm> {
/// The segments of the namespace.
segments: Vec<T::String>,
Expand Down
2 changes: 2 additions & 0 deletions src/ty/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ use serde::{
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
#[cfg_attr(any(feature = "std", feature = "decode"), derive(scale::Decode))]
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, From, Encode)]
#[cfg_attr(feature = "dogfood", derive(scale_info_derive::TypeInfo))]
pub struct TypeDefVariant<T: Form = MetaForm> {
/// The variants of a variant type
#[cfg_attr(
Expand Down Expand Up @@ -148,6 +149,7 @@ where
)]
#[cfg_attr(any(feature = "std", feature = "decode"), derive(scale::Decode))]
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Encode)]
#[cfg_attr(feature = "dogfood", derive(scale_info_derive::TypeInfo))]
pub struct Variant<T: Form = MetaForm> {
/// The name of the variant.
name: T::String,
Expand Down