mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 10:01:17 +00:00
Support without_std env in environmental! (#110)
* Make environmental crate support without_std env. * Small doc fixes.
This commit is contained in:
committed by
Gav Wood
parent
1d8a9a6dd3
commit
633b9f4c0b
@@ -2,3 +2,7 @@
|
||||
name = "environmental"
|
||||
version = "0.1.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = []
|
||||
|
||||
@@ -40,12 +40,18 @@
|
||||
//! }
|
||||
//! ```
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::thread::LocalKey;
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
#![cfg_attr(not(feature = "std"), feature(const_fn))]
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
include!("../with_std.rs");
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
include!("../without_std.rs");
|
||||
|
||||
#[doc(hidden)]
|
||||
pub fn using<T: ?Sized, R, F: FnOnce() -> R>(
|
||||
global: &'static LocalKey<RefCell<Option<*mut T>>>,
|
||||
global: &'static imp::LocalKey<imp::RefCell<Option<*mut T>>>,
|
||||
protected: &mut T,
|
||||
f: F
|
||||
) -> R {
|
||||
@@ -59,13 +65,13 @@ pub fn using<T: ?Sized, R, F: FnOnce() -> R>(
|
||||
global.with(|r| {
|
||||
let original = {
|
||||
let mut global = r.borrow_mut();
|
||||
::std::mem::replace(&mut *global, Some(protected as _))
|
||||
imp::replace(&mut *global, Some(protected as _))
|
||||
};
|
||||
|
||||
// even if `f` panics the original will be replaced.
|
||||
struct ReplaceOriginal<'a, T: 'a + ?Sized> {
|
||||
original: Option<*mut T>,
|
||||
global: &'a RefCell<Option<*mut T>>,
|
||||
global: &'a imp::RefCell<Option<*mut T>>,
|
||||
}
|
||||
|
||||
impl<'a, T: 'a + ?Sized> Drop for ReplaceOriginal<'a, T> {
|
||||
@@ -85,7 +91,7 @@ pub fn using<T: ?Sized, R, F: FnOnce() -> R>(
|
||||
|
||||
#[doc(hidden)]
|
||||
pub fn with<T: ?Sized, R, F: FnOnce(&mut T) -> R>(
|
||||
global: &'static LocalKey<RefCell<Option<*mut T>>>,
|
||||
global: &'static imp::LocalKey<imp::RefCell<Option<*mut T>>>,
|
||||
mutator: F,
|
||||
) -> Option<R> {
|
||||
global.with(|r| unsafe {
|
||||
@@ -104,6 +110,7 @@ pub fn with<T: ?Sized, R, F: FnOnce(&mut T) -> R>(
|
||||
/// Declare a new global reference module whose underlying value does not contain references.
|
||||
///
|
||||
/// Will create a module of a given name that contains two functions:
|
||||
///
|
||||
/// * `pub fn using<R, F: FnOnce() -> R>(protected: &mut $t, f: F) -> R`
|
||||
/// This executes `f`, returning its value. During the call, the module's reference is set to
|
||||
/// be equal to `protected`.
|
||||
@@ -163,7 +170,7 @@ macro_rules! environmental {
|
||||
#[allow(non_camel_case_types)]
|
||||
struct $name { __private_field: () }
|
||||
|
||||
thread_local!(static GLOBAL: ::std::cell::RefCell<Option<*mut $t>>
|
||||
thread_local_impl!(static GLOBAL: ::std::cell::RefCell<Option<*mut $t>>
|
||||
= ::std::cell::RefCell::new(None));
|
||||
|
||||
impl $name {
|
||||
@@ -187,8 +194,8 @@ macro_rules! environmental {
|
||||
#[allow(non_camel_case_types)]
|
||||
struct $name { __private_field: () }
|
||||
|
||||
thread_local!(static GLOBAL: ::std::cell::RefCell<Option<*mut ($t + 'static)>>
|
||||
= ::std::cell::RefCell::new(None));
|
||||
thread_local_impl!(static GLOBAL: $crate::imp::RefCell<Option<*mut ($t + 'static)>>
|
||||
= $crate::imp::RefCell::new(None));
|
||||
|
||||
impl $name {
|
||||
#[allow(unused_imports)]
|
||||
@@ -198,7 +205,7 @@ macro_rules! environmental {
|
||||
f: F
|
||||
) -> R {
|
||||
let lifetime_extended = unsafe {
|
||||
::std::mem::transmute::<&mut $t, &mut ($t + 'static)>(protected)
|
||||
$crate::imp::transmute::<&mut $t, &mut ($t + 'static)>(protected)
|
||||
};
|
||||
$crate::using(&GLOBAL, lifetime_extended, f)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
// Copyright 2018 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/>.
|
||||
|
||||
#[doc(hidden)]
|
||||
pub mod imp {
|
||||
pub use std::cell::RefCell;
|
||||
pub use std::thread::LocalKey;
|
||||
pub use std::mem::transmute;
|
||||
pub use std::mem::replace;
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export]
|
||||
macro_rules! thread_local_impl {
|
||||
($(#[$attr:meta])* static $name:ident: $t:ty = $init:expr) => (
|
||||
thread_local!($(#[$attr])* static $name: $t = $init);
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
// Copyright 2018 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/>.
|
||||
|
||||
#[doc(hidden)]
|
||||
pub mod imp {
|
||||
pub use core::cell::RefCell;
|
||||
pub use core::mem::transmute;
|
||||
pub use core::mem::replace;
|
||||
|
||||
// This code is a simplified version of [`LocalKey`] and it's wasm32 specialization: [`statik::Key`].
|
||||
// [`LocalKey`]: https://github.com/alexcrichton/rust/blob/98931165a23a1c2860d99759385f45d6807c8982/src/libstd/thread/local.rs#L89
|
||||
// [`statik::Key`]: https://github.com/alexcrichton/rust/blob/98931165a23a1c2860d99759385f45d6807c8982/src/libstd/thread/local.rs#L310-L312
|
||||
|
||||
pub struct LocalKey<T: 'static> {
|
||||
pub init: fn() -> T,
|
||||
pub inner: RefCell<Option<T>>,
|
||||
}
|
||||
|
||||
// This is safe as long there is no threads in wasm32.
|
||||
unsafe impl<T: 'static> ::core::marker::Sync for LocalKey<T> { }
|
||||
|
||||
impl<T: 'static> LocalKey<T> {
|
||||
pub const fn new(init: fn() -> T) -> LocalKey<T> {
|
||||
LocalKey {
|
||||
init,
|
||||
inner: RefCell::new(None),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with<F, R>(&'static self, f: F) -> R
|
||||
where F: FnOnce(&T) -> R
|
||||
{
|
||||
if self.inner.borrow().is_none() {
|
||||
let v = (self.init)();
|
||||
*self.inner.borrow_mut() = Some(v);
|
||||
}
|
||||
// This code can't panic because:
|
||||
// 1. `inner` can be borrowed mutably only once at the initialization time.
|
||||
// 2. After the initialization `inner` is always `Some`.
|
||||
f(&*self.inner.borrow().as_ref().unwrap())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export]
|
||||
macro_rules! thread_local_impl {
|
||||
($(#[$attr:meta])* static $name:ident: $t:ty = $init:expr) => (
|
||||
$(#[$attr])*
|
||||
static $name: $crate::imp::LocalKey<$t> = {
|
||||
fn __init() -> $t { $init }
|
||||
|
||||
$crate::imp::LocalKey::new(__init)
|
||||
};
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user