pub struct EntityResource<T> { /* private fields */ }Expand description
A resource that survives entity restarts (shard movement, crash recovery).
When an entity handler is respawned due to a crash, the EntityResource is
NOT closed — only the handler is recreated. The resource is only closed on
explicit close() or when the entity is reaped.
While an EntityResource is active (not closed), the entity’s on_idle()
should return true to prevent idle reaping. Use is_active() to check.
§Example
let resource = EntityResource::new(MyConnection::new().await?);
// Use the resource
let conn = resource.read().await;
conn.query("SELECT 1").await?;
// Check if active for keep-alive
if resource.is_active() {
// Entity should not be reaped
}
// Explicit close when done
resource.close().await;Implementations§
Source§impl<T> EntityResource<T>
impl<T> EntityResource<T>
Sourcepub async fn read(&self) -> Option<RwLockReadGuard<'_, Option<T>>>
pub async fn read(&self) -> Option<RwLockReadGuard<'_, Option<T>>>
Read access to the resource value.
Returns None if the resource has been closed.
Sourcepub async fn write(&self) -> Option<RwLockWriteGuard<'_, Option<T>>>
pub async fn write(&self) -> Option<RwLockWriteGuard<'_, Option<T>>>
Write access to the resource value.
Returns None if the resource has been closed.
Sourcepub async fn close(&self)
pub async fn close(&self)
Close the resource, dropping the inner value and marking it inactive.
After closing, is_active() returns false and read()/write()
return None. The entity can then be reaped on idle.
Sourcepub fn close_token(&self) -> CancellationToken
pub fn close_token(&self) -> CancellationToken
Returns a CancellationToken that is cancelled when the resource is
closed. Useful for tying background tasks to the resource lifetime.
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for EntityResource<T>
impl<T> !RefUnwindSafe for EntityResource<T>
impl<T> Send for EntityResource<T>
impl<T> Sync for EntityResource<T>
impl<T> Unpin for EntityResource<T>
impl<T> UnsafeUnpin for EntityResource<T>
impl<T> !UnwindSafe for EntityResource<T>
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> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
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 moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request