Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow oneshots to be serialized to json manually #12

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Allow oneshots to be serialized to json manually
Signed-off-by: Sawyer Bergeron <sawyerbergeron@gmail.com>
  • Loading branch information
szbergeron committed Jun 27, 2024
commit 2c3ae00fc3db995c7d4297190bc061c8fd6fefb4
13 changes: 13 additions & 0 deletions src/tascii/src/oneshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,25 @@ impl WeakUntypedOneshotHandle {

pub trait OneShotAny: Send + Sync {
fn as_any(&self) -> &dyn std::any::Any;

fn value_as_json(&self) -> Option<Result<serde_json::Value, TaskError>>;
}

impl<T: TaskSafe + Send + Sync> OneShotAny for OneShot<Result<T, TaskError>> {
fn as_any(&self) -> &dyn std::any::Any {
self
}

fn value_as_json(&self) -> Option<Result<serde_json::Value, TaskError>> {
let v = self.get()?;

let r = match v {
Ok(v) => Ok(serde_json::to_value(v).ok()?),
Err(e) => Err(e), // work around T being different even if E is same
};

Some(r)
}
}

#[derive(Serialize, Deserialize, Clone, Debug)]
Expand Down