Skip to content

Commit

Permalink
bit of code cleanup of async in traits
Browse files Browse the repository at this point in the history
  • Loading branch information
simdimdim committed Dec 9, 2024
1 parent b4af121 commit f3914a1
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 17 deletions.
3 changes: 2 additions & 1 deletion retriever/src/bin/retriever.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ async fn main() -> io::Result<()> {
};
info!("Delay: {}", &args.delay);
info!("Looking for {}", if args.image { "images" } else { "text" });
let mut page: Page = args.url.try_into().unwrap();
let mut page: Page = args.url.into();
page.set_next(sep);
let mut all_imgs = vec![];
if args.novel {
Expand Down Expand Up @@ -135,6 +135,7 @@ pub fn gen_epub_for(pb: PathBuf) {
let mut file = OpenOptions::new()
.create(true)
.write(true)
.truncate(false)
.open(pb.join("book.epub"))
.unwrap();
let mut book = EpubBuilder::new(ZipLibrary::new().unwrap()).unwrap();
Expand Down
47 changes: 34 additions & 13 deletions storage/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,47 @@ pub use self::file::*;
pub struct Location(pub url::Url);

pub trait Backend: Sized {
type Config;
type Config: Identify;
type State = ();
async fn save<T>(&self) -> Result<()>
fn save(
&self, config: Option<Self::Config>,
) -> impl std::future::Future<Output = Result<()>> + Send
where
Self: Save, {
<Self as Save>::save(self).await
Self: Save,
Self: Send,
Self: Sync,
<Self as Backend>::Config: std::marker::Send, {
async move {
<Self as Save>::save(
self,
<<Self as Backend>::Config as Identify>::id(self).as_str(),
config,
)
.await
}
}
async fn load(&mut self, location: url::Url) -> Result<()>
fn load(&mut self, location: url::Url) -> impl std::future::Future<Output = Result<()>> + Send
where
Self: Load<Data = Self>, {
*self = <Self as Load>::load(self, location).await?;
Ok(())
Self: Load<Data = Self>,
Self: Send, {
async {
*self = <Self as Load>::load(self, location).await?;
Ok(())
}
}
}
pub trait Save {
async fn save(&self) -> Result<()>;
fn save<T: Send>(
&self, name: &str, config: T,
) -> impl std::future::Future<Output = Result<()>> + Send;
}
pub trait Load {
pub trait Load: Sized {
type Data;
async fn load(&self, _: url::Url) -> Result<Self::Data>
where
Self: Sized;
fn load(&self, _: url::Url) -> impl std::future::Future<Output = Result<Self::Data>> + Send;
fn load_mut(
&mut self, _: url::Url,
) -> impl std::future::Future<Output = Result<Self::Data>> + Send;
}
pub trait Identify {
fn id<T>(obj: T) -> String;
}
18 changes: 16 additions & 2 deletions storage/src/backend/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,32 @@ pub struct FileStoreConfig {
pub location: PathBuf,
}
impl Save for FileStore {
async fn save(&self) -> Result<()> { todo!() }
async fn save<T>(&self, _name: &str, _config: T) -> Result<()>
where
<Self as Backend>::Config: Identify, {
todo!()
}
}
impl Load for FileStore {
type Data = Vec<u8>;

async fn load(&self, location: url::Url) -> Result<Self::Data> {
tokio::fs::read(location.path()).await
}

async fn load_mut(&mut self, _location: url::Url) -> Result<Self::Data>
where
Self: Sized, {
// *self = tokio::fs::read(location.path()).await?;
// Ok(self)
todo!()
}
}
impl Backend for FileStore {
type Config = FileStoreConfig;
type State = ();
}

// fn config(_: &Self::Config) -> Self { todo!() }
impl Identify for FileStoreConfig {
fn id<T>(_: T) -> String { todo!() }
}
1 change: 0 additions & 1 deletion storage/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![feature(async_fn_in_trait)]
#![feature(associated_type_defaults)]

pub mod backend;
Expand Down

0 comments on commit f3914a1

Please sign in to comment.