Skip to content

Commit fb871a0

Browse files
authored
Combine storage traits and derives into single crate (#1389)
* Add ink_storage_traits crate * Remove unused hash layout trait and impls * Remove dependency between storage traits and ink_env * Move Storable definition and derive to ink_storage_traits * Replace usages of primitives storable with new location * Fmt * README and license sym links * Fix up CI script crates and codegen symlinks * update CI crates again * Add StorageLayout impl for Hash * Fix up collections ui tests * Fix up storage traits tests * Add missing feature to codegen syn dependency * Fix storage traits reference in env * Add missing no_std cfg_attr * Fix example build * Fmt * Fix example * Move Clear trait to primitives * Fix example * Fix env access * Fix up docs * Trying to fix UI tests * Fix LICENSE and README paths
1 parent c0956e4 commit fb871a0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+415
-537
lines changed

.gitlab-ci.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ variables:
2929
# CI_IMAGE is changed to "-:staging" when the CI image gets rebuilt
3030
# read more https://github.com/paritytech/scripts/pull/244
3131
CI_IMAGE: "paritytech/ink-ci-linux:production"
32-
PURELY_STD_CRATES: "lang/codegen metadata engine"
33-
ALSO_WASM_CRATES: "env storage storage/derive allocator prelude primitives lang lang/macro lang/ir"
32+
PURELY_STD_CRATES: "lang/codegen storage/traits/codegen metadata engine"
33+
ALSO_WASM_CRATES: "env storage storage/traits storage/traits/derive allocator prelude primitives lang lang/macro lang/ir"
3434
ALL_CRATES: "${PURELY_STD_CRATES} ${ALSO_WASM_CRATES}"
3535
DELEGATOR_SUBCONTRACTS: "accumulator adder subber"
3636
UPGRADEABLE_CONTRACTS: "forward-calls set-code-hash"
@@ -286,9 +286,9 @@ docs:
286286
- ./crate-docs/
287287
script:
288288
- cargo doc --no-deps --all-features
289-
-p scale-info -p ink_metadata
290-
-p ink_env -p ink_storage -p ink_storage_derive
291-
-p ink_primitives -p ink_prelude -p ink_primitives_derive
289+
-p scale-info -p ink_metadata -p ink_env
290+
-p ink_storage -p ink_storage_traits -p ink_storage_codegen -p ink_storage_derive
291+
-p ink_primitives -p ink_prelude
292292
-p ink_lang -p ink_lang_macro -p ink_lang_ir -p ink_lang_codegen
293293
- mv ${CARGO_TARGET_DIR}/doc ./crate-docs
294294
# FIXME: remove me after CI image gets nonroot

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ members = [
1313
"crates/engine",
1414
"crates/env",
1515
"crates/storage",
16-
"crates/storage/derive",
16+
"crates/storage/traits",
17+
"crates/storage/traits/derive",
1718
]
1819
exclude = [
1920
"examples/",

crates/engine/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ categories = ["no-std", "embedded"]
1515
include = ["Cargo.toml", "src/**/*.rs", "README.md", "LICENSE"]
1616

1717
[dependencies]
18+
ink_primitives = { path = "../../crates/primitives", default-features = false }
1819
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive", "full"] }
1920
derive_more = { version = "0.99", default-features = false, features = ["from", "display"] }
2021

crates/env/Cargo.toml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ categories = ["no-std", "embedded"]
1515
include = ["Cargo.toml", "src/**/*.rs", "README.md", "LICENSE"]
1616

1717
[dependencies]
18-
ink_metadata = { version = "4.0.0-alpha.1", path = "../metadata/", default-features = false, features = ["derive"], optional = true }
19-
ink_allocator = { version = "4.0.0-alpha.1", path = "../allocator/", default-features = false }
20-
ink_primitives = { version = "4.0.0-alpha.1", path = "../primitives/", default-features = false }
21-
ink_prelude = { version = "4.0.0-alpha.1", path = "../prelude/", default-features = false }
18+
ink_metadata = { version = "4.0.0-alpha.1", path = "../metadata", default-features = false, features = ["derive"], optional = true }
19+
ink_allocator = { version = "4.0.0-alpha.1", path = "../allocator", default-features = false }
20+
ink_storage_traits = { version = "4.0.0-alpha.1", path = "../storage/traits", default-features = false }
21+
ink_prelude = { version = "4.0.0-alpha.1", path = "../prelude", default-features = false }
22+
ink_primitives = { version = "4.0.0-alpha.1", path = "../primitives", default-features = false }
2223

2324
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive", "full"] }
2425
derive_more = { version = "0.99", default-features = false, features = ["from", "display"] }
@@ -56,6 +57,7 @@ std = [
5657
"ink_allocator/std",
5758
"ink_prelude/std",
5859
"ink_primitives/std",
60+
"ink_storage_traits/std",
5961
"ink_engine/std",
6062
"scale/std",
6163
"scale-info/std",

crates/env/src/api.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use crate::{
3939
Environment,
4040
Result,
4141
};
42-
use ink_primitives::traits::Storable;
42+
use ink_storage_traits::Storable;
4343

4444
/// Returns the address of the caller of the executed contract.
4545
///

crates/env/src/backend.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::{
2727
Environment,
2828
Result,
2929
};
30-
use ink_primitives::traits::Storable;
30+
use ink_storage_traits::Storable;
3131

3232
/// The flags to indicate further information about the end of a contract execution.
3333
#[derive(Default)]

crates/env/src/call/call_builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ use crate::{
2424
ExecutionInput,
2525
},
2626
types::Gas,
27-
Clear,
2827
Environment,
2928
Error,
3029
};
3130
use core::marker::PhantomData;
31+
use ink_primitives::Clear;
3232
use num_traits::Zero;
3333

3434
/// The final parameters to the cross-contract call.
@@ -222,9 +222,9 @@ where
222222
/// # use ::ink_env::{
223223
/// # Environment,
224224
/// # DefaultEnvironment,
225-
/// # Clear,
226225
/// # call::{build_call, Selector, ExecutionInput, utils::ReturnType, DelegateCall},
227226
/// # };
227+
/// # use ink_primitives::Clear;
228228
/// # type AccountId = <DefaultEnvironment as Environment>::AccountId;
229229
/// let my_return_value: i32 = build_call::<DefaultEnvironment>()
230230
/// .call_type(DelegateCall::new()

crates/env/src/engine/off_chain/impls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use ink_engine::{
4444
ext,
4545
ext::Engine,
4646
};
47-
use ink_primitives::traits::Storable;
47+
use ink_storage_traits::Storable;
4848

4949
/// The capacity of the static buffer.
5050
/// This is the same size as the ink! on-chain environment. We chose to use the same size

crates/env/src/engine/on_chain/buffer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,12 @@ impl<'a> ScopedBuffer<'a> {
175175
#[inline(always)]
176176
pub fn take_storable_encoded<T>(&mut self, value: &T) -> &'a mut [u8]
177177
where
178-
T: ink_primitives::traits::Storable,
178+
T: ink_storage_traits::Storable,
179179
{
180180
debug_assert_eq!(self.offset, 0);
181181
let buffer = core::mem::take(&mut self.buffer);
182182
let mut encode_scope = EncodeScope::from(buffer);
183-
ink_primitives::traits::Storable::encode(value, &mut encode_scope);
183+
ink_storage_traits::Storable::encode(value, &mut encode_scope);
184184
let encode_len = encode_scope.len();
185185
let _ = core::mem::replace(&mut self.buffer, encode_scope.into_buffer());
186186
self.take(encode_len)

crates/env/src/engine/on_chain/impls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use crate::{
4646
ReturnFlags,
4747
TypedEnvBackend,
4848
};
49-
use ink_primitives::traits::Storable;
49+
use ink_storage_traits::Storable;
5050

5151
impl CryptoHash for Blake2x128 {
5252
fn hash(input: &[u8], output: &mut <Self as HashOutput>::Type) {

crates/env/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,13 @@ pub use self::{
100100
},
101101
topics::Topics,
102102
types::{
103-
AccountId,
104-
Clear,
105103
DefaultEnvironment,
106104
Environment,
107105
FromLittleEndian,
108-
Hash,
109106
NoChainExtension,
110107
},
111108
};
109+
use ink_primitives::Clear;
112110

113111
cfg_if::cfg_if! {
114112
if #[cfg(any(feature = "ink-debug", feature = "std"))] {

crates/env/src/types.rs

Lines changed: 4 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,10 @@
3232
//! the trait bounds on the `Environment` trait types.
3333
3434
use super::arithmetic::AtLeast32BitUnsigned;
35-
use core::array::TryFromSliceError;
36-
use derive_more::From;
37-
use scale::{
38-
Decode,
39-
Encode,
35+
use ink_primitives::{
36+
AccountId,
37+
Clear,
38+
Hash,
4039
};
4140
#[cfg(feature = "std")]
4241
use scale_info::TypeInfo;
@@ -194,138 +193,3 @@ pub type Gas = u64;
194193

195194
/// The default block number type.
196195
pub type BlockNumber = u32;
197-
198-
/// The default environment `AccountId` type.
199-
///
200-
/// # Note
201-
///
202-
/// This is a mirror of the `AccountId` type used in the default configuration
203-
/// of PALLET contracts.
204-
#[derive(
205-
Debug,
206-
Copy,
207-
Clone,
208-
PartialEq,
209-
Eq,
210-
Ord,
211-
PartialOrd,
212-
Hash,
213-
Encode,
214-
Decode,
215-
From,
216-
Default,
217-
)]
218-
#[cfg_attr(feature = "std", derive(TypeInfo))]
219-
pub struct AccountId([u8; 32]);
220-
221-
impl AsRef<[u8; 32]> for AccountId {
222-
#[inline]
223-
fn as_ref(&self) -> &[u8; 32] {
224-
&self.0
225-
}
226-
}
227-
228-
impl AsMut<[u8; 32]> for AccountId {
229-
#[inline]
230-
fn as_mut(&mut self) -> &mut [u8; 32] {
231-
&mut self.0
232-
}
233-
}
234-
235-
impl AsRef<[u8]> for AccountId {
236-
#[inline]
237-
fn as_ref(&self) -> &[u8] {
238-
&self.0[..]
239-
}
240-
}
241-
242-
impl AsMut<[u8]> for AccountId {
243-
#[inline]
244-
fn as_mut(&mut self) -> &mut [u8] {
245-
&mut self.0[..]
246-
}
247-
}
248-
249-
impl<'a> TryFrom<&'a [u8]> for AccountId {
250-
type Error = TryFromSliceError;
251-
252-
fn try_from(bytes: &'a [u8]) -> Result<Self, TryFromSliceError> {
253-
let address = <[u8; 32]>::try_from(bytes)?;
254-
Ok(Self(address))
255-
}
256-
}
257-
258-
/// The default environment `Hash` type.
259-
///
260-
/// # Note
261-
///
262-
/// This is a mirror of the `Hash` type used in the default configuration
263-
/// of PALLET contracts.
264-
#[derive(
265-
Debug,
266-
Copy,
267-
Clone,
268-
PartialEq,
269-
Eq,
270-
Ord,
271-
PartialOrd,
272-
Hash,
273-
Encode,
274-
Decode,
275-
From,
276-
Default,
277-
)]
278-
#[cfg_attr(feature = "std", derive(TypeInfo))]
279-
pub struct Hash([u8; 32]);
280-
281-
impl<'a> TryFrom<&'a [u8]> for Hash {
282-
type Error = TryFromSliceError;
283-
284-
fn try_from(bytes: &'a [u8]) -> Result<Self, TryFromSliceError> {
285-
let address = <[u8; 32]>::try_from(bytes)?;
286-
Ok(Self(address))
287-
}
288-
}
289-
290-
impl AsRef<[u8]> for Hash {
291-
fn as_ref(&self) -> &[u8] {
292-
&self.0[..]
293-
}
294-
}
295-
296-
impl AsMut<[u8]> for Hash {
297-
fn as_mut(&mut self) -> &mut [u8] {
298-
&mut self.0[..]
299-
}
300-
}
301-
302-
/// The equivalent of `Zero` for hashes.
303-
///
304-
/// A hash that consists only of 0 bits is clear.
305-
pub trait Clear {
306-
/// Returns `true` if the hash is clear.
307-
fn is_clear(&self) -> bool;
308-
309-
/// Returns a clear hash.
310-
fn clear() -> Self;
311-
}
312-
313-
impl Clear for [u8; 32] {
314-
fn is_clear(&self) -> bool {
315-
self.as_ref().iter().all(|&byte| byte == 0x00)
316-
}
317-
318-
fn clear() -> Self {
319-
[0x00; 32]
320-
}
321-
}
322-
323-
impl Clear for Hash {
324-
fn is_clear(&self) -> bool {
325-
<[u8; 32] as Clear>::is_clear(&self.0)
326-
}
327-
328-
fn clear() -> Self {
329-
Self(<[u8; 32] as Clear>::clear())
330-
}
331-
}

crates/lang/codegen/src/generator/storage_item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl GenerateCode for StorageItem<'_> {
6363
#[derive(
6464
::ink_storage::traits::StorableHint,
6565
::ink_storage::traits::StorageKey,
66-
::ink_primitives::traits::Storable,
66+
::ink_storage::traits::Storable,
6767
)]
6868
};
6969
}

