Skip to content

Commit

Permalink
Reexport scale dependencies, introduce #[ink::scale_derive] (#1890)
Browse files Browse the repository at this point in the history
* WIP

* Added derives, WIP search and replace of codegen

* Use ink reexported traits and derives in codegen

* Use new attribute macros everywhere

* Single attribute for deriving

* Fix type_info attribute

* Rename to scale_derive

* Use new macros

* Restore ink::Event derive

* Remove scale and scale-info dependencies

* Migrate Error type derives

* Fixing some UI tests

* Fixing some UI tests

* Examples dependencies

* Remove std feature for scale/scale-info integration-tests

* spellcheck

* Remove scale/std from last integration-test

* Fix contract_ref type_info

* Fix call_builder type_info

* Add UI tests

* Fix UI tests

* Fix integration-test

* Fix UI test

* Fmt

* Fix integration test

* Fix integration test

* Docs

* Fix integration-tests

* Fix integration-tests

* Fix integration-test

* CHANGELOG.md

* Fix

* fmt

* Fix integration test

* Fix integration tests

* Fix integration test

* Fix integration test

* Fix integration test

* Use CamelCase for `scale_derive`

* UI test
  • Loading branch information
ascjones authored Sep 1, 2023
1 parent ff4a0b5 commit 5fb5477
Show file tree
Hide file tree
Showing 121 changed files with 397 additions and 527 deletions.
2 changes: 2 additions & 0 deletions .config/cargo_spellcheck.dic
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,13 @@ reentrancy
refcount
scalability
scalable
scale_derive
sr25519
stdin
stdout
subber
tuple
type_info
unordered
untyped
v1
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Persist static buffer size in metadata - [#1880](https://github.com/paritytech/ink/pull/1880)
- Add backend choice to the E2E testcase configuration ‒ [#1864](https://github.com/paritytech/ink/pull/1864)

### Changed
- Reexport `scale` dependencies, introduce `#[ink::scale_derive]`[#1890](https://github.com/paritytech/ink/pull/1890)

### Added
- Schema generation - [#1765](https://github.com/paritytech/ink/pull/1765)
- Add `set_block_number` to off-chain test api `Engine` - [#1806](https://github.com/paritytech/ink/pull/1806)
Expand Down
4 changes: 2 additions & 2 deletions crates/env/src/call/create_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ where
/// #[ink(storage)]
/// pub struct Contract {}
///
/// #[derive(Debug, PartialEq, Eq, scale::Encode, scale::Decode)]
/// #[cfg_attr(feature = "std", derive(::scale_info::TypeInfo))]
/// #[derive(Debug, PartialEq, Eq)]
/// #[ink::scale_derive(Encode, Decode, TypeInfo)]
/// pub enum Error {
/// Foo,
/// }
Expand Down
5 changes: 3 additions & 2 deletions crates/ink/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ ink_prelude = { workspace = true }
ink_macro = { workspace = true }

scale = { package = "parity-scale-codec", workspace = true }
scale-info = { workspace = true, default-features = false, features = ["derive"], optional = true }
derive_more = { workspace = true, features = ["from"] }

[dev-dependencies]
ink_ir = { workspace = true, default-features = true }
ink_metadata = { workspace = true }

trybuild = { workspace = true, features = ["diff"] }
# Required for the doctest of `env_access::EnvAccess::instantiate_contract`
scale-info = { workspace = true, features = ["derive"] }


[features]
default = ["std"]
Expand All @@ -44,6 +44,7 @@ std = [
"ink_storage/std",
"ink_macro/std",
"scale/std",
"scale-info/std",
]
# Enable contract debug messages via `debug_print!` and `debug_println!`.
ink-debug = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,16 @@ impl CallBuilder<'_> {
/// messages and trait implementations in a type safe way.
#[repr(transparent)]
#[cfg_attr(feature = "std", derive(
::scale_info::TypeInfo,
::ink::storage::traits::StorageLayout,
))]
#[derive(
::core::fmt::Debug,
::scale::Encode,
::scale::Decode,
::core::hash::Hash,
::core::cmp::PartialEq,
::core::cmp::Eq,
::core::clone::Clone,
)]
#[::ink::scale_derive(Encode, Decode, TypeInfo)]
pub struct #cb_ident {
account_id: AccountId,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,16 @@ impl ContractRef<'_> {
let ref_ident = self.generate_contract_ref_ident();
quote_spanned!(span=>
#[cfg_attr(feature = "std", derive(
::scale_info::TypeInfo,
::ink::storage::traits::StorageLayout,
))]
#[derive(
::core::fmt::Debug,
::scale::Encode,
::scale::Decode,
::core::hash::Hash,
::core::cmp::PartialEq,
::core::cmp::Eq,
::core::clone::Clone,
)]
#[::ink::scale_derive(Encode, Decode, TypeInfo)]
#( #doc_attrs )*
pub struct #ref_ident {
inner: <#storage_ident as ::ink::codegen::ContractCallBuilder>::Type,
Expand All @@ -121,7 +119,7 @@ impl ContractRef<'_> {
impl<E> ::ink::env::call::ConstructorReturnType<#ref_ident>
for ::core::result::Result<#storage_ident, E>
where
E: ::scale::Decode
E: ::ink::scale::Decode
{
const IS_RESULT: bool = true;

Expand Down
4 changes: 1 addition & 3 deletions crates/ink/codegen/src/generator/chain_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,7 @@ impl GenerateCode for ChainExtension<'_> {
let instance_ident = format_ident!("__ink_{}Instance", ident);
quote_spanned!(span =>
#(#attrs)*
#[cfg_attr(feature = "std", derive(
::scale_info::TypeInfo,
))]
#[::ink::scale_derive(TypeInfo)]
pub enum #ident {}

