Skip to content

Commit

Permalink
✨ Add thread::ThreadId & task::TaskId
Browse files Browse the repository at this point in the history
  • Loading branch information
czy-29 committed Dec 7, 2024
1 parent a0a812e commit ad49f32
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@ all-features = true

[dependencies]
anyhow = "1.0.94"
derive_more = { version = "1.0.0", features = ["display"] }
indexmap = "2.7.0"
ron = "0.8.1"
serde = { version = "1.0.215", features = ["derive"] }
thiserror = "2.0.4"
tokio = { version = "1.42.0", features = ["rt"] }

[dev-dependencies]
tokio = { version = "1.42.0", features = ["macros"] }

[build-dependencies]
version_check = "0.9.5"
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@
pub mod collections;
/// Extensions to the [`std::result`](https://doc.rust-lang.org/stable/std/result/index.html) module.
pub mod result;
/// Extensions to the [`std::task`](https://doc.rust-lang.org/stable/std/task/index.html) &
/// [`tokio::task`](https://docs.rs/tokio/latest/tokio/task/index.html) module.
pub mod task;
/// Extensions to the [`std::thread`](https://doc.rust-lang.org/stable/std/thread/index.html) module.
pub mod thread;

pub use result::AnyRes;
25 changes: 25 additions & 0 deletions src/task.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use derive_more::Display;
use serde::{Deserialize, Serialize};
use std::num::NonZeroU64;

/// A [`TaskId`](https://docs.rs/tokio/latest/tokio/task/struct.Id.html) that can be `serde`.
#[derive(Debug, Display, Serialize, Deserialize, Copy, Clone, Eq, PartialEq, Hash)]
#[serde(transparent)]
pub struct TaskId(pub NonZeroU64);

impl From<tokio::task::Id> for TaskId {
fn from(value: tokio::task::Id) -> Self {
Self(value.to_string().parse().unwrap())
}
}

#[cfg(test)]
mod tests {
use super::*;

#[tokio::test]
async fn from_tokio_task_id() {
let id = tokio::spawn(async { tokio::task::id() }).await.unwrap();
assert_eq!(id.to_string(), TaskId::from(id).to_string());
}
}
33 changes: 33 additions & 0 deletions src/thread.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use derive_more::Display;
use serde::{Deserialize, Serialize};
use std::num::NonZeroU64;

/// A [`ThreadId`](https://doc.rust-lang.org/stable/std/thread/struct.ThreadId.html) that can be `serde` and `Display`ed
#[derive(Debug, Display, Serialize, Deserialize, Copy, Clone, Eq, PartialEq, Hash)]
#[serde(transparent)]
pub struct ThreadId(pub NonZeroU64);

impl From<std::thread::ThreadId> for ThreadId {
fn from(value: std::thread::ThreadId) -> Self {
#[derive(Deserialize)]
#[serde(rename = "ThreadId")]
struct Inner(NonZeroU64);

Self(ron::from_str::<Inner>(&format!("{:?}", value)).unwrap().0)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn from_std_thread_id() {
let id = std::thread::current().id();
let thread_id = ThreadId::from(id);
let debug = format!("{:?}", id);

assert_eq!(debug, format!("{:?}", thread_id));
assert_eq!(debug, format!("ThreadId({})", thread_id));
}
}

0 comments on commit ad49f32

Please sign in to comment.