generated from opensound-org/template-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add
thread::ThreadId
& task::TaskId
- Loading branch information
Showing
4 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |