From 6c1ef4a7a7a9bc371c0ce0ee06aac6412a7159ca Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 1 Aug 2024 15:48:36 +0200 Subject: [PATCH 1/2] Remove `bstr` dependency --- Cargo.toml | 3 --- examples/simple.rs | 5 ++--- src/unix/linux/process.rs | 3 +-- 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cc89cbbaa..0c11112af 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -101,9 +101,6 @@ ntapi = { version = "0.4", optional = true } # versions when bumping to a new release, and only increase the minimum when absolutely necessary. windows = { version = ">=0.54, <=0.57", optional = true } -[target.'cfg(any(windows, target_os = "linux", target_os = "android"))'.dependencies] -bstr = "1.9.0" - [target.'cfg(not(any(target_os = "unknown", target_arch = "wasm32")))'.dependencies] libc = "^0.2.153" diff --git a/examples/simple.rs b/examples/simple.rs index 0ac208db4..5a671833c 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -4,7 +4,6 @@ #![allow(unused_must_use, non_upper_case_globals)] #![allow(clippy::manual_range_contains)] -use bstr::ByteSlice; use std::io::{self, BufRead, Write}; use std::str::FromStr; use sysinfo::{Components, Disks, Networks, Pid, Signal, System, Users}; @@ -243,7 +242,7 @@ fn interpret_input( &mut io::stdout(), "{}:{} status={:?}", pid, - proc_.name().as_encoded_bytes().as_bstr(), + proc_.name().to_string_lossy(), proc_.status() ); } @@ -294,7 +293,7 @@ fn interpret_input( writeln!( &mut io::stdout(), "==== {} ====", - proc_.name().as_encoded_bytes().as_bstr() + proc_.name().to_string_lossy() ); writeln!(&mut io::stdout(), "{proc_:?}"); } diff --git a/src/unix/linux/process.rs b/src/unix/linux/process.rs index 80a97f3ab..e39c6ddb3 100644 --- a/src/unix/linux/process.rs +++ b/src/unix/linux/process.rs @@ -11,7 +11,6 @@ use std::path::{Path, PathBuf}; use std::str::{self, FromStr}; use std::sync::atomic::{AtomicUsize, Ordering}; -use bstr::ByteSlice; use libc::{c_ulong, gid_t, kill, uid_t}; use crate::sys::system::SystemInfo; @@ -815,7 +814,7 @@ fn copy_from_file(entry: &Path) -> Vec { let mut out = Vec::with_capacity(10); let mut data = data.as_slice(); while let Some(pos) = data.iter().position(|c| *c == 0) { - let s = &data[..pos].trim(); + let s = &data[..pos].trim_ascii(); if !s.is_empty() { out.push(OsStr::from_bytes(s).to_os_string()); } From 73db406a181c03f1135aa7ae86266d539a73bcf2 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 1 Aug 2024 15:57:15 +0200 Subject: [PATCH 2/2] Remove usage of `trim_ascii` for now --- src/unix/linux/process.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/unix/linux/process.rs b/src/unix/linux/process.rs index e39c6ddb3..f3438a9e8 100644 --- a/src/unix/linux/process.rs +++ b/src/unix/linux/process.rs @@ -802,6 +802,26 @@ pub(crate) fn refresh_procs( nb_updated.into_inner() } +// FIXME: To be removed once MSRV for this crate is 1.80 nd use the `trim_ascii()` method instead. +fn trim_ascii(mut bytes: &[u8]) -> &[u8] { + // Code from Rust code library. + while let [rest @ .., last] = bytes { + if last.is_ascii_whitespace() { + bytes = rest; + } else { + break; + } + } + while let [first, rest @ ..] = bytes { + if first.is_ascii_whitespace() { + bytes = rest; + } else { + break; + } + } + bytes +} + fn copy_from_file(entry: &Path) -> Vec { match File::open(entry) { Ok(mut f) => { @@ -814,7 +834,7 @@ fn copy_from_file(entry: &Path) -> Vec { let mut out = Vec::with_capacity(10); let mut data = data.as_slice(); while let Some(pos) = data.iter().position(|c| *c == 0) { - let s = &data[..pos].trim_ascii(); + let s = trim_ascii(&data[..pos]); if !s.is_empty() { out.push(OsStr::from_bytes(s).to_os_string()); }