diff --git a/lang/macro/src/codegen/storage.rs b/lang/macro/src/codegen/storage.rs index 49be2707bbad829aa0415c301b9df122e550bfb0..05ebf8d73df16e9eba8ac686640a18926603b151 100644 --- a/lang/macro/src/codegen/storage.rs +++ b/lang/macro/src/codegen/storage.rs @@ -79,7 +79,10 @@ impl GenerateCode for Storage<'_> { const _: () = { // Used to make `self.env()` available in message code. #[allow(unused_imports)] - use ink_lang::Env as _; + use ::ink_lang::{ + Env as _, + StaticEnv as _, + }; #use_emit_event #message_impls @@ -98,6 +101,14 @@ impl Storage<'_> { Default::default() } } + + impl<'a> ink_lang::StaticEnv for Storage { + type EnvAccess = ink_lang::EnvAccess<'static, EnvTypes>; + + fn env() -> Self::EnvAccess { + Default::default() + } + } } } diff --git a/lang/macro/tests/compile_tests.rs b/lang/macro/tests/compile_tests.rs index 4d2349ae723432aef735ce2032e8884fced05d6c..228b0ab762c5a0452c60d65190fb90c58a25ec52 100644 --- a/lang/macro/tests/compile_tests.rs +++ b/lang/macro/tests/compile_tests.rs @@ -22,6 +22,7 @@ fn compile_tests() { t.pass("tests/ui/pass/05-erc721-contract.rs"); t.pass("tests/ui/pass/06-non-ink-items.rs"); t.pass("tests/ui/pass/07-flipper-as-dependency.rs"); + t.pass("tests/ui/pass/08-static-env.rs"); t.compile_fail("tests/ui/fail/01-constructor-returns.rs"); t.compile_fail("tests/ui/fail/02-missing-constructor.rs"); t.compile_fail("tests/ui/fail/03-invalid-version.rs"); diff --git a/lang/macro/tests/ui/pass/08-static-env.rs b/lang/macro/tests/ui/pass/08-static-env.rs new file mode 100644 index 0000000000000000000000000000000000000000..cb96d5e5e75903e3d8cb0cb30178219e0ac6f4cd --- /dev/null +++ b/lang/macro/tests/ui/pass/08-static-env.rs @@ -0,0 +1,20 @@ +use ink_lang as ink; + +#[ink::contract(version = "0.1.0")] +mod static_env { + #[ink(storage)] + struct StaticEnv {} + + impl StaticEnv { + #[ink(constructor)] + fn new(&mut self) { + } + + #[ink(message)] + fn gas_left(&mut self) -> Balance { + Self::env().gas_left() + } + } +} + +fn main() {} diff --git a/lang/src/env_access.rs b/lang/src/env_access.rs index 4c0262ba6cf3e7f89d7ef7306b307c96d97f4c74..86576091dcd8723889b8a0063dc7deb9597c161c 100644 --- a/lang/src/env_access.rs +++ b/lang/src/env_access.rs @@ -27,7 +27,7 @@ use ink_core::{ }; use ink_primitives::Key; -/// Allows to directly access the environment mutably. +/// Simplifies interaction with the host environment via `self`. /// /// # Note /// @@ -35,13 +35,28 @@ use ink_primitives::Key; /// their environment in order to allow the different dispatch functions /// to use it for returning the contract's output. pub trait Env { - /// The environmental types. + /// The access wrapper. type EnvAccess; /// Accesses the environment with predefined environmental types. fn env(self) -> Self::EnvAccess; } +/// Simplifies interaction with the host environment via `Self`. +/// +/// # Note +/// +/// This is generally implemented for storage structs that include +/// their environment in order to allow the different dispatch functions +/// to use it for returning the contract's output. +pub trait StaticEnv { + /// The access wrapper. + type EnvAccess; + + /// Accesses the environment with predefined environmental types. + fn env() -> Self::EnvAccess; +} + /// A typed accessor to the environment. /// /// This allows ink! messages to make use of the environment efficiently diff --git a/lang/src/lib.rs b/lang/src/lib.rs index 806da6df3e87a4cb815f42cc79148cefcd779f5c..e1f86a3754287b64ab0e1bdcc4191e8ed11b64a0 100644 --- a/lang/src/lib.rs +++ b/lang/src/lib.rs @@ -57,6 +57,7 @@ pub use self::{ env_access::{ Env, EnvAccess, + StaticEnv, }, error::{ DispatchError,