Struct ResourceGuard
pub struct ResourceGuard {
max_memory_bytes: AtomicU64,
current_memory_bytes: AtomicU64,
reserved_bytes: AtomicU64,
enforce_limits: AtomicBool,
safety_margin: f32,
}Expand description
Resource guard for preventing system overload.
Tracks memory allocation and provides checks before large allocations.
Fields§
§max_memory_bytes: AtomicU64§current_memory_bytes: AtomicU64§reserved_bytes: AtomicU64§enforce_limits: AtomicBool§safety_margin: f32Implementations§
§impl ResourceGuard
impl ResourceGuard
pub fn new() -> ResourceGuard
pub fn new() -> ResourceGuard
Creates a new resource guard with default limits.
pub fn with_max_memory(max_bytes: u64) -> ResourceGuard
pub fn with_max_memory(max_bytes: u64) -> ResourceGuard
Creates a resource guard with a specific memory limit.
pub fn with_safety_margin(self, margin: f32) -> ResourceGuard
pub fn with_safety_margin(self, margin: f32) -> ResourceGuard
Creates a resource guard with custom safety margin.
pub fn unguarded() -> ResourceGuard
pub fn unguarded() -> ResourceGuard
Creates a resource guard that doesn’t enforce limits (for testing).
pub fn set_max_memory(&self, max_bytes: u64)
pub fn set_max_memory(&self, max_bytes: u64)
Sets the maximum memory limit.
pub fn max_memory(&self) -> u64
pub fn max_memory(&self) -> u64
Gets the maximum memory limit.
pub fn current_memory(&self) -> u64
pub fn current_memory(&self) -> u64
Gets the current tracked memory usage.
pub fn reserved_memory(&self) -> u64
pub fn reserved_memory(&self) -> u64
Gets the currently reserved memory.
pub fn available_memory(&self) -> u64
pub fn available_memory(&self) -> u64
Gets the effective available memory (max - current - reserved).
pub fn can_allocate(&self, bytes: u64) -> bool
pub fn can_allocate(&self, bytes: u64) -> bool
Checks if a given allocation can proceed.
pub fn can_allocate_safe(&self, bytes: u64) -> Result<(), ResourceError>
pub fn can_allocate_safe(&self, bytes: u64) -> Result<(), ResourceError>
Checks if an allocation can proceed, also checking system memory.
pub fn record_allocation(&self, bytes: u64)
pub fn record_allocation(&self, bytes: u64)
Records a memory allocation.
pub fn record_deallocation(&self, bytes: u64)
pub fn record_deallocation(&self, bytes: u64)
Records a memory deallocation.
pub fn reserve(&self, bytes: u64) -> Result<ReservationGuard<'_>, ResourceError>
pub fn reserve(&self, bytes: u64) -> Result<ReservationGuard<'_>, ResourceError>
Reserves memory for a future allocation.
Returns a guard that releases the reservation on drop.
pub fn validate(&self, estimate: &MemoryEstimate) -> Result<(), ResourceError>
pub fn validate(&self, estimate: &MemoryEstimate) -> Result<(), ResourceError>
Validates a memory estimate before allocation.
pub fn max_safe_elements(&self, bytes_per_element: usize) -> usize
pub fn max_safe_elements(&self, bytes_per_element: usize) -> usize
Returns the maximum safe element count for a given per-element byte cost.
pub fn set_enforce_limits(&self, enforce: bool)
pub fn set_enforce_limits(&self, enforce: bool)
Enables or disables limit enforcement.
pub fn is_enforcing(&self) -> bool
pub fn is_enforcing(&self) -> bool
Returns whether limits are being enforced.
Trait Implementations§
§impl Debug for ResourceGuard
impl Debug for ResourceGuard
§impl Default for ResourceGuard
impl Default for ResourceGuard
§fn default() -> ResourceGuard
fn default() -> ResourceGuard
Auto Trait Implementations§
impl !Freeze for ResourceGuard
impl RefUnwindSafe for ResourceGuard
impl Send for ResourceGuard
impl Sync for ResourceGuard
impl Unpin for ResourceGuard
impl UnwindSafe for ResourceGuard
Blanket Implementations§
§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
§type ArchivedMetadata = ()
type ArchivedMetadata = ()
§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
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
§impl<F, W, T, D> Deserialize<With<T, W>, D> for F
impl<F, W, T, D> Deserialize<With<T, W>, D> for F
§fn deserialize(
&self,
deserializer: &mut D,
) -> Result<With<T, W>, <D as Fallible>::Error>
fn deserialize( &self, deserializer: &mut D, ) -> Result<With<T, W>, <D as Fallible>::Error>
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§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