const _: () = {
Expand Down
28 changes: 14 additions & 14 deletions crates/ink/codegen/src/generator/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ impl Dispatch<'_> {
#( #cfg_attrs )*
#const_ident => {
::core::result::Result::Ok(Self::#constructor_ident(
<#constructor_input as ::scale::Decode>::decode(input)
<#constructor_input as ::ink::scale::Decode>::decode(input)
.map_err(|_| ::ink::reflect::DispatchError::InvalidParameters)?
))
}
Expand All @@ -534,7 +534,7 @@ impl Dispatch<'_> {
);
quote! {
::core::result::Result::Ok(Self::#constructor_ident(
<#constructor_input as ::scale::Decode>::decode(input)
<#constructor_input as ::ink::scale::Decode>::decode(input)
.map_err(|_| ::ink::reflect::DispatchError::InvalidParameters)?
))
}
Expand Down Expand Up @@ -612,12 +612,12 @@ impl Dispatch<'_> {
fn decode_dispatch<I>(input: &mut I)
-> ::core::result::Result<Self, ::ink::reflect::DispatchError>
where
I: ::scale::Input,
I: ::ink::scale::Input,
{
#(
#constructor_selector
)*
match <[::core::primitive::u8; 4usize] as ::scale::Decode>::decode(input)
match <[::core::primitive::u8; 4usize] as ::ink::scale::Decode>::decode(input)
.map_err(|_| ::ink::reflect::DispatchError::InvalidSelector)?
{
#( #constructor_match , )*
Expand All @@ -626,10 +626,10 @@ impl Dispatch<'_> {
}
}

