MCPcopy Create free account
hub / github.com/cloud-hypervisor/cloud-hypervisor / parse_str

Method parse_str

net_util/src/mac.rs:23–49  ·  view source on GitHub ↗
(s: &S)

Source from the content-addressed store, hash-verified

21
22impl MacAddr {
23 pub fn parse_str<S>(s: &S) -> Result<MacAddr, io::Error>
24 where
25 S: AsRef<str> + ?Sized,
26 {
27 let v: Vec<&str> = s.as_ref().split(':').collect();
28 let mut bytes = [0u8; MAC_ADDR_LEN];
29 let common_err = Err(io::Error::other(format!(
30 "parsing of {} into a MAC address failed",
31 s.as_ref()
32 )));
33
34 if v.len() != MAC_ADDR_LEN {
35 return common_err;
36 }
37
38 for i in 0..MAC_ADDR_LEN {
39 if v[i].len() != 2 {
40 return common_err;
41 }
42 if !v[i].bytes().all(|a| a.is_ascii_hexdigit()) {
43 return common_err;
44 }
45 bytes[i] = u8::from_str_radix(v[i], 16).unwrap();
46 }
47
48 Ok(MacAddr { bytes })
49 }
50
51 // Does not check whether src.len() == MAC_ADDR_LEN.
52 #[inline]

Callers

nothing calls this directly

Calls 2

lenMethod · 0.45
bytesMethod · 0.45

Tested by

no test coverage detected