mirror of
https://github.com/bevyengine/bevy
synced 2025-01-06 18:28:59 +00:00
9d67edc3a6
# Objective Split - containing only the fixed typos - https://github.com/bevyengine/bevy/pull/12036#pullrequestreview-1894738751 # Migration Guide In `crates/bevy_mikktspace/src/generated.rs` ```rs // before pub struct SGroup { pub iVertexRepresentitive: i32, .. } // after pub struct SGroup { pub iVertexRepresentative: i32, .. } ``` In `crates/bevy_core_pipeline/src/core_2d/mod.rs` ```rs // before Node2D::ConstrastAdaptiveSharpening // after Node2D::ContrastAdaptiveSharpening ``` --------- Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com> Co-authored-by: James Liu <contact@jamessliu.com> Co-authored-by: François <mockersf@gmail.com>
72 lines
2.2 KiB
Rust
72 lines
2.2 KiB
Rust
use core::cell::Cell;
|
|
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<Cell<T>>,
|
|
}
|
|
|
|
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(|cell| cell.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 it's default.
|
|
pub fn scope<R>(&self, f: impl FnOnce(&mut T) -> R) -> R {
|
|
let cell = self.locals.get_or_default();
|
|
let mut value = cell.take();
|
|
let ret = f(&mut value);
|
|
cell.set(value);
|
|
ret
|
|
}
|
|
}
|
|
|
|
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<B>(&mut self) -> impl Iterator<Item = T> + '_
|
|
where
|
|
B: FromIterator<T>,
|
|
{
|
|
self.locals.iter_mut().flat_map(|item| item.take())
|
|
}
|
|
}
|
|
|
|
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());
|
|
}
|
|
}
|
|
}
|