@@ -72,46 +72,25 @@ macro_rules! parser_err {
7272mod alter;
7373mod merge;
7474
75- #[cfg(feature = "std")]
76- /// Implementation [`RecursionCounter`] if std is available
7775mod recursion {
78- use std::cell::Cell ;
79- use std::rc::Rc ;
76+ use alloc::rc::Rc ;
77+ use core::cell::Cell ;
8078
8179 use super::ParserError;
8280
83- /// Tracks remaining recursion depth. This value is decremented on
84- /// each call to [`RecursionCounter::try_decrease()`], when it reaches 0 an error will
85- /// be returned.
86- ///
87- /// Note: Uses an [`std::rc::Rc`] and [`std::cell::Cell`] in order to satisfy the Rust
88- /// borrow checker so the automatic [`DepthGuard`] decrement a
89- /// reference to the counter.
90- ///
91- /// Note: when "recursive-protection" feature is enabled, this crate uses additional stack overflow protection
92- /// for some of its recursive methods. See [`recursive::recursive`] for more information.
9381 pub(crate) struct RecursionCounter {
9482 remaining_depth: Rc<Cell<usize>>,
9583 }
9684
9785 impl RecursionCounter {
98- /// Creates a [`RecursionCounter`] with the specified maximum
99- /// depth
10086 pub fn new(remaining_depth: usize) -> Self {
10187 Self {
10288 remaining_depth: Rc::new(remaining_depth.into()),
10389 }
10490 }
10591
106- /// Decreases the remaining depth by 1.
107- ///
108- /// Returns [`Err`] if the remaining depth falls to 0.
109- ///
110- /// Returns a [`DepthGuard`] which will adds 1 to the
111- /// remaining depth upon drop;
11292 pub fn try_decrease(&self) -> Result<DepthGuard, ParserError> {
11393 let old_value = self.remaining_depth.get();
114- // ran out of space
11594 if old_value == 0 {
11695 Err(ParserError::RecursionLimitExceeded)
11796 } else {
@@ -121,7 +100,6 @@ mod recursion {
121100 }
122101 }
123102
124- /// Guard that increases the remaining depth by 1 on drop
125103 pub struct DepthGuard {
126104 remaining_depth: Rc<Cell<usize>>,
127105 }
@@ -131,33 +109,13 @@ mod recursion {
131109 Self { remaining_depth }
132110 }
133111 }
112+
134113 impl Drop for DepthGuard {
135114 fn drop(&mut self) {
136115 let old_value = self.remaining_depth.get();
137- self.remaining_depth.set(old_value + 1);
138- }
139- }
140- }
141-
142- #[cfg(not(feature = "std"))]
143- mod recursion {
144- /// Implementation [`RecursionCounter`] if std is NOT available (and does not
145- /// guard against stack overflow).
146- ///
147- /// Has the same API as the std [`RecursionCounter`] implementation
148- /// but does not actually limit stack depth.
149- pub(crate) struct RecursionCounter {}
150-
151- impl RecursionCounter {
152- pub fn new(_remaining_depth: usize) -> Self {
153- Self {}
154- }
155- pub fn try_decrease(&self) -> Result<DepthGuard, super::ParserError> {
156- Ok(DepthGuard {})
116+ self.remaining_depth.set(old_value.saturating_add(1));
157117 }
158118 }
159-
160- pub struct DepthGuard {}
161119}
162120
163121#[derive(PartialEq, Eq)]
0 commit comments