Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Support without_std env in environmental! #110

Merged
merged 2 commits into from
Apr 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions substrate/environmental/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
name = "environmental"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]

[features]
default = ["std"]
std = []
27 changes: 17 additions & 10 deletions substrate/environmental/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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> {
Expand All @@ -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 {
Expand All @@ -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`.
Expand Down Expand Up @@ -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 {
Expand All @@ -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)]
Expand All @@ -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)
}
Expand Down
31 changes: 31 additions & 0 deletions substrate/environmental/with_std.rs
Original file line number Diff line number Diff line change
@@ -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);
);
}
69 changes: 69 additions & 0 deletions substrate/environmental/without_std.rs
Original file line number Diff line number Diff line change
@@ -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)
};
);
}