crates/lang/ir/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ include = ["Cargo.toml", "src/**/*.rs", "README.md", "LICENSE"]
1818
name = "ink_lang_ir"
1919

2020
[dependencies]
21-
ink_storage_codegen = { version = "4.0.0-alpha.1", path = "../../storage/codegen" }
21+
ink_storage_codegen = { version = "4.0.0-alpha.1", path = "../../storage/traits/codegen" }
2222
quote = "1"
2323
syn = { version = "1.0", features = ["parsing", "full", "visit", "extra-traits"] }
2424
proc-macro2 = "1.0"

crates/lang/macro/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ pub fn trait_definition(attr: TokenStream, item: TokenStream) -> TokenStream {
704704
/// StorageKey,
705705
/// StorableHint,
706706
/// };
707-
/// use ink_primitives::traits::Storable;
707+
/// use ink_storage::traits::Storable;
708708
///
709709
/// // Deriving `scale::Decode` and `scale::Encode` also derives blanket implementation of all
710710
/// // required traits to be storable.
@@ -796,7 +796,7 @@ pub fn trait_definition(attr: TokenStream, item: TokenStream) -> TokenStream {
796796
/// StorableHint,
797797
/// StorageKey,
798798
/// };
799-
/// use ink_primitives::traits::Storable;
799+
/// use ink_storage::traits::Storable;
800800
///
801801
/// #[ink_lang::storage_item(derive = false)]
802802
/// #[derive(StorableHint, Storable, StorageKey)]

crates/lang/src/codegen/dispatch/execution.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ use ink_env::{
2424
Environment,
2525
ReturnFlags,
2626
};
27-
use ink_primitives::traits::Storable;
28-
use ink_storage::traits::StorageKey;
27+
use ink_storage::traits::{
28+
Storable,
29+
StorageKey,
30+
};
2931
use scale::Encode;
3032

3133
/// Returns `Ok` if the caller did not transfer additional value to the callee.

crates/lang/src/env_access.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,9 +565,9 @@ where
565565
/// # pub mod my_contract {
566566
/// use ink_env::{
567567
/// DefaultEnvironment,
568-
/// Clear,
569568
/// call::{build_call, DelegateCall, Selector, ExecutionInput, utils::ReturnType}
570569
/// };
570+
/// use ink_primitives::Clear;
571571
///
572572
/// #
573573
/// # #[ink(storage)]

0 commit comments

Comments
 (0)