| Crates.io | preloader |
| lib.rs | preloader |
| version | 0.1.3 |
| created_at | 2025-06-24 02:11:50.022749+00 |
| updated_at | 2025-09-07 22:48:20.130365+00 |
| description | Asynchronous data preloader library |
| homepage | |
| repository | https://github.com/dleowns1102/preloader-rs.git |
| max_upload_size | |
| id | 1723805 |
| size | 52,463 |
A high-performance asynchronous data preloader library for Rust that provides efficient caching and concurrent access patterns.
Future traittry_get()take()Add this to your Cargo.toml:
[dependencies]
preloader = "0.1.3"
tokio = { version = "1.0", features = ["full"] }
thiserror = "1.0"
use preloader::Preloader;
use tokio;
#[tokio::main]
async fn main() {
let preloader = Preloader::new();
// Start loading data asynchronously
preloader.load(async {
// Simulate expensive operation (network request, file I/O, etc.)
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
"expensive data".to_string()
}).await;
// Block until data is ready
match preloader.get().await {
Ok(data) => println!("Data loaded: {}", data),
Err(e) => println!("Error: {}", e),
}
// Non-blocking access (returns immediately if ready)
match preloader.try_get() {
Ok(data) => println!("Immediate access: {}", data),
Err(e) => println!("Not ready yet: {}", e),
}
}
use preloader::Preloader;
use std::sync::Arc;
use tokio;
#[tokio::main]
async fn main() {
let preloader = Arc::new(Preloader::new());
// Start loading
preloader.load(async {
// Simulate database query
tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;
vec![1, 2, 3, 4, 5]
}).await;
// Multiple concurrent consumers
let mut handles = vec![];
for i in 0..5 {
let preloader = Arc::clone(&preloader);
handles.push(tokio::spawn(async move {
let data = preloader.get().await.unwrap();
println!("Consumer {} got: {:?}", i, data);
}));
}
// Wait for all consumers
for handle in handles {
handle.await.unwrap();
}
}
Preloader<T>The main preloader struct that handles asynchronous data loading and caching.
new() -> Preloader<T> - Create a new preloader instanceload(future: impl Future<Output = T> + Send + 'static) -> () - Start loading data asynchronouslyget() -> Result<&T, PreloaderError> - Get data (blocks until ready)try_get() -> Result<&T, PreloaderError> - Try to get data (non-blocking)take(self) -> Result<T, PreloaderError> - Take ownership of data, consuming the preloader (blocks until ready)is_loaded() -> bool - Check if data is loaded and ready for immediate accessget_unchecked() -> &T - Get data without checks (unsafe, panics if not ready)try_get_unchecked() -> &T - Try to get data without checks (unsafe, panics if not ready)#[derive(Debug, thiserror::Error)]
pub enum PreloaderError {
#[error("Preloader is not loaded")]
NotLoaded,
#[error("Preloader is loading")]
Loading,
}
type Result<T> = std::result::Result<T, PreloaderError>;
Send + Sync implementationtake() to consume data for transformation pipelinesuse preloader::Preloader;
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
struct Config {
database_url: String,
api_key: String,
port: u16,
}
async fn load_config() -> Config {
let preloader = Preloader::new();
preloader.load(async {
// Load from file or environment
let content = tokio::fs::read_to_string("config.json").await.unwrap();
serde_json::from_str(&content).unwrap()
}).await;
preloader.get().await.unwrap().clone()
}
use preloader::Preloader;
use sqlx::PgPool;
async fn create_connection_pool() -> PgPool {
let preloader = Preloader::new();
preloader.load(async {
PgPool::connect("postgresql://user:pass@localhost/db").await.unwrap()
}).await;
preloader.get().await.unwrap().clone()
}
use preloader::Preloader;
use tokio;
#[tokio::main]
async fn main() {
let preloader = Preloader::new();
// Start loading data asynchronously
preloader.load(async {
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
"data to consume".to_string()
}).await;
// Take ownership of the data, consuming the preloader itself
match preloader.take().await {
Ok(data) => {
println!("Consumed data: {}", data);
// Do something with owned data
let modified = data + " - modified";
println!("Modified: {}", modified);
},
Err(e) => println!("Error: {}", e),
}
// Note: preloader is consumed and cannot be used after take()
// The following code would not compile:
// let result = preloader.try_get(); // Error: use of moved value
}
use preloader::Preloader;
use std::sync::Arc;
struct AppState {
config: Arc<Preloader<Config>>,
}
impl AppState {
fn new() -> Self {
Self {
config: Arc::new(Preloader::new()),
}
}
// Safe method for general use
async fn get_config(&self) -> Result<&Config, PreloaderError> {
self.config.get().await
}
// High-performance method for hot paths
fn get_config_fast(&self) -> &Config {
// Only use when you're certain the config is loaded
unsafe { self.config.get_unchecked() }
}
}
use preloader::Preloader;
use std::sync::Arc;
struct AppState {
config: Arc<Preloader<Config>>,
cache: Arc<Preloader<Cache>>,
}
impl AppState {
fn new() -> Self {
Self {
config: Arc::new(Preloader::new()),
cache: Arc::new(Preloader::new()),
}
}
async fn get_config(&self) -> &Config {
// Load config only when first accessed
if self.config.try_get().is_err() {
self.config.load(async {
// Load configuration logic
Config::load_from_env().await
}).await;
}
self.config.get().await.unwrap()
}
}
The preloader follows a clear state machine:
load() is first calledload() is called again (ignored)The Preloader is designed for concurrent access:
get() and try_get() callsload() call is processedget() - Always safe, blocks until data is readytry_get() - Always safe, returns error if not readyget_unchecked() - Unsafe: Panics if data is not loadedtry_get_unchecked() - Unsafe: Panics if data is not loadedUse unsafe methods only when you are absolutely certain the data is loaded and ready.
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
git clone https://github.com/yourusername/preloader.git
cd preloader
cargo test
cargo doc --open
# Run all tests
cargo test
# Run tests with output
cargo test -- --nocapture
# Run specific test
cargo test test_concurrent_access
This project is licensed under the MIT License - see the LICENSE file for details.
Made with ❤️ in Rust