pub struct DepthCounter { /* private fields */ }Expand description
A lightweight depth counter for stack overflow protection.
Unlike RecursionGuard, DepthCounter does not track which keys are
being visited — it only limits nesting depth. Use this when:
- The same node/key may be legitimately revisited (e.g., expression re-checking with different contextual types)
- You only need stack overflow protection, not cycle detection
§Safety
Shares the same debug-mode safety features as RecursionGuard:
- Debug leak detection: Dropping with depth > 0 panics.
- Debug underflow detection: Calling
leave()at depth 0 panics.
§Usage
let mut counter = DepthCounter::with_profile(RecursionProfile::ExpressionCheck);
if !counter.enter() {
return TypeId::ERROR; // depth exceeded
}
let result = do_work();
counter.leave();
resultImplementations§
Source§impl DepthCounter
impl DepthCounter
Sourcepub const fn new(max_depth: u32) -> Self
pub const fn new(max_depth: u32) -> Self
Create a counter with an explicit max depth.
Prefer with_profile for standard use cases.
Sourcepub const fn with_profile(profile: RecursionProfile) -> Self
pub const fn with_profile(profile: RecursionProfile) -> Self
Create a counter from a named RecursionProfile.
Only the profile’s max_depth is used (iterations are not relevant
for a depth-only counter).
Sourcepub const fn with_initial_depth(max_depth: u32, initial_depth: u32) -> Self
pub const fn with_initial_depth(max_depth: u32, initial_depth: u32) -> Self
Create a counter with an initial depth already set.
Used when inheriting depth from a parent context to maintain the overall depth limit across context boundaries. The inherited depth is treated as the “base” — debug leak detection only fires if depth exceeds this base at drop time.
Sourcepub const fn enter(&mut self) -> bool
pub const fn enter(&mut self) -> bool
Try to enter a deeper level.
Returns true if the depth limit has not been reached and entry
is allowed. The caller must call leave when done.
Returns false if the depth limit has been reached. The exceeded
flag is set and the depth is not incremented — do not call
leave() in this case.
Sourcepub const fn is_exceeded(&self) -> bool
pub const fn is_exceeded(&self) -> bool
Returns true if the depth limit was previously exceeded.
Sticky — stays true until reset.
Sourcepub const fn mark_exceeded(&mut self)
pub const fn mark_exceeded(&mut self)
Manually mark as exceeded.