Skip to main content

wslplugins_rs/
session_id.rs

1use crate::WSLSessionInformation;
2#[cfg(feature = "serde")]
3use serde::{Deserialize, Serialize};
4use std::fmt::{self, Debug, Display};
5
6#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
7#[cfg_attr(feature = "serde", serde(transparent))]
8#[derive(Clone, Copy, PartialEq, Eq, Hash)]
9pub struct SessionID(pub u32);
10
11pub trait HasSessionId {
12    fn session_id(&self) -> SessionID;
13}
14
15impl<T: HasSessionId + ?Sized> HasSessionId for &T {
16    #[inline]
17    fn session_id(&self) -> SessionID {
18        (*self).session_id()
19    }
20}
21
22impl From<u32> for SessionID {
23    #[inline]
24    fn from(value: u32) -> Self {
25        Self(value)
26    }
27}
28
29impl From<SessionID> for u32 {
30    #[inline]
31    fn from(value: SessionID) -> Self {
32        value.0
33    }
34}
35
36impl From<&WSLSessionInformation> for SessionID {
37    #[inline]
38    fn from(value: &WSLSessionInformation) -> Self {
39        value.id()
40    }
41}
42
43impl HasSessionId for SessionID {
44    #[inline]
45    fn session_id(&self) -> SessionID {
46        *self
47    }
48}
49
50impl Debug for SessionID {
51    #[inline]
52    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53        Debug::fmt(&self.0, f)
54    }
55}
56
57impl Display for SessionID {
58    #[inline]
59    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60        Display::fmt(&self.0, f)
61    }
62}