impl ::scale::Decode for __ink_ConstructorDecoder {
fn decode<I>(input: &mut I) -> ::core::result::Result<Self, ::scale::Error>
impl ::ink::scale::Decode for __ink_ConstructorDecoder {
fn decode<I>(input: &mut I) -> ::core::result::Result<Self, ::ink::scale::Error>
where
I: ::scale::Input,
I: ::ink::scale::Input,
{
<Self as ::ink::reflect::DecodeDispatch>::decode_dispatch(input)
.map_err(::core::convert::Into::into)
Expand Down Expand Up @@ -719,7 +719,7 @@ impl Dispatch<'_> {
#( #cfg_attrs )*
#const_ident => {
::core::result::Result::Ok(Self::#message_ident(
<#message_input as ::scale::Decode>::decode(input)
<#message_input as ::ink::scale::Decode>::decode(input)
.map_err(|_| ::ink::reflect::DispatchError::InvalidParameters)?
))
}
Expand All @@ -734,7 +734,7 @@ impl Dispatch<'_> {
expand_message_input(message_span, storage_ident, item.id.clone());
quote! {
::core::result::Result::Ok(Self::#message_ident(
<#message_input as ::scale::Decode>::decode(input)
<#message_input as ::ink::scale::Decode>::decode(input)
.map_err(|_| ::ink::reflect::DispatchError::InvalidParameters)?
))
}
Expand Down Expand Up @@ -809,12 +809,12 @@ impl Dispatch<'_> {
fn decode_dispatch<I>(input: &mut I)
-> ::core::result::Result<Self, ::ink::reflect::DispatchError>
where
I: ::scale::Input,
I: ::ink::scale::Input,
{
#(
#message_selector
)*
match <[::core::primitive::u8; 4usize] as ::scale::Decode>::decode(input)
match <[::core::primitive::u8; 4usize] as ::ink::scale::Decode>::decode(input)
.map_err(|_| ::ink::reflect::DispatchError::InvalidSelector)?
{
#( #message_match , )*
Expand All @@ -823,10 +823,10 @@ impl Dispatch<'_> {
}
}

impl ::scale::Decode for __ink_MessageDecoder {
fn decode<I>(input: &mut I) -> ::core::result::Result<Self, ::scale::Error>
impl ::ink::scale::Decode for __ink_MessageDecoder {
fn decode<I>(input: &mut I) -> ::core::result::Result<Self, ::ink::scale::Error>
where
I: ::scale::Input,
I: ::ink::scale::Input,
{
<Self as ::ink::reflect::DecodeDispatch>::decode_dispatch(input)
.map_err(::core::convert::Into::into)
Expand Down
3 changes: 2 additions & 1 deletion crates/ink/codegen/src/generator/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ impl GenerateCode for Event<'_> {
.then(|| quote::quote! { #[ink(anonymous)] });
quote::quote! (
#[cfg_attr(feature = "std", derive(::ink::EventMetadata))]
#[derive(::ink::Event, ::scale::Encode, ::scale::Decode)]
#[derive(::ink::Event)]
#[::ink::scale_derive(Encode, Decode)]
#anonymous
#item
)
Expand Down
2 changes: 1 addition & 1 deletion crates/ink/codegen/src/generator/storage_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ impl GenerateCode for StorageItem<'_> {
if self.item.config().derive() {
derive = quote! {
#[cfg_attr(feature = "std", derive(
::scale_info::TypeInfo,
::ink::storage::traits::StorageLayout,
))]
#[::ink::scale_derive(TypeInfo)]
#[derive(
::ink::storage::traits::StorableHint,
::ink::storage::traits::StorageKey,
Expand Down
13 changes: 5 additions & 8 deletions crates/ink/codegen/src/generator/trait_def/call_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,7 @@ impl CallBuilder<'_> {
/// All calls to types (contracts) implementing the trait will be built by this type.
#[doc(hidden)]
#[allow(non_camel_case_types)]
#[derive(
::scale::Encode,
::scale::Decode,
)]
#[::ink::scale_derive(Encode, Decode)]
#[repr(transparent)]
pub struct #call_builder_ident<E>
where
Expand Down Expand Up @@ -198,15 +195,15 @@ impl CallBuilder<'_> {

#[cfg(feature = "std")]
/// We require this manual implementation since the derive produces incorrect trait bounds.
impl<E> ::scale_info::TypeInfo for #call_builder_ident<E>
impl<E> ::ink::scale_info::TypeInfo for #call_builder_ident<E>
where
E: ::ink::env::Environment,
<E as ::ink::env::Environment>::AccountId: ::scale_info::TypeInfo + 'static,
<E as ::ink::env::Environment>::AccountId: ::ink::scale_info::TypeInfo + 'static,
{
type Identity = <E as ::ink::env::Environment>::AccountId;

fn type_info() -> ::scale_info::Type {
<<E as ::ink::env::Environment>::AccountId as ::scale_info::TypeInfo>::type_info()
fn type_info() -> ::ink::scale_info::Type {
<<E as ::ink::env::Environment>::AccountId as ::ink::scale_info::TypeInfo>::type_info()
}
}
)
Expand Down
15 changes: 6 additions & 9 deletions crates/ink/codegen/src/generator/trait_def/call_forwarder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,7 @@ impl CallForwarder<'_> {
/// will be handled by this type.
#[doc(hidden)]
#[allow(non_camel_case_types)]
#[derive(
::scale::Encode,
::scale::Decode,
)]
#[::ink::scale_derive(Encode, Decode)]
#[repr(transparent)]
pub struct #call_forwarder_ident<E>
where
Expand Down Expand Up @@ -193,18 +190,18 @@ impl CallForwarder<'_> {

