Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
polkadot-sdk
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
parity
Mirrored projects
polkadot-sdk
Commits
5f861603
Commit
5f861603
authored
7 years ago
by
Gav
Browse files
Options
Downloads
Patches
Plain Diff
Add environmental module
parent
adc2e52a
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
substrate/environmental/Cargo.toml
+4
-0
4 additions, 0 deletions
substrate/environmental/Cargo.toml
substrate/environmental/src/lib.rs
+107
-0
107 additions, 0 deletions
substrate/environmental/src/lib.rs
with
111 additions
and
0 deletions
substrate/environmental/Cargo.toml
0 → 100644
+
4
−
0
View file @
5f861603
[package]
name
=
"environmental"
version
=
"0.1.0"
authors
=
[
"Parity Technologies <admin@parity.io>"
]
This diff is collapsed.
Click to expand it.
substrate/environmental/src/lib.rs
0 → 100644
+
107
−
0
View file @
5f861603
// Copyright 2017 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
use
std
::
cell
::
RefCell
;
use
std
::
thread
::
LocalKey
;
pub
fn
using_environment
<
'a
,
T
:
'a
,
S
,
F
:
FnOnce
()
>
(
global
:
&
'static
LocalKey
<
RefCell
<*
mut
S
>>
,
protected
:
&
'a
mut
T
,
f
:
F
)
{
global
.with
(|
r
|
*
r
.borrow_mut
()
=
protected
as
*
mut
T
as
*
mut
S
);
f
();
global
.with
(|
r
|
*
r
.borrow_mut
()
=
0
as
*
mut
S
);
}
pub
fn
with_environment
<
'r
,
S
,
T
:
'r
,
F
:
FnOnce
(
&
'r
mut
T
)
>
(
global
:
&
'static
LocalKey
<
RefCell
<*
mut
S
>>
,
mutator
:
F
,
)
{
global
.with
(|
r
|
{
let
br
=
r
.borrow_mut
();
if
*
br
!=
0
as
*
mut
S
{
// safe because it's only non-zero when it in with_environment, which
// is holding on to the underlying reference safely
unsafe
{
mutator
(
&
mut
*
(
*
br
as
*
mut
S
as
*
mut
T
));
}
}
});
}
#[macro_export]
macro_rules!
decl_environment
{
(
$name:ident
:
$t:ty
)
=>
{
thread_local!
{
static
$name
:
std
::
cell
::
RefCell
<*
mut
$t
>
=
std
::
cell
::
RefCell
::
new
(
0
as
*
mut
$t
);
}
}
}
#[macro_export]
macro_rules!
declare_generic_environment
{
(
$name:ident
:
$t:tt
)
=>
{
mod
$name
{
use
super
::
*
;
decl_environment!
(
GLOBAL
:
$t
<
'static
>
);
pub
fn
using
<
'a
:
'b
,
'b
,
F
:
FnOnce
(),
T
:
'a
>
(
protected
:
&
'b
mut
T
,
f
:
F
)
{
$crate
::
using_environment
(
&
GLOBAL
,
protected
,
f
);
}
pub
fn
with
<
F
:
for
<
'r
,
't
:
'r
>
FnOnce
(
&
'r
mut
$t
<
't
>
)
>
(
f
:
F
)
{
let
dummy
=
();
with_closed
(
f
,
&
dummy
);
}
fn
with_closed
<
'd
:
't
,
't
:
'r
,
'r
,
F
:
FnOnce
(
&
'r
mut
$t
<
't
>
)
>
(
f
:
F
,
_dummy
:
&
'd
(),
)
{
$crate
::
with_environment
(
&
GLOBAL
,
f
);
}
}
}
}
#[macro_export]
macro_rules!
declare_simple_environment
{
(
$name:ident
:
$t:tt
)
=>
{
mod
$name
{
use
super
::
*
;
decl_environment!
(
GLOBAL
:
$t
);
pub
fn
using
<
'a
:
'b
,
'b
,
F
:
FnOnce
(),
T
:
'a
>
(
protected
:
&
'b
mut
T
,
f
:
F
)
{
$crate
::
using_environment
(
&
GLOBAL
,
protected
,
f
);
}
pub
fn
with
<
F
:
for
<
'r
>
FnOnce
(
&
'r
mut
$t
)
>
(
f
:
F
)
{
let
dummy
=
();
with_closed
(
f
,
&
dummy
);
}
fn
with_closed
<
'd
:
'r
,
'r
,
F
:
FnOnce
(
&
'r
mut
$t
)
>
(
f
:
F
,
_dummy
:
&
'd
(),
)
{
$crate
::
with_environment
(
&
GLOBAL
,
f
);
}
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment