Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
parity
Mirrored projects
ink
Commits
26b344fb
Unverified
Commit
26b344fb
authored
May 25, 2020
by
Hero Bird
Committed by
GitHub
May 25, 2020
Browse files
[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
Changes
5
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
lang/macro/src/codegen/storage.rs
View file @
26b344fb
...
...
@@ -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
()
}
}
}
}
...
...
lang/macro/tests/compile_tests.rs
View file @
26b344fb
...
...
@@ -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"
);
...
...
lang/macro/tests/ui/pass/08-static-env.rs
0 → 100644
View file @
26b344fb
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
()
{}
lang/src/env_access.rs
View file @
26b344fb
...
...
@@ -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 ty
pe
s
.
/// The
access wrap
pe
r
.
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
...
...
lang/src/lib.rs
View file @
26b344fb
...
...
@@ -57,6 +57,7 @@ pub use self::{
env_access
::{
Env
,
EnvAccess
,
StaticEnv
,
},
error
::{
DispatchError
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment