pub trait TocCache:
Clone
+ Send
+ Sync
+ 'static {
// Required methods
fn get_toc(&self, key: &[u8]) -> Option<String>;
fn store_toc(&self, key: &[u8], toc: &str);
}Expand description
A trait for caching Table of Contents (TOC) data.
This trait provides methods for storing and retrieving TOC information using an opaque byte key. Implementations can use this to avoid re-fetching TOC data when the key matches a cached version.
The key is constructed by the library and should be treated as an opaque identifier. Implementors are free to encode it in whatever way suits their storage backend (e.g., hex encoding for filenames, raw bytes for in-memory maps).
§Concurrency
Both methods take &self to allow concurrent reads during parallel TOC fetching
(Log and Param subsystems fetch their TOCs simultaneously). Implementations should
use interior mutability (e.g., RwLock) for thread-safe caching.
§Example
use std::sync::{Arc, RwLock};
use std::collections::HashMap;
use crazyflie_lib::TocCache;
#[derive(Clone)]
struct InMemoryCache {
data: Arc<RwLock<HashMap<Vec<u8>, String>>>,
}
impl TocCache for InMemoryCache {
fn get_toc(&self, key: &[u8]) -> Option<String> {
self.data.read().ok()?.get(key).cloned()
}
fn store_toc(&self, key: &[u8], toc: &str) {
if let Ok(mut lock) = self.data.write() {
lock.insert(key.to_vec(), toc.to_string());
}
}
}Required Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.