referrerpolicy=no-referrer-when-downgrade

sp_externalities/
scope_limited.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Stores the externalities in an `environmental` value to make it scope limited available.
19
20use crate::Externalities;
21
22environmental::environmental!(ext: trait Externalities);
23
24/// Set the given externalities while executing the given closure. To get access to the
25/// externalities while executing the given closure [`with_externalities`] grants access to them.
26/// The externalities are only set for the same thread this function was called from.
27pub fn set_and_run_with_externalities<F, R>(ext: &mut dyn Externalities, f: F) -> R
28where
29	F: FnOnce() -> R,
30{
31	ext::using(ext, f)
32}
33
34/// Execute the given closure with the currently set externalities.
35///
36/// Returns `None` if no externalities are set or `Some(_)` with the result of the closure.
37pub fn with_externalities<F: FnOnce(&mut dyn Externalities) -> R, R>(f: F) -> Option<R> {
38	ext::with(f)
39}