-
Notifications
You must be signed in to change notification settings - Fork 426
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Follow-up: Implement storage2 trait derive proc. macros (#399)
* [core/derive] implement SpreadLayout and PackedLayout derives * [core/derive] no longer is no_std (not useful) Also simplified its dependencies a bit. * [core/derive] apply rustfmt * [core/derive] fix clippy warning
- Loading branch information
Showing
9 changed files
with
1,464 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2018-2020 Parity Technologies (UK) Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use proc_macro2::TokenStream as TokenStream2; | ||
use quote::quote; | ||
|
||
pub(crate) fn allocate_using_derive(mut s: synstructure::Structure) -> TokenStream2 { | ||
// We cannot implement AllocateUsing on enums because we cannot specify | ||
// which variant we are going to use. | ||
if let syn::Data::Enum(ref _enum_data) = s.ast().data { | ||
panic!("cannot derive AllocateUsing for enums") | ||
} | ||
s.bind_with(|_| synstructure::BindStyle::Move); | ||
s.add_bounds(synstructure::AddBounds::Fields); | ||
// The `synstructure` crate treats every input as `enum`. | ||
// So even `struct`s are treated as single-variant enums. | ||
// Some line above we exclude `enum` for deriving this trait so | ||
// all inputs (`struct` only) have exactly one variant which | ||
// is the `struct` itself. | ||
let body = s.variants()[0].construct(|field, _| { | ||
let ty = &field.ty; | ||
quote! { | ||
<#ty as ink_core::storage::alloc::AllocateUsing>::allocate_using(alloc) | ||
} | ||
}); | ||
s.gen_impl(quote! { | ||
gen impl ink_core::storage::alloc::AllocateUsing for @Self { | ||
unsafe fn allocate_using<A>(alloc: &mut A) -> Self | ||
where | ||
A: ink_core::storage::alloc::Allocate, | ||
{ | ||
#body | ||
} | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright 2018-2020 Parity Technologies (UK) Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use proc_macro2::TokenStream as TokenStream2; | ||
use quote::quote; | ||
|
||
pub(crate) fn flush_derive(mut s: synstructure::Structure) -> TokenStream2 { | ||
if s.variants().is_empty() { | ||
panic!("deriving Flush for empty enum is invalid") | ||
} | ||
s.bind_with(|_| synstructure::BindStyle::Move); | ||
s.add_bounds(synstructure::AddBounds::Fields); | ||
// Upon seeing the first variant we set this to true. | ||
// This is needed so that we do not have a `match self` | ||
// for empty enums which apparently causes errors. | ||
// If there is a better solution to tackle this please | ||
// update this. | ||
let mut requires_match = false; | ||
let body = s.each(|bi| { | ||
requires_match = true; | ||
quote! { | ||
ink_core::storage::Flush::flush(#bi) | ||
} | ||
}); | ||
let body = if requires_match { | ||
quote! { | ||
match self { | ||
#body | ||
} | ||
} | ||
} else { | ||
quote! {} | ||
}; | ||
s.gen_impl(quote! { | ||
gen impl ink_core::storage::Flush for @Self { | ||
fn flush(&mut self) { | ||
#body | ||
} | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2018-2020 Parity Technologies (UK) Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use proc_macro2::TokenStream as TokenStream2; | ||
use quote::quote; | ||
|
||
/// Derives `ink_core`'s `PackedLayout` trait for the given `struct` or `enum`. | ||
pub fn packed_layout_derive(mut s: synstructure::Structure) -> TokenStream2 { | ||
s.bind_with(|_| synstructure::BindStyle::Move); | ||
s.add_bounds(synstructure::AddBounds::Generics); | ||
|
||
let pull_body = s.each(|binding| { | ||
quote! { ::ink_core::storage2::traits::PackedLayout::pull_packed(#binding, __key); } | ||
}); | ||
let push_body = s.each(|binding| { | ||
quote! { ::ink_core::storage2::traits::PackedLayout::push_packed(#binding, __key); } | ||
}); | ||
let clear_body = s.each(|binding| { | ||
quote! { ::ink_core::storage2::traits::PackedLayout::clear_packed(#binding, __key); } | ||
}); | ||
|
||
s.gen_impl(quote! { | ||
gen impl ::ink_core::storage2::traits::PackedLayout for @Self { | ||
fn pull_packed(&mut self, __key: &::ink_primitives::Key) { | ||
match self { #pull_body } | ||
} | ||
fn push_packed(&self, __key: &::ink_primitives::Key) { | ||
match self { #push_body } | ||
} | ||
fn clear_packed(&self, __key: &::ink_primitives::Key) { | ||
match self { #clear_body } | ||
} | ||
} | ||
}) | ||
} |
Oops, something went wrong.