#[cfg(feature = "std")]
/// We require this manual implementation since the derive produces incorrect trait bounds.
impl<E> ::scale_info::TypeInfo for #call_forwarder_ident<E>
impl<E> ::ink::scale_info::TypeInfo for #call_forwarder_ident<E>
where
E: ::ink::env::Environment,
<E as ::ink::env::Environment>::AccountId: ::scale_info::TypeInfo + 'static,
<E as ::ink::env::Environment>::AccountId: ::ink::scale_info::TypeInfo + 'static,
{
type Identity = <
<Self as ::ink::codegen::TraitCallBuilder>::Builder as ::scale_info::TypeInfo
<Self as ::ink::codegen::TraitCallBuilder>::Builder as ::ink::scale_info::TypeInfo
>::Identity;

fn type_info() -> ::scale_info::Type {
fn type_info() -> ::ink::scale_info::Type {
<
<Self as ::ink::codegen::TraitCallBuilder>::Builder as ::scale_info::TypeInfo
<Self as ::ink::codegen::TraitCallBuilder>::Builder as ::ink::scale_info::TypeInfo
>::type_info()
}
}
Expand Down
37 changes: 37 additions & 0 deletions crates/ink/macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod chain_extension;
mod contract;
mod event;
mod ink_test;
mod scale;
mod selector;
mod storage;
mod storage_item;
Expand Down Expand Up @@ -1549,5 +1550,41 @@ synstructure::decl_derive!(
storage::storage_layout_derive
);

/// Derive the re-exported traits `ink::scale::Encode`, `ink::scale::Decode` and
/// `ink::scale_info::TypeInfo`. It enables using the built in derive macros for these
/// traits without depending directly on the `parity-scale-codec` and `scale-info` crates.
///
/// # Options
/// - `encode`: derives `ink::scale::Encode`
/// - `decode`: derives `ink::scale::Decode`
/// - `type_info`: derives `ink::scale_info::TypeInfo`
///
/// # Examples
///
/// ```
/// #[ink::scale_derive(Encode, Decode, TypeInfo)]
/// pub enum Error {}
/// ```
/// This is a convenience macro that expands to include the additional `crate` attributes
/// required for the path of the re-exported crates.
///
/// ```
/// #[derive(::ink::scale::Encode, ::ink::scale::Decode)]
/// #[codec(crate = ::ink::scale)]
/// #[cfg_attr(
/// feature = "std",
/// derive(::scale_info::TypeInfo),
/// scale_info(crate = ::ink::scale_info)
/// )]
/// pub enum Error {}
/// ```
#[proc_macro_attribute]
pub fn scale_derive(attr: TokenStream, item: TokenStream) -> TokenStream {
match scale::derive(attr.into(), item.into()) {
Ok(output) => output.into(),
Err(err) => err.to_compile_error().into(),
}
}

#[cfg(test)]
pub use contract::generate_or_err;
Loading

0 comments on commit 5fb5477

Please sign in to comment.