24_future_store/
24_future_store.rs1use keket::{
2 database::{AssetDatabase, path::AssetPathStatic},
3 fetch::file::FileAssetFetch,
4 protocol::text::TextAssetProtocol,
5 store::future::FutureAssetStore,
6};
7use std::{error::Error, path::PathBuf};
8
9#[tokio::main]
10async fn main() -> Result<(), Box<dyn Error>> {
11 let mut database = AssetDatabase::default()
13 .with_protocol(TextAssetProtocol)
14 .with_fetch(FileAssetFetch::default().with_root("resources"))
15 .with_store(FutureAssetStore::new(tokio_save_file));
16
17 let _ = tokio::fs::remove_file("./resources/saved2.txt").await;
18
19 let before = database.spawn("text://saved2.txt", ("Abra cadabra!".to_owned(),))?;
21 println!("Before: {}", before.access::<&String>(&database));
22 before.store(&mut database)?;
24
25 while database.is_busy() {
27 database.maintain()?;
28 tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
29 }
30
31 before.delete(&mut database);
33 assert!(!before.does_exists(&database));
34
35 let after = database.ensure("text://saved2.txt")?;
37 println!("After: {}", after.access::<&String>(&database));
38 Ok(())
41}
42
43async fn tokio_save_file(path: AssetPathStatic, bytes: Vec<u8>) -> Result<(), Box<dyn Error>> {
45 let file_path = PathBuf::from("resources").join(path.path());
46
47 tokio::fs::create_dir_all(file_path.parent().unwrap()).await?;
48 tokio::fs::write(&file_path, bytes).await?;
49
50 Ok(())
51}
52