pub struct TwoDaCache<'a> { /* private fields */ }Expand description
Resolver-backed cache of parsed 2DA tables.
Borrows a Resolver for resref-to-bytes lookups and owns a
lazily-populated HashMap of parsed TwoDa tables keyed by
TwoDaName. Construct once, reuse across decode passes.
Single-threaded by design: Self::twoda takes &mut self so
the cache can grow on miss without interior mutability or
Arc<Mutex<_>> overhead. If a decoder needs data from two tables
in one call, it should pull the typed primitives it needs out of
the first borrow before requesting the next.
Implementations§
Source§impl<'a> TwoDaCache<'a>
impl<'a> TwoDaCache<'a>
Sourcepub fn new(resolver: &'a Resolver<'a>) -> Self
pub fn new(resolver: &'a Resolver<'a>) -> Self
Constructs a new cache backed by the given resolver.
The resolver is borrowed for the lifetime of the cache, so the caller is free to keep adding sources to the resolver before constructing the cache but must not mutate it through that reference once the cache exists.
Sourcepub fn twoda(
&mut self,
name: impl AsRef<str>,
) -> Result<&TwoDa, TwoDaCacheError>
pub fn twoda( &mut self, name: impl AsRef<str>, ) -> Result<&TwoDa, TwoDaCacheError>
Returns a reference to the named 2DA, lazily loading and caching it on first request.
name is anything that yields a bare table name string without
the .2da extension. Two natural inputs:
- a
&strliteral (cache.twoda("itempropdef")), - a
TwoDaNameconstant from thetablesmodule (cache.twoda(tables::ITEMPROPDEF)).
On a cache miss the name is validated, the resolver is asked
for the table, and on hit the bytes are parsed via
read_twoda_from_bytes before being inserted into the
cache.
Errors:
TwoDaCacheError::InvalidNamewhen the resolved string failsResRefvalidation. Cannot fire when the input is aTwoDaName(already validated) or atablesconstant (validated at compile time).TwoDaCacheError::Missingwhen no resolver source contains the requested table.TwoDaCacheError::Parsewhen the bytes are present but fail to parse as a valid 2DA.