mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-30 18:14:18 +01:00
Dependencies on `std` that still exist: * Locks * `std::error::Error` (waiting for Rust 1.81) * `std::os::raw::c_char` for FFI * `thread_local` and `Backtrace` in `snatch`
37 lines
975 B
Rust
37 lines
975 B
Rust
use alloc::{sync::Arc, vec::Vec};
|
|
use core::slice::Iter;
|
|
|
|
/// A tracker that holds strong references to resources.
|
|
///
|
|
/// This is only used to keep resources alive.
|
|
#[derive(Debug)]
|
|
pub(crate) struct StatelessTracker<T> {
|
|
resources: Vec<Arc<T>>,
|
|
}
|
|
|
|
impl<T> StatelessTracker<T> {
|
|
pub fn new() -> Self {
|
|
Self {
|
|
resources: Vec::new(),
|
|
}
|
|
}
|
|
|
|
/// Inserts a single resource into the resource tracker.
|
|
///
|
|
/// Returns a reference to the newly inserted resource.
|
|
/// (This allows avoiding a clone/reference count increase in many cases.)
|
|
pub fn insert_single(&mut self, resource: Arc<T>) -> &Arc<T> {
|
|
self.resources.push(resource);
|
|
unsafe { self.resources.last().unwrap_unchecked() }
|
|
}
|
|
}
|
|
|
|
impl<'a, T> IntoIterator for &'a StatelessTracker<T> {
|
|
type Item = &'a Arc<T>;
|
|
type IntoIter = Iter<'a, Arc<T>>;
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
self.resources.as_slice().iter()
|
|
}
|
|
}
|