Skip to content

Commit 78daad3

Browse files
committed
Cleanup unnecessary usage of OsString
1 parent dbaf684 commit 78daad3

File tree

1 file changed

+14
-26
lines changed

1 file changed

+14
-26
lines changed

src/symbolize/gimli/elf.rs

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#![allow(clippy::useless_conversion)]
22

3-
use super::mystd::ffi::{OsStr, OsString};
3+
use super::mystd::ffi::OsStr;
44
use super::mystd::fs;
5-
use super::mystd::os::unix::ffi::{OsStrExt, OsStringExt};
5+
use super::mystd::os::unix::ffi::OsStrExt;
66
use super::mystd::path::{Path, PathBuf};
77
use super::Either;
88
use super::{gimli, Context, Endian, EndianSlice, Mapping, Stash};
9+
use alloc::str::String;
910
use alloc::sync::Arc;
1011
use alloc::vec::Vec;
1112
use core::convert::{TryFrom, TryInto};
@@ -433,25 +434,17 @@ fn locate_build_id(build_id: &[u8]) -> Option<PathBuf> {
433434
}
434435

435436
let mut path =
436-
Vec::with_capacity(BUILD_ID_PATH.len() + BUILD_ID_SUFFIX.len() + build_id.len() * 2 + 1);
437+
String::with_capacity(BUILD_ID_PATH.len() + BUILD_ID_SUFFIX.len() + build_id.len() * 2 + 1);
437438
path.extend(BUILD_ID_PATH);
438-
path.push(hex(build_id[0] >> 4));
439-
path.push(hex(build_id[0] & 0xf));
440-
path.push(b'/');
439+
path.push(char::from_digit((build_id[0] >> 4) as u32, 16)?);
440+
path.push(char::from_digit((build_id[0] & 0xf) as u32, 16)?);
441+
path.push('/');
441442
for byte in &build_id[1..] {
442-
path.push(hex(byte >> 4));
443-
path.push(hex(byte & 0xf));
443+
path.push(char::from_digit((byte >> 4) as u32, 16)?);
444+
path.push(char::from_digit((byte & 0xf) as u32, 16)?);
444445
}
445446
path.extend(BUILD_ID_SUFFIX);
446-
Some(PathBuf::from(OsString::from_vec(path)))
447-
}
448-
449-
fn hex(byte: u8) -> u8 {
450-
if byte < 10 {
451-
b'0' + byte
452-
} else {
453-
b'a' + byte - 10
454-
}
447+
Some(PathBuf::from(path))
455448
}
456449

457450
/// Locate a file specified in a `.gnu_debuglink` section.
@@ -468,9 +461,8 @@ fn hex(byte: u8) -> u8 {
468461
fn locate_debuglink(path: &Path, filename: &[u8]) -> Option<PathBuf> {
469462
let path = fs::canonicalize(path).ok()?;
470463
let parent = path.parent()?;
471-
let mut f = PathBuf::from(OsString::with_capacity(
472-
DEBUG_PATH.len() + parent.as_os_str().len() + filename.len() + 2,
473-
));
464+
let mut f =
465+
PathBuf::with_capacity(DEBUG_PATH.len() + parent.as_os_str().len() + filename.len() + 2);
474466
let filename = Path::new(OsStr::from_bytes(filename));
475467

476468
// Try "/parent/filename" if it differs from "path"
@@ -481,9 +473,7 @@ fn locate_debuglink(path: &Path, filename: &[u8]) -> Option<PathBuf> {
481473
}
482474

483475
// Try "/parent/.debug/filename"
484-
let mut s = OsString::from(f);
485-
s.clear();
486-
f = PathBuf::from(s);
476+
f.clear();
487477
f.push(parent);
488478
f.push(".debug");
489479
f.push(filename);
@@ -493,9 +483,7 @@ fn locate_debuglink(path: &Path, filename: &[u8]) -> Option<PathBuf> {
493483

494484
if debug_path_exists() {
495485
// Try "/usr/lib/debug/parent/filename"
496-
let mut s = OsString::from(f);
497-
s.clear();
498-
f = PathBuf::from(s);
486+
f.clear();
499487
f.push(OsStr::from_bytes(DEBUG_PATH));
500488
f.push(parent.strip_prefix("/").unwrap());
501489
f.push(filename);

0 commit comments

Comments
 (0)