diff options
Diffstat (limited to 'util')
-rw-r--r-- | util/Cargo.toml | 2 | ||||
-rw-r--r-- | util/src/queue.rs | 17 |
2 files changed, 11 insertions, 8 deletions
diff --git a/util/Cargo.toml b/util/Cargo.toml index 9a6558b..5f32580 100644 --- a/util/Cargo.toml +++ b/util/Cargo.toml @@ -2,6 +2,7 @@ name = "kittybox-util" version = "0.2.0" edition = "2021" +rust-version = "1.75.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -14,7 +15,6 @@ http = ["dep:http"] [dependencies] serde = { version = "^1.0.170", features = ["derive"] } serde_json = "^1.0.64" -async-trait = "^0.1.50" futures-util = "^0.3.14" uuid = "^1.3.3" url = "2.5.2" diff --git a/util/src/queue.rs b/util/src/queue.rs index c880597..edbec86 100644 --- a/util/src/queue.rs +++ b/util/src/queue.rs @@ -1,8 +1,11 @@ +use std::future::Future; use futures_util::Stream; use std::pin::Pin; use uuid::Uuid; -#[async_trait::async_trait] +/// A stream of jobs from the job queue. +pub type JobStream<J, E> = Pin<Box<dyn Stream<Item = Result<J, E>> + Send>>; + /// A job queue that can store and return jobs. pub trait JobQueue<T: JobItem>: Send + Sync + Sized + Clone + 'static { /// A type of job object that will be returned by the queue. @@ -17,13 +20,14 @@ pub trait JobQueue<T: JobItem>: Send + Sync + Sized + Clone + 'static { /// /// Returns an error if a job queue failed in some way. Having no /// items is not a failure, in which case `Ok(None)` is returned. - async fn get_one(&self) -> Result<Option<Self::Job>, Self::Error>; + #[must_use = "Undone jobs get put back into the queue."] + fn get_one(&self) -> impl Future<Output = Result<Option<Self::Job>, Self::Error>> + Send; /// Put an item into a job queue, returning its UUID. - async fn put(&self, item: &T) -> Result<Uuid, Self::Error>; + fn put(&self, item: &T) -> impl Future<Output = Result<Uuid, Self::Error>> + Send; /* /// Check the amount of pending and stuck items in the job queue. - async fn len(&self) -> Result<(usize, usize), Self::Error>; + fn len(&self) -> impl Future<Output = Result<(usize, usize), Self::Error>> + Send; /// Returns whether the job queue has some pending items. async fn is_empty(&self) -> Result<bool, Self::Error> { Ok(self.len().await?.0 == 0) @@ -40,10 +44,9 @@ pub trait JobQueue<T: JobItem>: Send + Sync + Sized + Clone + 'static { /// /// Note that one item may be returned several times if it is not /// marked as done. - async fn into_stream(self) -> Result<Pin<Box<dyn Stream<Item = Result<Self::Job, Self::Error>> + Send>>, Self::Error>; + fn into_stream(self) -> impl Future<Output = Result<JobStream<Self::Job, Self::Error>, Self::Error>> + Send; } -#[async_trait::async_trait] /// A job description yielded from a job queue. /// /// # Implementors @@ -58,7 +61,7 @@ pub trait Job<T: JobItem, Q: JobQueue<T>>: Send + Sync + Sized { /// Get the object describing the task itself. fn job(&self) -> &T; /// Mark the job as done and remove it from the job queue. - async fn done(self) -> Result<(), Q::Error>; + fn done(self) -> impl Future<Output = Result<(), Q::Error>> + Send; } /// An object describing the job itself, returned as part of a |