Get the cached texture by its key, or create it using the provided function and cache it.
(&mut self, key: String, create_fn: F)
| 331 | |
| 332 | /// Get the cached texture by its key, or create it using the provided function and cache it. |
| 333 | pub fn get_or_create<F>(&mut self, key: String, create_fn: F) -> &Texture2D |
| 334 | where F: FnOnce() -> Texture2D |
| 335 | { |
| 336 | if !self.textures.contains_key(&key) { |
| 337 | let texture = create_fn(); |
| 338 | self.textures.insert(key.clone(), CacheEntry { frames_not_used: 0, owner: texture.into() }); |
| 339 | } |
| 340 | let entry = self.textures.get_mut(&key).unwrap(); |
| 341 | entry.frames_not_used = 0; |
| 342 | entry.owner.texture() |
| 343 | } |
| 344 | |
| 345 | pub async fn get_or_create_async<F, Fut>(&mut self, key: String, create_fn: F) -> &Texture2D |
| 346 | where F: FnOnce() -> Fut, |