Skip to main content

TransformerCache

Struct TransformerCache 

Source
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

Source

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.

Source

pub fn capacity(&self) -> usize

Returns the effective capacity (post-clamp).

Source

pub fn len(&self) -> usize

Returns the number of entries currently cached.

Source

pub fn is_empty(&self) -> bool

Returns true when the cache holds no entries.

Source

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.

Source

pub fn clear(&self)

Removes all cached entries. Capacity is preserved.

Source

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.

Source

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 Arc is cloned and returned; the key is promoted to the front of the MRU order.
  • Miss — the cache mutex is released, a fresh Transformer::from_epsg is 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-cached Arc is 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

Source§

fn clone(&self) -> TransformerCache

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TransformerCache

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.