pub trait TokenConstraint {
// Required method
fn allowed(&self, state: &DecodeState<'_>) -> AllowedSet;
}Expand description
A rule that says which next tokens are valid, given what’s been decoded so far.
The whole grammar-constrained-decoding feature hangs off this one method.
Implementors range from the dead-simple (AllowedTokens ignores the
state and returns a fixed set) to the stateful (JsonStructure replays
the generated bytes to work out where in the JSON grammar it is). A future
full-CFG or JSON-schema constraint is just another impl — the masking
core (mask_logits) and the sampler wrapper (ConstrainedSampler)
never need to know which one they’re driving.
§Contract
allowed must be a pure function of state: same generated prefix ->
same AllowedSet. No interior mutability that changes the answer across
identical calls, or reproducibility (and the tests that pin it) break.
Required Methods§
Sourcefn allowed(&self, state: &DecodeState<'_>) -> AllowedSet
fn allowed(&self, state: &DecodeState<'_>) -> AllowedSet
The set of token ids that keep the output valid if emitted next.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".