Skip to main content

glrmask/
constraint_handle.rs

1use crate::{ConstraintState, Result, Vocab};
2use crate::runtime::Constraint as RuntimeConstraint;
3
4
5#[derive(Clone, Debug)]
6pub struct Constraint {
7    inner: RuntimeConstraint,
8}
9
10impl Constraint {
11    pub(crate) fn from_runtime(inner: RuntimeConstraint) -> Self { Self { inner } }
12
13    pub(crate) fn runtime(&self) -> &RuntimeConstraint { &self.inner }
14    /// Starts a new sequence.
15    pub fn start(&self) -> ConstraintState { self.inner.start() }
16
17    /// The mask's length in bits (max token ID plus one).
18    pub fn mask_bit_len(&self) -> usize { self.inner.mask_bit_len() }
19
20    /// Serializes the compiled constraint for later loading.
21    pub fn save(&self) -> Vec<u8> { self.inner.save() }
22
23    /// May fail if saved by a different GLRMask version. Fails if `vocab` differs from the one it was compiled with.
24    pub fn load(bytes: &[u8], vocab: &Vocab) -> Result<Self> {
25        crate::artifact_load::guard(|| RuntimeConstraint::load(bytes, vocab).map(Self::from_runtime))
26    }
27
28    #[cfg(any(test, feature = "internal-api"))]
29    #[doc(hidden)]
30    pub fn __inner(&self) -> &RuntimeConstraint { &self.inner }
31
32    #[cfg(any(test, feature = "internal-api"))]
33    #[doc(hidden)]
34    pub fn __inner_mut(&mut self) -> &mut RuntimeConstraint { &mut self.inner }
35
36    #[cfg(any(test, feature = "internal-api"))]
37    #[doc(hidden)]
38    pub fn __into_inner(self) -> RuntimeConstraint { self.inner }
39
40    #[cfg(any(test, feature = "internal-api"))]
41    #[doc(hidden)]
42    pub fn __from_inner(inner: RuntimeConstraint) -> Self { Self::from_runtime(inner) }
43}