pub struct Resources(/* private fields */);Expand description
A collection of resources that can be accessed by external effects.
This is used to pass resources to external effects while properly scoping the resource to the running stage graph.
If you want to share a resource across multiple stage graphs, you can use Arc<Mutex<T>> or similar.
§API Design Choices
StageGraph supports single-threaded simulation as well as multi-threaded production code.
Since effect implementations must cover both cases with the same code, the resulting API must
be constrained by both environments. The simulation can easily provided a &mut Resources,
but if we require that, then the production code will have to hold a lock on the Resources
for the whole duration of each effect, serializing all resources.
Therefore, even though not needed in simulation, we design the API so that the effect can use its resources for shorter durations.
§Sync Bound
In order to allow resources to be used without blocking the whole resource collection, shared
references can be obtained with read locking. Since this fundamentally allows shared access
from multiple threads, the resources must be Sync. If your resource is not Sync, you can
use SyncWrapper
or a mutex.
Implementations§
Source§impl Resources
impl Resources
Sourcepub fn put<T: Any + Send + Sync>(&self, resource: T)
pub fn put<T: Any + Send + Sync>(&self, resource: T)
Put a resource into the resources collection.
This variant uses locking to ensure that the resource is not accessed concurrently.
Sourcepub fn get_mut<T: Any + Send + Sync>(
&self,
) -> Result<MappedRwLockWriteGuard<'_, T>>
pub fn get_mut<T: Any + Send + Sync>( &self, ) -> Result<MappedRwLockWriteGuard<'_, T>>
Get a mutable reference to a resource from the resources collection.
This variant takes a write lock on the resource collection, blocking all other operations.
See get for a variant that uses read locking. Concurrent operations will
be blocked while the returned guard is held, so drop it as soon as you
don’t need it any more.
If you need exclusive access to a single resource without blocking the rest of the
resource collection, consider putting an Arc<Mutex<T>> in the resources collection.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Resources
impl !RefUnwindSafe for Resources
impl Send for Resources
impl Sync for Resources
impl Unpin for Resources
impl UnsafeUnpin for Resources
impl !UnwindSafe for Resources
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more