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
use {
crate::error::*,
std::str::FromStr,
};
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct DeviceId {
pub major: u32,
pub minor: u32,
}
impl FromStr for DeviceId {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
let parts: Vec<&str> = s.split(':').collect();
match parts.len() {
1 => Ok(parts[0].parse::<u64>()?.into()),
2 => Ok(Self {
major: parts[0].parse()?,
minor: parts[1].parse()?,
}),
_ => Err(Error::UnexpectedFormat),
}
}
}
impl From<u64> for DeviceId {
fn from(num: u64) -> Self {
Self {
major: (num >> 8) as u32,
minor: (num & 0xFF) as u32,
}
}
}
impl DeviceId {
pub fn new(major: u32, minor: u32) -> Self {
Self { major, minor }
}
}
#[test]
fn test_from_str() {
assert_eq!(DeviceId::new(8, 16), DeviceId::from_str("8:16").unwrap());
}
#[test]
fn test_from_u64() {
assert_eq!(DeviceId::new(8, 16), DeviceId::from(2064u64));
}