Skip to content

Commit 934f424

Browse files
committed
Linux: parse numbers as u64 directly, fixes #126
The test meminfo example was failing because it was being parsed as usize (32 on 32-bit platforms) and then converted to u64 to make a bytesize. Make the parse function generic to use with u64 directly.
1 parent cbd9c16 commit 934f424

1 file changed

Lines changed: 24 additions & 12 deletions

File tree

src/platform/linux.rs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ where
6767
delimited(multispace0, inner, multispace0)
6868
}
6969

70-
/// Parse an unsigned integer out of a string, surrounded by whitespace
71-
fn usize_s(input: &str) -> IResult<&str, usize> {
70+
/// Parse a number out of a string, surrounded by whitespace
71+
fn num<T: str::FromStr>(input: &str) -> IResult<&str, T> {
7272
map_res(
7373
map_res(map(ws(digit1), str::as_bytes), str::from_utf8),
7474
str::FromStr::from_str,
@@ -85,7 +85,7 @@ fn proc_stat_cpu_time(input: &str) -> IResult<&str, CpuTime> {
8585
map(
8686
preceded(
8787
ws(proc_stat_cpu_prefix),
88-
tuple((usize_s, usize_s, usize_s, usize_s, usize_s, usize_s)),
88+
tuple((num, num, num, num, num, num)),
8989
),
9090
|(user, nice, system, idle, iowait, irq)| CpuTime {
9191
user,
@@ -164,8 +164,8 @@ fn cpu_time() -> io::Result<Vec<CpuTime>> {
164164
// Parse a `/proc/meminfo` line into (key, ByteSize)
165165
fn proc_meminfo_line(input: &str) -> IResult<&str, (&str, ByteSize)> {
166166
complete(map(
167-
tuple((take_until(":"), delimited(tag(":"), usize_s, ws(tag("kB"))))),
168-
|(key, value)| (key, ByteSize::kib(value as u64)),
167+
tuple((take_until(":"), delimited(tag(":"), num, ws(tag("kB"))))),
168+
|(key, value)| (key, ByteSize::kib(value)),
169169
))(input)
170170
}
171171

@@ -365,9 +365,9 @@ fn proc_net_sockstat(input: &str) -> IResult<&str, ProcNetSockStat> {
365365
preceded(
366366
not_line_ending,
367367
tuple((
368-
preceded(ws(tag("TCP: inuse")), usize_s),
369-
delimited(ws(tag("orphan")), usize_s, not_line_ending),
370-
preceded(ws(tag("UDP: inuse")), usize_s),
368+
preceded(ws(tag("TCP: inuse")), num),
369+
delimited(ws(tag("orphan")), num, not_line_ending),
370+
preceded(ws(tag("UDP: inuse")), num),
371371
)),
372372
),
373373
|(tcp_in_use, tcp_orphaned, udp_in_use)| ProcNetSockStat {
@@ -403,8 +403,8 @@ struct ProcNetSockStat6 {
403403
fn proc_net_sockstat6(input: &str) -> IResult<&str, ProcNetSockStat6> {
404404
map(
405405
ws(tuple((
406-
preceded(tag("TCP6: inuse"), usize_s),
407-
preceded(tag("UDP6: inuse"), usize_s),
406+
preceded(tag("TCP6: inuse"), num),
407+
preceded(tag("UDP6: inuse"), num),
408408
))),
409409
|(tcp_in_use, udp_in_use)| ProcNetSockStat6 {
410410
tcp_in_use,
@@ -452,8 +452,20 @@ fn stat_mount(mount: ProcMountsData) -> io::Result<Filesystem> {
452452
fn proc_diskstats_line(input: &str) -> IResult<&str, BlockDeviceStats> {
453453
map(
454454
ws(tuple((
455-
usize_s, usize_s, word_s, usize_s, usize_s, usize_s, usize_s, usize_s, usize_s,
456-
usize_s, usize_s, usize_s, usize_s, usize_s,
455+
num::<usize>,
456+
num::<usize>,
457+
word_s,
458+
num,
459+
num,
460+
num,
461+
num,
462+
num,
463+
num,
464+
num,
465+
num,
466+
num,
467+
num,
468+
num,
457469
))),
458470
|(
459471
_major_number,

0 commit comments

Comments
 (0)