Unverified Commit 26b344fb authored by Hero Bird's avatar Hero Bird Committed by GitHub

[core] add Self::env() syntax to ink! lang to access environment (#419)

parent b0681a04
Pipeline #93826 passed with stages
in 8 minutes and 27 seconds
...@@ -79,7 +79,10 @@ impl GenerateCode for Storage<'_> { ...@@ -79,7 +79,10 @@ impl GenerateCode for Storage<'_> {
const _: () = { const _: () = {
// Used to make `self.env()` available in message code. // Used to make `self.env()` available in message code.
#[allow(unused_imports)] #[allow(unused_imports)]
use ink_lang::Env as _; use ::ink_lang::{
Env as _,
StaticEnv as _,
};
#use_emit_event #use_emit_event
#message_impls #message_impls
...@@ -98,6 +101,14 @@ impl Storage<'_> { ...@@ -98,6 +101,14 @@ impl Storage<'_> {
Default::default() Default::default()
} }
} }
impl<'a> ink_lang::StaticEnv for Storage {
type EnvAccess = ink_lang::EnvAccess<'static, EnvTypes>;
fn env() -> Self::EnvAccess {
Default::default()
}
}
} }
} }
......
...@@ -22,6 +22,7 @@ fn compile_tests() { ...@@ -22,6 +22,7 @@ fn compile_tests() {
t.pass("tests/ui/pass/05-erc721-contract.rs"); t.pass("tests/ui/pass/05-erc721-contract.rs");
t.pass("tests/ui/pass/06-non-ink-items.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/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/01-constructor-returns.rs");
t.compile_fail("tests/ui/fail/02-missing-constructor.rs"); t.compile_fail("tests/ui/fail/02-missing-constructor.rs");
t.compile_fail("tests/ui/fail/03-invalid-version.rs"); t.compile_fail("tests/ui/fail/03-invalid-version.rs");
......
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() {}
...@@ -27,7 +27,7 @@ use ink_core::{ ...@@ -27,7 +27,7 @@ use ink_core::{
}; };
use ink_primitives::Key; use ink_primitives::Key;
/// Allows to directly access the environment mutably. /// Simplifies interaction with the host environment via `self`.
/// ///
/// # Note /// # Note
/// ///
...@@ -35,13 +35,28 @@ use ink_primitives::Key; ...@@ -35,13 +35,28 @@ use ink_primitives::Key;
/// their environment in order to allow the different dispatch functions /// their environment in order to allow the different dispatch functions
/// to use it for returning the contract's output. /// to use it for returning the contract's output.
pub trait Env { pub trait Env {
/// The environmental types. /// The access wrapper.
type EnvAccess; type EnvAccess;
/// Accesses the environment with predefined environmental types. /// Accesses the environment with predefined environmental types.
fn env(self) -> Self::EnvAccess; 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. /// A typed accessor to the environment.
/// ///
/// This allows ink! messages to make use of the environment efficiently /// This allows ink! messages to make use of the environment efficiently
......
...@@ -57,6 +57,7 @@ pub use self::{ ...@@ -57,6 +57,7 @@ pub use self::{
env_access::{ env_access::{
Env, Env,
EnvAccess, EnvAccess,
StaticEnv,
}, },
error::{ error::{
DispatchError, DispatchError,
......
Markdown is supported
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