Skip to main content

MatchOptions

Struct MatchOptions 

Source
pub struct MatchOptions {
    pub max_steps: Option<u64>,
    pub max_depth: Option<usize>,
}
Expand description

Resource limits for recognition.

Fields§

§max_steps: Option<u64>

Give up after this many steps, rather than running unboundedly.

Default None. Exceeding it is MatchError::StepLimit, never a silent rejection: “does not match” and “could not decide” are different answers (D16).

§max_depth: Option<usize>

Give up after recursing this deep. Default DEFAULT_MAX_DEPTH.

§Why this one has a default when max_steps does not

Exceeding a step limit returns MatchError::StepLimit; exceeding the stack aborts the process, and no caller can catch that or report it as “could not decide”. An oracle is fed adversarial input by design, so the safe default is a limit rather than a crash.

§Depth, stack, and how they relate

Depth counts grammar nodes entered, not input characters. Nesting in the input turns into depth because a recursive rule re-enters itself, at a rate the grammar fixes: for RFC 8259 one level of [ costs six nodes, so the default allows JSON nested about 40 deep. A flat input costs almost nothing however long it is — length is not the problem, nesting is.

Each level costs roughly 2 KB of stack, measured on a debug build; a release build uses less. That fixes the ceiling at about one level per 2 KB:

stackdeepest safe max_depth
512 KB128
1 MB400
2 MiB — what std::thread gives a spawned thread900
64 MB~30,000

The default sits under the 1 MB row, so it is safe wherever the recognizer is called from. Raising it means giving the recognizer a bigger stack to match:

std::thread::Builder::new()
    .stack_size(64 * 1024 * 1024)
    .spawn(move || {
        let mut recognizer = Recognizer::new(&grammar, "a").with_options(MatchOptions {
            max_depth: Some(40_000),
            ..MatchOptions::default()
        });
        recognizer.accepts("start")
    })?
    .join()
    .expect("no overflow")?;

None removes the limit, which is only safe if the input’s nesting is already bounded by something else.

Trait Implementations§

Source§

impl Clone for MatchOptions

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for MatchOptions

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for MatchOptions

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Eq for MatchOptions

Source§

impl PartialEq for MatchOptions

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for MatchOptions

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.