ink_macro/storage/storage_key.rs
1// Copyright (C) Use Ink (UK) Ltd.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use ink_ir::utils::find_storage_key_salt;
16use proc_macro2::TokenStream as TokenStream2;
17use quote::{
18 quote,
19 ToTokens,
20};
21
22pub fn storage_key_derive(mut s: synstructure::Structure) -> TokenStream2 {
23 s.add_bounds(synstructure::AddBounds::None)
24 .underscore_const(true);
25
26 let salt = if let Some(param) = find_storage_key_salt(s.ast()) {
27 param.ident.to_token_stream()
28 } else {
29 quote! { () }
30 };
31
32 s.gen_impl(quote! {
33 gen impl ::ink::storage::traits::StorageKey for @Self {
34 const KEY: ::ink::primitives::Key = <#salt as ::ink::storage::traits::StorageKey>::KEY;
35 }
36 })
37}