Skip to main content

wslplugins_rs/
wsl_version.rs

1use std::{
2    fmt::{self, Debug, Display},
3    hash::Hash,
4    ptr,
5};
6
7/// Represents a WSL version number.
8///
9/// This struct wraps the `WSLVersion` from the WSL Plugin API and provides
10/// safe, idiomatic Rust access to its fields.
11/// # Example
12/// ```
13/// use wslplugins_rs::WSLVersion;
14/// let version = WSLVersion::new(2, 0, 0);
15/// assert_eq!(version.major(), 2);
16/// assert_eq!(version.minor(), 0);
17/// assert_eq!(version.revision(), 0);
18/// ```
19#[repr(transparent)]
20#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
21pub struct WSLVersion(wslpluginapi_sys::WSLVersion);
22
23impl WSLVersion {
24    /// Creates a new `WSLVersion` instance.
25    /// # Parameters
26    /// - `major`: The major version number.
27    /// - `minor`: The minor version number.
28    /// - `revision`: The revision number.
29    /// # Returns
30    /// The new `WSLVersion` instance.
31    #[must_use]
32    #[inline]
33    pub const fn new(major: u32, minor: u32, revision: u32) -> Self {
34        Self(wslpluginapi_sys::WSLVersion {
35            Major: major,
36            Minor: minor,
37            Revision: revision,
38        })
39    }
40
41    /// Retrieves the major version number.
42    #[must_use]
43    #[inline]
44    pub const fn major(&self) -> u32 {
45        self.0.Major
46    }
47
48    /// Set the major version number.
49    #[inline]
50    pub const fn set_major(&mut self, major: u32) {
51        self.0.Major = major;
52    }
53
54    /// Retrieves the minor version number.
55    #[must_use]
56    #[inline]
57    pub const fn minor(&self) -> u32 {
58        self.0.Minor
59    }
60
61    /// Set the minor version number.
62    #[inline]
63    pub const fn set_minor(&mut self, minor: u32) {
64        self.0.Minor = minor;
65    }
66
67    /// Retrieves the revision version number.
68    #[must_use]
69    #[inline]
70    pub const fn revision(&self) -> u32 {
71        self.0.Revision
72    }
73
74    /// Set the revision version number.
75    #[inline]
76    pub const fn set_revision(&mut self, revision: u32) {
77        self.0.Revision = revision;
78    }
79}
80
81impl From<wslpluginapi_sys::WSLVersion> for WSLVersion {
82    #[inline]
83    fn from(value: wslpluginapi_sys::WSLVersion) -> Self {
84        Self(value)
85    }
86}
87
88impl From<WSLVersion> for wslpluginapi_sys::WSLVersion {
89    #[inline]
90    fn from(value: WSLVersion) -> Self {
91        value.0
92    }
93}
94
95impl AsRef<WSLVersion> for wslpluginapi_sys::WSLVersion {
96    #[inline]
97    fn as_ref(&self) -> &WSLVersion {
98        // SAFETY: conveting this kind of ref is safe as it is transparent
99        unsafe { &*ptr::from_ref(self).cast::<WSLVersion>() }
100    }
101}
102
103impl AsRef<wslpluginapi_sys::WSLVersion> for WSLVersion {
104    #[inline]
105    fn as_ref(&self) -> &wslpluginapi_sys::WSLVersion {
106        &self.0
107    }
108}
109
110impl Default for WSLVersion {
111    #[inline]
112    fn default() -> Self {
113        Self::new(1, 0, 0)
114    }
115}
116
117impl Display for WSLVersion {
118    #[inline]
119    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
120        write!(f, "{}.{}.{}", self.major(), self.minor(), self.revision())
121    }
122}
123
124impl Debug for WSLVersion {
125    #[inline]
126    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
127        f.debug_struct(stringify!(WSLVersion))
128            .field("major", &self.major())
129            .field("minor", &self.minor())
130            .field("revision", &self.revision())
131            .finish()
132    }
133}