Skip to content

Commit 1eb20f0

Browse files
authored
Remove TaskWrapper in favor of directly using PhantomData (#21)
* Remove TaskWrapper in favor of directly using PhantomData * Remove Arc
1 parent 5963af6 commit 1eb20f0

File tree

3 files changed

+8
-35
lines changed

3 files changed

+8
-35
lines changed

src/client.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ use serde::Serialize;
22
use serde_json::Value as JsonValue;
33
use sqlx::{Executor, PgPool, Postgres};
44
use std::collections::HashMap;
5+
use std::marker::PhantomData;
56
use std::sync::Arc;
67
use tokio::sync::RwLock;
78
use uuid::Uuid;
89

9-
use crate::task::{Task, TaskRegistry, TaskWrapper};
10+
use crate::task::{Task, TaskRegistry};
1011
use crate::types::{
1112
CancellationPolicy, RetryStrategy, SpawnOptions, SpawnResult, SpawnResultRow, WorkerOptions,
1213
};
@@ -261,10 +262,7 @@ where
261262
/// Register a task type. Required before spawning or processing.
262263
pub async fn register<T: Task<State>>(&self) -> &Self {
263264
let mut registry = self.registry.write().await;
264-
registry.insert(
265-
T::NAME.to_string(),
266-
Arc::new(TaskWrapper::<T, State>::new()),
267-
);
265+
registry.insert(T::NAME.to_string(), &PhantomData::<T>);
268266
self
269267
}
270268

src/task.rs

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use async_trait::async_trait;
22
use serde::{Serialize, de::DeserializeOwned};
33
use serde_json::Value as JsonValue;
4-
use std::sync::Arc;
4+
use std::marker::PhantomData;
55

66
use crate::context::TaskContext;
77
use crate::error::{TaskError, TaskResult};
@@ -110,34 +110,8 @@ where
110110
) -> Result<JsonValue, TaskError>;
111111
}
112112

113-
/// Wrapper that implements ErasedTask for any Task type
114-
pub struct TaskWrapper<T, State>(std::marker::PhantomData<(T, State)>)
115-
where
116-
T: Task<State>,
117-
State: Clone + Send + Sync + 'static;
118-
119-
impl<T, State> TaskWrapper<T, State>
120-
where
121-
T: Task<State>,
122-
State: Clone + Send + Sync + 'static,
123-
{
124-
pub fn new() -> Self {
125-
Self(std::marker::PhantomData)
126-
}
127-
}
128-
129-
impl<T, State> Default for TaskWrapper<T, State>
130-
where
131-
T: Task<State>,
132-
State: Clone + Send + Sync + 'static,
133-
{
134-
fn default() -> Self {
135-
Self::new()
136-
}
137-
}
138-
139113
#[async_trait]
140-
impl<T, State> ErasedTask<State> for TaskWrapper<T, State>
114+
impl<T, State> ErasedTask<State> for PhantomData<T>
141115
where
142116
T: Task<State>,
143117
State: Clone + Send + Sync + 'static,
@@ -159,4 +133,4 @@ where
159133
}
160134

161135
/// Type alias for the task registry
162-
pub type TaskRegistry<State> = std::collections::HashMap<String, Arc<dyn ErasedTask<State>>>;
136+
pub type TaskRegistry<State> = std::collections::HashMap<String, &'static dyn ErasedTask<State>>;

src/worker.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ impl Worker {
261261
// Look up handler
262262
let registry = registry.read().await;
263263
let handler = match registry.get(&task.task_name) {
264-
Some(h) => h.clone(),
264+
Some(h) => *h,
265265
None => {
266266
tracing::error!("Unknown task: {}", task.task_name);
267267
Self::fail_run(
@@ -319,6 +319,7 @@ impl Worker {
319319
deadline = Instant::now();
320320
warn_fired = false;
321321
}
322+
322323
}
323324

324325
_ = sleep_until(warn_at), if !warn_fired => {

0 commit comments

Comments
 (0)