Skip to content

Commit

Permalink
Merge pull request #1332 from GuillaumeGomez/rm-bstr
Browse files Browse the repository at this point in the history
Remove `bstr` dependency
  • Loading branch information
GuillaumeGomez committed Aug 1, 2024
2 parents 5c40b60 + 73db406 commit 9009bb6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
5 changes: 2 additions & 3 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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()
);
}
Expand Down Expand Up @@ -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_:?}");
}
Expand Down
23 changes: 21 additions & 2 deletions src/unix/linux/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -803,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<OsString> {
match File::open(entry) {
Ok(mut f) => {
Expand All @@ -815,7 +834,7 @@ fn copy_from_file(entry: &Path) -> Vec<OsString> {
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 = trim_ascii(&data[..pos]);
if !s.is_empty() {
out.push(OsStr::from_bytes(s).to_os_string());
}
Expand Down

0 comments on commit 9009bb6

Please sign in to comment.