
# Background In `no_std` compatible crates, there is often an `std` feature which will allow access to the standard library. Currently, with the `std` feature _enabled_, the [`std::prelude`](https://doc.rust-lang.org/std/prelude/index.html) is implicitly imported in all modules. With the feature _disabled_, instead the [`core::prelude`](https://doc.rust-lang.org/core/prelude/index.html) is implicitly imported. This creates a subtle and pervasive issue where `alloc` items _may_ be implicitly included (if `std` is enabled), or must be explicitly included (if `std` is not enabled). # Objective - Make the implicit imports for `no_std` crates consistent regardless of what features are/not enabled. ## Solution - Replace the `cfg_attr` "double negative" `no_std` attribute with conditional compilation to _include_ `std` as an external crate. ```rust // Before #![cfg_attr(not(feature = "std"), no_std)] // After #![no_std] #[cfg(feature = "std")] extern crate std; ``` - Fix imports that are currently broken but are only now visible with the above fix. ## Testing - CI ## Notes I had previously used the "double negative" version of `no_std` based on general consensus that it was "cleaner" within the Rust embedded community. However, this implicit prelude issue likely was considered when forming this consensus. I believe the reason why is the items most affected by this issue are provided by the `alloc` crate, which is rarely used within embedded but extensively used within Bevy.
78 lines
2.5 KiB
Rust
78 lines
2.5 KiB
Rust
use alloc::vec::Vec;
|
|
use core::{cell::RefCell, ops::DerefMut};
|
|
use thread_local::ThreadLocal;
|
|
|
|
/// A cohesive set of thread-local values of a given type.
|
|
///
|
|
/// Mutable references can be fetched if `T: Default` via [`Parallel::scope`].
|
|
#[derive(Default)]
|
|
pub struct Parallel<T: Send> {
|
|
locals: ThreadLocal<RefCell<T>>,
|
|
}
|
|
|
|
/// A scope guard of a `Parallel`, when this struct is dropped ,the value will writeback to its `Parallel`
|
|
impl<T: Send> Parallel<T> {
|
|
/// Gets a mutable iterator over all of the per-thread queues.
|
|
pub fn iter_mut(&mut self) -> impl Iterator<Item = &'_ mut T> {
|
|
self.locals.iter_mut().map(RefCell::get_mut)
|
|
}
|
|
|
|
/// Clears all of the stored thread local values.
|
|
pub fn clear(&mut self) {
|
|
self.locals.clear();
|
|
}
|
|
}
|
|
|
|
impl<T: Default + Send> Parallel<T> {
|
|
/// Retrieves the thread-local value for the current thread and runs `f` on it.
|
|
///
|
|
/// If there is no thread-local value, it will be initialized to its default.
|
|
pub fn scope<R>(&self, f: impl FnOnce(&mut T) -> R) -> R {
|
|
let mut cell = self.locals.get_or_default().borrow_mut();
|
|
let ret = f(cell.deref_mut());
|
|
ret
|
|
}
|
|
|
|
/// Mutably borrows the thread-local value.
|
|
///
|
|
/// If there is no thread-local value, it will be initialized to it's default.
|
|
pub fn borrow_local_mut(&self) -> impl DerefMut<Target = T> + '_ {
|
|
self.locals.get_or_default().borrow_mut()
|
|
}
|
|
}
|
|
|
|
impl<T, I> Parallel<I>
|
|
where
|
|
I: IntoIterator<Item = T> + Default + Send + 'static,
|
|
{
|
|
/// Drains all enqueued items from all threads and returns an iterator over them.
|
|
///
|
|
/// Unlike [`Vec::drain`], this will piecemeal remove chunks of the data stored.
|
|
/// If iteration is terminated part way, the rest of the enqueued items in the same
|
|
/// chunk will be dropped, and the rest of the undrained elements will remain.
|
|
///
|
|
/// The ordering is not guaranteed.
|
|
pub fn drain(&mut self) -> impl Iterator<Item = T> + '_ {
|
|
self.locals.iter_mut().flat_map(|item| item.take())
|
|
}
|
|
}
|
|
|
|
#[cfg(feature = "alloc")]
|
|
impl<T: Send> Parallel<Vec<T>> {
|
|
/// Collect all enqueued items from all threads and appends them to the end of a
|
|
/// single Vec.
|
|
///
|
|
/// The ordering is not guaranteed.
|
|
pub fn drain_into(&mut self, out: &mut Vec<T>) {
|
|
let size = self
|
|
.locals
|
|
.iter_mut()
|
|
.map(|queue| queue.get_mut().len())
|
|
.sum();
|
|
out.reserve(size);
|
|
for queue in self.locals.iter_mut() {
|
|
out.append(queue.get_mut());
|
|
}
|
|
}
|
|
}
|