aboutsummaryrefslogtreecommitdiff
path: root/src/protocol/common/thread_id.rs
blob: 36a3ea905b20d75a4f5e235f1932295801aed6d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use core::convert::{TryFrom, TryInto};
use core::num::NonZeroUsize;

use crate::protocol::common::hex::decode_hex;

/// Tid/Pid Selector.
#[derive(PartialEq, Eq, Debug, Clone, Copy)]
pub enum IdKind {
    /// All threads (-1)
    All,
    /// Any thread (0)
    Any,
    /// Thread with specific ID (id > 0)
    WithId(NonZeroUsize),
}

/// Unique Thread ID.
#[derive(PartialEq, Eq, Debug, Clone, Copy)]
pub struct ThreadId {
    /// Process ID (may or may not be present).
    pub pid: Option<IdKind>,
    /// Thread ID.
    pub tid: IdKind,
}

impl TryFrom<&[u8]> for ThreadId {
    type Error = ();

    fn try_from(s: &[u8]) -> Result<Self, ()> {
        match s {
            [b'p', s @ ..] => {
                // p<pid>.<tid>
                let mut s = s.split(|b| *b == b'.');
                let pid: IdKind = s.next().ok_or(())?.try_into()?;
                let tid: IdKind = match s.next() {
                    Some(s) => s.try_into()?,
                    None => IdKind::All, // sending only p<pid> is valid
                };

                Ok(ThreadId {
                    pid: Some(pid),
                    tid,
                })
            }
            _ => {
                // <tid>
                let tid: IdKind = s.try_into()?;

                Ok(ThreadId { pid: None, tid })
            }
        }
    }
}

impl TryFrom<&[u8]> for IdKind {
    type Error = ();

    fn try_from(s: &[u8]) -> Result<Self, ()> {
        Ok(match s {
            b"-1" => IdKind::All,
            b"0" => IdKind::Any,
            id => IdKind::WithId(NonZeroUsize::new(decode_hex(id).map_err(drop)?).ok_or(())?),
        })
    }
}

impl TryFrom<&mut [u8]> for ThreadId {
    type Error = ();

    fn try_from(s: &mut [u8]) -> Result<Self, ()> {
        Self::try_from(s as &[u8])
    }
}

impl TryFrom<&mut [u8]> for IdKind {
    type Error = ();

    fn try_from(s: &mut [u8]) -> Result<Self, ()> {
        Self::try_from(s as &[u8])
    }
}

/// Like [`IdKind`], without the `Any` variant. Typically used when working
/// with vCont (i.e: where the `Any` variant wouldn't be valid).
#[derive(PartialEq, Eq, Debug, Clone, Copy)]
pub enum SpecificIdKind {
    /// Thread with specific ID (id > 0)
    WithId(core::num::NonZeroUsize),
    /// All threads (-1)
    All,
}

/// Like [`ThreadId`], without the `Any` variants. Typically used when working
/// with vCont (i.e: where the `Any` variant wouldn't be valid).
#[derive(Debug, Copy, Clone)]
pub struct SpecificThreadId {
    /// Process ID (may or may not be present).
    pub pid: Option<SpecificIdKind>,
    /// Thread ID.
    pub tid: SpecificIdKind,
}

impl TryFrom<IdKind> for SpecificIdKind {
    type Error = ();

    fn try_from(id: IdKind) -> Result<SpecificIdKind, ()> {
        Ok(match id {
            IdKind::All => SpecificIdKind::All,
            IdKind::WithId(id) => SpecificIdKind::WithId(id),
            IdKind::Any => return Err(()),
        })
    }
}

impl TryFrom<ThreadId> for SpecificThreadId {
    type Error = ();

    fn try_from(thread: ThreadId) -> Result<SpecificThreadId, ()> {
        Ok(SpecificThreadId {
            pid: match thread.pid {
                None => None,
                Some(id_kind) => Some(id_kind.try_into()?),
            },
            tid: thread.tid.try_into()?,
        })
    }
}

/// Like [`ThreadId`], without the `Any`, or `All` variants.
#[derive(Debug, Copy, Clone)]
pub struct ConcreteThreadId {
    /// Process ID (may or may not be present).
    pub pid: Option<NonZeroUsize>,
    /// Thread ID.
    pub tid: NonZeroUsize,
}

impl TryFrom<ThreadId> for ConcreteThreadId {
    type Error = ();

    fn try_from(thread: ThreadId) -> Result<ConcreteThreadId, ()> {
        Ok(ConcreteThreadId {
            pid: match thread.pid {
                None => None,
                Some(id_kind) => Some(id_kind.try_into()?),
            },
            tid: thread.tid.try_into()?,
        })
    }
}

impl TryFrom<IdKind> for NonZeroUsize {
    type Error = ();

    fn try_from(value: IdKind) -> Result<NonZeroUsize, ()> {
        match value {
            IdKind::WithId(v) => Ok(v),
            _ => Err(()),
        }
    }
}