ink_codegen/traits.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 proc_macro2::TokenStream as TokenStream2;
16
17/// Types implementing this trait are code generators for the ink! language.
18pub trait GenerateCode {
19 /// Generates ink! contract code.
20 fn generate_code(&self) -> TokenStream2;
21}
22
23/// Types implementing this trait can forward code generation to other generators.
24pub trait GenerateCodeUsing: AsRef<ir::Contract> {
25 /// Generates code using the given codegen module.
26 fn generate_code_using<'a, G>(&'a self) -> TokenStream2
27 where
28 G: GenerateCode + From<&'a ir::Contract>;
29}
30
31impl<T> GenerateCodeUsing for T
32where
33 T: AsRef<ir::Contract>,
34{
35 fn generate_code_using<'a, G>(&'a self) -> TokenStream2
36 where
37 G: GenerateCode + From<&'a ir::Contract>,
38 {
39 <G as GenerateCode>::generate_code(&G::from(
40 <Self as AsRef<ir::Contract>>::as_ref(self),
41 ))
42 }
43}