From bda05ec4bfb861c9880dd700ab31c1f9bc79e926 Mon Sep 17 00:00:00 2001 From: Blaž Hrastnik Date: Sun, 17 Oct 2021 14:06:52 +0900 Subject: Use a newtype for ThreadId --- helix-dap/src/client.rs | 20 ++++++++++---------- helix-dap/src/types.rs | 33 ++++++++++++++++++++++----------- 2 files changed, 32 insertions(+), 21 deletions(-) (limited to 'helix-dap/src') diff --git a/helix-dap/src/client.rs b/helix-dap/src/client.rs index 62557951..28cc7c90 100644 --- a/helix-dap/src/client.rs +++ b/helix-dap/src/client.rs @@ -1,7 +1,7 @@ use crate::{ transport::{Payload, Request, Transport}, types::*, - Error, Result, + Error, Result, ThreadId, }; use helix_core::syntax::DebuggerQuirks; @@ -30,9 +30,9 @@ pub struct Client { request_counter: AtomicU64, pub caps: Option, // thread_id -> frames - pub stack_frames: HashMap>, - pub thread_states: HashMap, - pub thread_id: Option, + pub stack_frames: HashMap>, + pub thread_states: HashMap, + pub thread_id: Option, /// Currently active frame for the current thread. pub active_frame: Option, pub breakpoints: Vec, @@ -311,7 +311,7 @@ impl Client { self.request::(()).await } - pub async fn continue_thread(&mut self, thread_id: isize) -> Result> { + pub async fn continue_thread(&mut self, thread_id: ThreadId) -> Result> { let args = requests::ContinueArguments { thread_id }; let response = self.request::(args).await?; @@ -320,7 +320,7 @@ impl Client { pub async fn stack_trace( &mut self, - thread_id: isize, + thread_id: ThreadId, ) -> Result<(Vec, Option)> { let args = requests::StackTraceArguments { thread_id, @@ -358,7 +358,7 @@ impl Client { Ok(response.variables) } - pub async fn step_in(&mut self, thread_id: isize) -> Result<()> { + pub async fn step_in(&mut self, thread_id: ThreadId) -> Result<()> { let args = requests::StepInArguments { thread_id, target_id: None, @@ -368,7 +368,7 @@ impl Client { self.request::(args).await } - pub async fn step_out(&mut self, thread_id: isize) -> Result<()> { + pub async fn step_out(&mut self, thread_id: ThreadId) -> Result<()> { let args = requests::StepOutArguments { thread_id, granularity: None, @@ -377,7 +377,7 @@ impl Client { self.request::(args).await } - pub async fn next(&mut self, thread_id: isize) -> Result<()> { + pub async fn next(&mut self, thread_id: ThreadId) -> Result<()> { let args = requests::NextArguments { thread_id, granularity: None, @@ -386,7 +386,7 @@ impl Client { self.request::(args).await } - pub async fn pause(&mut self, thread_id: isize) -> Result<()> { + pub async fn pause(&mut self, thread_id: ThreadId) -> Result<()> { let args = requests::PauseArguments { thread_id }; self.request::(args).await diff --git a/helix-dap/src/types.rs b/helix-dap/src/types.rs index 5430771f..c2becf33 100644 --- a/helix-dap/src/types.rs +++ b/helix-dap/src/types.rs @@ -2,6 +2,17 @@ use serde::{Deserialize, Serialize}; use serde_json::Value; use std::path::PathBuf; +#[derive( + Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize, +)] +pub struct ThreadId(isize); + +impl std::fmt::Display for ThreadId { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.0.fmt(f) + } +} + pub trait Request { type Arguments: serde::de::DeserializeOwned + serde::Serialize; type Result: serde::de::DeserializeOwned + serde::Serialize; @@ -157,7 +168,7 @@ pub struct StackFrame { #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] pub struct Thread { - pub id: isize, + pub id: ThreadId, pub name: String, } @@ -317,7 +328,7 @@ pub mod requests { #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] pub struct ContinueArguments { - pub thread_id: isize, + pub thread_id: ThreadId, } #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] @@ -338,7 +349,7 @@ pub mod requests { #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] pub struct StackTraceArguments { - pub thread_id: isize, + pub thread_id: ThreadId, pub start_frame: Option, pub levels: Option, pub format: Option, @@ -424,7 +435,7 @@ pub mod requests { #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] pub struct StepInArguments { - pub thread_id: isize, + pub thread_id: ThreadId, pub target_id: Option, pub granularity: Option, } @@ -441,7 +452,7 @@ pub mod requests { #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] pub struct StepOutArguments { - pub thread_id: isize, + pub thread_id: ThreadId, pub granularity: Option, } @@ -457,7 +468,7 @@ pub mod requests { #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] pub struct NextArguments { - pub thread_id: isize, + pub thread_id: ThreadId, pub granularity: Option, } @@ -473,7 +484,7 @@ pub mod requests { #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] pub struct PauseArguments { - pub thread_id: isize, + pub thread_id: ThreadId, } #[derive(Debug)] @@ -574,7 +585,7 @@ pub mod events { pub struct Stopped { pub reason: String, pub description: Option, - pub thread_id: Option, + pub thread_id: Option, pub preserve_focus_hint: Option, pub text: Option, pub all_threads_stopped: Option, @@ -584,7 +595,7 @@ pub mod events { #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] pub struct Continued { - pub thread_id: isize, + pub thread_id: ThreadId, pub all_threads_continued: Option, } @@ -604,7 +615,7 @@ pub mod events { #[serde(rename_all = "camelCase")] pub struct Thread { pub reason: String, - pub thread_id: isize, + pub thread_id: ThreadId, } #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] @@ -661,7 +672,7 @@ pub mod events { // #[serde(rename_all = "camelCase")] // pub struct Invalidated { // pub areas: Vec, - // pub thread_id: Option, + // pub thread_id: Option, // pub stack_frame_id: Option, // } -- cgit v1.2.3-70-g09d2