Skip to main content

linux_media/
version.rs

1use std::fmt;
2
3use serde::{Deserialize, Serialize};
4
5/// Version information wrapper formatted with `KERNEL_VERSION` macro.
6#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Serialize, Deserialize)]
7pub struct Version {
8    pub major: u8,
9    pub minor: u8,
10    pub patch: u8,
11}
12
13impl From<u32> for Version {
14    /// Convert to u32.
15    ///
16    /// 17..24th bits, 9..16th bits, 0..8th bits represents major, minor and patch version respectively.
17    fn from(ver: u32) -> Self {
18        let major = ((ver & (0xFF << 16)) >> 16) as u8;
19        let minor = ((ver & (0xFF << 8)) >> 8) as u8;
20        let patch = ((ver & (0xFF << 0)) >> 0) as u8;
21        Self {
22            major,
23            minor,
24            patch,
25        }
26    }
27}
28
29impl Into<u32> for Version {
30    fn into(self: Version) -> u32 {
31        let Self {
32            major,
33            minor,
34            patch,
35        } = self;
36        ((major as u32) << 16) | ((minor as u32) << 8) | (patch as u32)
37    }
38}
39
40impl fmt::Display for Version {
41    /// Version is formatted to "{major}.{minor}.{patch}".
42    /// Where the each component is formatted as decimal number.
43    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
44        write!(f, "{}.{}.{}", self.major, self.minor, self.patch)
45    }
46}
47
48impl Version {
49    pub fn new(major: u8, minor: u8, patch: u8) -> Self {
50        Self {
51            major,
52            minor,
53            patch,
54        }
55    }
56}