pub struct CompiledGrammar { /* private fields */ }Expand description
This is the primary object to store compiled grammar.
A CompiledGrammar can be used to construct GrammarMatcher to generate token masks efficiently.
§Notes
Do not construct this class directly, instead use GrammarCompiler to construct the object.
Implementations§
Source§impl CompiledGrammar
impl CompiledGrammar
Sourcepub fn tokenizer_info(&self) -> TokenizerInfo
pub fn tokenizer_info(&self) -> TokenizerInfo
The tokenizer info associated with the compiled grammar.
Sourcepub fn memory_size_bytes(&self) -> usize
pub fn memory_size_bytes(&self) -> usize
The approximate memory usage of the compiled grammar in bytes.
Sourcepub fn serialize_json(&self) -> String
pub fn serialize_json(&self) -> String
Serialize the compiled grammar to a JSON string. It will serialize the compiled grammar without the tokenizer info, since the tokenizer info is shared by multiple compiled grammars.
§Notes
The metadata of the tokenizer info is serialized and will be checked when deserializing.
Sourcepub fn deserialize_json(
json: &str,
tokenizer_info: &TokenizerInfo,
) -> Result<Self, String>
pub fn deserialize_json( json: &str, tokenizer_info: &TokenizerInfo, ) -> Result<Self, String>
Deserialize the compiled grammar from a JSON string and associate it with the specified tokenizer info.
Returns
- Ok(CompiledGrammar) on success
- Err(String) if the JSON is invalid, format mismatch, version mismatch, or tokenizer metadata does not match. The error string mirrors the C++ exception message.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for CompiledGrammar
impl !RefUnwindSafe for CompiledGrammar
impl !Send for CompiledGrammar
impl !Sync for CompiledGrammar
impl Unpin for CompiledGrammar
impl UnwindSafe for CompiledGrammar
Blanket Implementations§
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
Mutably borrows from an owned value. Read more