pub struct TransformerCache { /* private fields */ }Expand description
Thread-safe LRU cache for Transformer instances.
TransformerCache is cheap to clone — internally a single
Arc<Mutex<…>> is shared. All clones see the same cache; entries
inserted through one handle are visible through every other.
Lookups, insertions, and eviction all run under a single mutex. The mutex is dropped while a fresh transformer is being built, so a single slow build does not block parallel hits for other EPSG pairs.
Implementations§
Source§impl TransformerCache
impl TransformerCache
Sourcepub fn new(capacity: usize) -> Self
pub fn new(capacity: usize) -> Self
Constructs a new cache with at most capacity cached entries.
capacity is clamped to a minimum of 1: a zero-capacity cache
would be useless (every call would have to rebuild from
scratch), so the clamp gracefully handles invalid input rather
than panicking.
Sourcepub fn contains(&self, src_epsg: u32, dst_epsg: u32) -> bool
pub fn contains(&self, src_epsg: u32, dst_epsg: u32) -> bool
Returns true when the given EPSG pair is currently cached.
This does not promote the key in the MRU order — it is a pure read query intended for introspection / metrics / tests.
Sourcepub fn cached_keys(&self) -> Vec<TransformerKey>
pub fn cached_keys(&self) -> Vec<TransformerKey>
Returns a snapshot of cached keys in MRU order (front = newest, back = oldest).
Intended for introspection and tests: the cache is locked only briefly to copy the queue, then released before the snapshot is returned, so the result is a point-in-time view that may go stale immediately.
Sourcepub fn get_or_build(
&self,
src_epsg: u32,
dst_epsg: u32,
) -> Result<Arc<Transformer>, Error>
pub fn get_or_build( &self, src_epsg: u32, dst_epsg: u32, ) -> Result<Arc<Transformer>, Error>
Returns a cached transformer for the (src_epsg, dst_epsg)
pair, building and inserting one on miss.
§Behaviour
- Hit — the cached
Arcis cloned and returned; the key is promoted to the front of the MRU order. - Miss — the cache mutex is released, a fresh
Transformer::from_epsgis constructed, then the mutex is re-acquired and the entry is inserted. If another thread raced in and inserted first, the freshly built transformer is dropped and the already-cachedArcis returned, preserving single-copy semantics. - Eviction — when at
capacity, the back of the MRU queue is removed before the new entry is inserted.
§Errors
Propagates any error from Transformer::from_epsg — typically
Error::EpsgCodeNotFound for unknown codes, or
Error::ProjectionInitError when the underlying proj4rs
initialisation fails.
Trait Implementations§
Source§impl Clone for TransformerCache
impl Clone for TransformerCache
Source§fn clone(&self) -> TransformerCache
fn clone(&self) -> TransformerCache
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more