Unverified Commit 1601b967 authored by Hero Bird's avatar Hero Bird Committed by GitHub
Browse files

Use Flush derives and generally update the Flush trait (#269)

* [core] add core/derive crate to add derive macro for Flush

* [core/derive] change license from GPL-3.0 to APACHE-2.0

* [core/derive] implement Flush and AllocateUsing derives based on synstructure

* [core] re-export AllocateUsing and Flush derives from within core

* [core/derive] fix AllocateUsing derive impl

* [core/derive] adjust AllocateUsing tests

* [core/derive] apply cargo fmt

* [core/derive] add test::utils and improve AllocateUsing tests

* [core/derive] add explanation docs

* [core/derive] add doc for a hack

* [core/derive] forbid deriving empty enums for Flush

* [core/derive] update compile tests

* [core] remove derive crate feature

* [core/derive] remove `extern crate ink_core` edition 2015 compat

* [core, examples, lang] update Flush impls

* [core] apply rustfmt

* [core] simplify Flush impl for StashHeader

* [core] remove derive crate feature

* [examples] fix clippy warning about Flush in delegator
parent d3b091d4
Pipeline #70473 passed with stages
in 15 minutes and 36 seconds
......@@ -18,7 +18,7 @@ include = ["Cargo.toml", "src/**/*.rs", "README.md", "LICENSE"]
ink_abi = { path = "../abi/", default-features = false, features = ["derive"], optional = true }
ink_alloc = { path = "../alloc/", default-features = false }
ink_utils = { path = "../utils/" }
ink_core_derive = { version = "0.1.0", path = "derive", default-features = false, optional = true }
ink_core_derive = { version = "0.1.0", path = "derive", default-features = false }
scale = { package = "parity-scale-codec", version = "1.1", default-features = false, features = ["derive", "full"] }
type-metadata = { git = "https://github.com/type-metadata/type-metadata.git", default-features = false, features = ["derive"], optional = true }
......@@ -28,9 +28,6 @@ cfg-if = "0.1"
[features]
default = ["test-env"]
derive = [
"ink_core_derive",
]
test-env = [
"std",
]
......
......@@ -51,8 +51,6 @@ pub(crate) fn flush_derive(mut s: synstructure::Structure) -> TokenStream2 {
quote! {}
};
s.gen_impl(quote! {
extern crate ink_core;
gen impl ink_core::storage::Flush for @Self {
fn flush(&mut self) {
#body
......@@ -81,8 +79,6 @@ pub(crate) fn allocate_using_derive(mut s: synstructure::Structure) -> TokenStre
}
});
s.gen_impl(quote! {
extern crate ink_core;
gen impl ink_core::storage::alloc::AllocateUsing for @Self {
unsafe fn allocate_using<A>(alloc: &mut A) -> Self
where
......
......@@ -12,21 +12,20 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::{
env::EnvTypes,
ink_core,
storage::Flush,
};
use core::{
array::TryFromSliceError,
convert::TryFrom,
};
use scale::{
Decode,
Encode,
};
use crate::{
env::EnvTypes,
storage::Flush,
};
#[cfg(feature = "ink-generate-abi")]
use type_metadata::Metadata;
......@@ -66,7 +65,7 @@ impl EnvTypes for DefaultSrmlTypes {
}
/// The default SRML address type.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Encode, Decode)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Encode, Decode, Flush)]
#[cfg_attr(feature = "ink-generate-abi", derive(Metadata))]
pub struct AccountId([u8; 32]);
......@@ -89,7 +88,7 @@ impl<'a> TryFrom<&'a [u8]> for AccountId {
pub type Balance = u128;
/// The default SRML hash type.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Encode, Decode)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Encode, Decode, Flush)]
#[cfg_attr(feature = "ink-generate-abi", derive(Metadata))]
pub struct Hash([u8; 32]);
......@@ -119,11 +118,3 @@ pub type Moment = u64;
/// The default SRML blocknumber type.
pub type BlockNumber = u64;
impl Flush for AccountId {
fn flush(&mut self) {}
}
impl Flush for Hash {
fn flush(&mut self) {}
}
......@@ -122,10 +122,7 @@ impl<E> AllocateUsing for EnvAccess<E> {
}
}
impl<E> Flush for EnvAccess<E> {
#[inline(always)]
fn flush(&mut self) {}
}
impl<E> Flush for EnvAccess<E> {}
impl<E> Initialize for EnvAccess<E> {
type Args = ();
......
......@@ -74,10 +74,7 @@ impl<E> AllocateUsing for EnvAccessMut<E> {
}
}
impl<E> Flush for EnvAccessMut<E> {
#[inline]
fn flush(&mut self) {}
}
impl<E> Flush for EnvAccessMut<E> {}
impl<E> Initialize for EnvAccessMut<E> {
type Args = ();
......
......@@ -119,10 +119,7 @@ impl<'a> TryFrom<&'a [u8]> for AccountId {
}
}
impl Flush for AccountId {
#[inline]
fn flush(&mut self) {}
}
impl Flush for AccountId {}
/// The default SRML `Hash` type.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Encode, Decode, From, Default)]
......@@ -138,7 +135,4 @@ impl<'a> TryFrom<&'a [u8]> for Hash {
}
}
impl Flush for Hash {
#[inline]
fn flush(&mut self) {}
}
impl Flush for Hash {}
......@@ -58,3 +58,6 @@ pub mod env;
pub mod env2;
pub mod memory;
pub mod storage;
// Needed for derive macros of `core/derive` sub crate.
pub(crate) use crate as ink_core;
......@@ -32,5 +32,4 @@ pub use self::{
},
};
#[cfg(feature = "derive")]
pub use ink_core_derive::AllocateUsing;
......@@ -30,9 +30,7 @@ pub struct BitBlock {
packs: [BitPack; Self::PACKS as usize],
}
impl Flush for BitBlock {
fn flush(&mut self) {}
}
impl Flush for BitBlock {}
/// Error indicating an invalid bit pack index.
#[derive(Debug, Copy, Clone)]
......
......@@ -26,16 +26,19 @@ use scale::{
#[cfg(feature = "ink-generate-abi")]
use type_metadata::Metadata;
use crate::storage::{
self,
alloc::{
Allocate,
AllocateUsing,
Initialize,
use crate::{
ink_core,
storage::{
self,
alloc::{
Allocate,
AllocateUsing,
Initialize,
},
chunk::SyncChunk,
Flush,
Key,
},
chunk::SyncChunk,
Flush,
Key,
};
/// A stash collection.
......@@ -91,14 +94,8 @@ struct StashHeader {
max_len: u32,
}
impl Flush for StashHeader {
#[inline]
fn flush(&mut self) {
self.next_vacant.flush();
self.len.flush();
self.max_len.flush();
}
}
/// No need to forward flush to fields.
impl ink_core::storage::Flush for StashHeader {}
/// Iterator over the values of a stash.
#[derive(Debug)]
......
......@@ -37,20 +37,18 @@ pub trait Flush {
/// Needs to take `self` by `&mut` since `SyncChunk` and `SyncCell`
/// and potentially other abstraction facilities are required to
/// write back their cached values which is a mutable operation.
fn flush(&mut self);
#[inline(always)]
fn flush(&mut self) {}
}
#[cfg(feature = "derive")]
pub use ink_core_derive::Flush;
macro_rules! impl_empty_flush_for {
( $($ty:ty),* ) => {
$(
impl Flush for $ty {
fn flush(&mut self) {}
}
)*
};
( $($ty:ty),* ) => {
$(
impl Flush for $ty {}
)*
};
}
impl_empty_flush_for! {
......@@ -60,20 +58,20 @@ impl_empty_flush_for! {
}
macro_rules! impl_tuple_flush_for {
( $(($n:tt, $name:ident)),* ) => {
impl< $($name),* > Flush for ($($name,)*)
where
$(
$name: Flush,
)*
{
fn flush(&mut self) {
$(
self.$n.flush();
)*
}
}
}
( $(($n:tt, $name:ident)),* ) => {
impl< $($name),* > Flush for ($($name,)*)
where
$(
$name: Flush,
)*
{
fn flush(&mut self) {
$(
self.$n.flush();
)*
}
}
}
}
impl_tuple_flush_for!();
......@@ -167,11 +165,7 @@ where
}
}
impl Flush for crate::memory::string::String {
fn flush(&mut self) {
// Note: Strings contain only characters that need no flushing.
}
}
impl Flush for crate::memory::string::String {}
impl<K, V> Flush for crate::memory::collections::btree_map::BTreeMap<K, V>
where
......@@ -185,11 +179,7 @@ where
}
}
impl<T> Flush for crate::memory::collections::btree_set::BTreeSet<T> {
fn flush(&mut self) {
// Note: Values within a `BTreeSet` are immutable and thus need not be flushed.
}
}
impl<T> Flush for crate::memory::collections::btree_set::BTreeSet<T> {}
impl<T> Flush for crate::memory::collections::linked_list::LinkedList<T>
where
......@@ -213,11 +203,4 @@ where
}
}
impl<T> Flush for crate::memory::collections::binary_heap::BinaryHeap<T>
where
T: Flush,
{
fn flush(&mut self) {
// Note: Values within a `BinaryHeap` are immutable and thus need not be flushed.
}
}
impl<T> Flush for crate::memory::collections::binary_heap::BinaryHeap<T> {}
......@@ -27,9 +27,7 @@ pub enum Which {
Subber,
}
impl ink_core::storage::Flush for Which {
fn flush(&mut self) {}
}
impl ink_core::storage::Flush for Which {}
contract! {
#![env = ink_core::env::DefaultSrmlTypes]
......
......@@ -29,9 +29,7 @@ pub struct Access {
pub end: Option<u32>,
}
impl Flush for Access {
fn flush(&mut self) {}
}
impl Flush for Access {}
impl Access {
/// Creates unlimited access rights.
......
......@@ -15,7 +15,10 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![feature(proc_macro_hygiene)]
use ink_core::storage;
use ink_core::storage::{
self,
Flush,
};
use ink_lang2 as ink;
use accumulator::Accumulator;
......@@ -30,17 +33,13 @@ mod delegator {
/// and in `Subber` state will delegate to the `Subber` contract.
///
/// The initial state is `Adder`.
#[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode, Flush)]
#[cfg_attr(feature = "ink-generate-abi", derive(type_metadata::Metadata))]
pub enum Which {
Adder,
Subber,
}
impl ink_core::storage::Flush for Which {
fn flush(&mut self) {}
}
/// Delegates calls to an adder or subber contract to mutate
/// a value in an accumulator contract.
///
......
......@@ -88,9 +88,7 @@ fn generate_state_as_dependency(contract: &hir::Contract) -> TokenStream2 {
account_id: AccountId,
}
impl ink_core::storage::Flush for #name {
fn flush(&mut self) {}
}
impl ink_core::storage::Flush for #name {}
/// Allows to enhance calls to `&self` contract messages.
pub struct CallEnhancer<'a> {
......
......@@ -402,9 +402,7 @@ fn contract_compiles() {
account_id: AccountId,
}
impl ink_core::storage::Flush for CallCounter {
fn flush(&mut self) {}
}
impl ink_core::storage::Flush for CallCounter {}
/// Allows to enhance calls to `&self` contract messages.
pub struct CallEnhancer<'a> {
......
......@@ -264,9 +264,7 @@ fn contract_compiles() {
account_id: AccountId,
}
impl ink_core::storage::Flush for Flipper {
fn flush(&mut self) {}
}
impl ink_core::storage::Flush for Flipper {}
/// Allows to enhance calls to `&self` contract messages.
pub struct CallEnhancer<'a> {
......
......@@ -333,9 +333,7 @@ fn contract_compiles() {
account_id: AccountId,
}
impl ink_core::storage::Flush for Incrementer {
fn flush(&mut self) {}
}
impl ink_core::storage::Flush for Incrementer {}
/// Allows to enhance calls to `&self` contract messages.
pub struct CallEnhancer<'a> {
......
......@@ -205,9 +205,7 @@ fn contract_compiles() {
account_id: AccountId,
}
impl ink_core::storage::Flush for Noop {
fn flush(&mut self) {}
}
impl ink_core::storage::Flush for Noop {}
/// Allows to enhance calls to `&self` contract messages.
pub struct CallEnhancer<'a> {
......
......@@ -156,12 +156,7 @@ impl CrossCalling<'_> {
fn generate_storage_impls(&self) -> TokenStream2 {
quote! {
impl ink_core::storage::Flush for StorageAsDependency {
#[inline(always)]
fn flush(&mut self) {
// Nothing to do here!
}
}
impl ink_core::storage::Flush for StorageAsDependency {}
#[cfg(feature = "ink-generate-abi")]
impl ink_core::storage::alloc::AllocateUsing for StorageAsDependency {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment