Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 6681694

Browse files
committed
Avoid converting filenames into strings where possible
1 parent 1da5054 commit 6681694

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

src/archive.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub(crate) struct ArArchiveBuilder<'a> {
2929
src_archives: Vec<File>,
3030
// Don't use `HashMap` here, as the order is important. `rust.metadata.bin` must always be at
3131
// the end of an archive for linkers to not get confused.
32-
entries: Vec<(String, ArchiveEntry)>,
32+
entries: Vec<(Vec<u8>, ArchiveEntry)>,
3333
}
3434

3535
impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
@@ -44,7 +44,7 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
4444
for entry in archive.members() {
4545
let entry = entry.unwrap();
4646
entries.push((
47-
String::from_utf8(entry.name().to_vec()).unwrap(),
47+
entry.name().to_vec(),
4848
ArchiveEntry::FromArchive { archive_index: 0, file_range: entry.file_range() },
4949
));
5050
}
@@ -68,21 +68,21 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
6868
}
6969

7070
fn src_files(&mut self) -> Vec<String> {
71-
self.entries.iter().map(|(name, _)| name.clone()).collect()
71+
self.entries.iter().map(|(name, _)| String::from_utf8(name.clone()).unwrap()).collect()
7272
}
7373

7474
fn remove_file(&mut self, name: &str) {
7575
let index = self
7676
.entries
7777
.iter()
78-
.position(|(entry_name, _)| entry_name == name)
78+
.position(|(entry_name, _)| entry_name == name.as_bytes())
7979
.expect("Tried to remove file not existing in src archive");
8080
self.entries.remove(index);
8181
}
8282

8383
fn add_file(&mut self, file: &Path) {
8484
self.entries.push((
85-
file.file_name().unwrap().to_str().unwrap().to_string(),
85+
file.file_name().unwrap().to_str().unwrap().to_string().into_bytes(),
8686
ArchiveEntry::File(file.to_owned()),
8787
));
8888
}
@@ -165,7 +165,7 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
165165
match object::File::parse(&*data) {
166166
Ok(object) => {
167167
symbol_table.insert(
168-
entry_name.as_bytes().to_vec(),
168+
entry_name.to_vec(),
169169
object
170170
.symbols()
171171
.filter_map(|symbol| {
@@ -190,7 +190,8 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
190190
} else {
191191
sess.fatal(&format!(
192192
"error parsing `{}` during archive creation: {}",
193-
entry_name, err
193+
String::from_utf8_lossy(&entry_name),
194+
err
194195
));
195196
}
196197
}
@@ -209,7 +210,7 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
209210
err
210211
));
211212
}),
212-
entries.iter().map(|(name, _)| name.as_bytes().to_vec()).collect(),
213+
entries.iter().map(|(name, _)| name.clone()).collect(),
213214
ar::GnuSymbolTableFormat::Size32,
214215
symbol_table,
215216
)
@@ -232,7 +233,7 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
232233

233234
// Add all files
234235
for (entry_name, data) in entries.into_iter() {
235-
let header = ar::Header::new(entry_name.into_bytes(), data.len() as u64);
236+
let header = ar::Header::new(entry_name, data.len() as u64);
236237
match builder {
237238
BuilderKind::Bsd(ref mut builder) => builder.append(&header, &mut &*data).unwrap(),
238239
BuilderKind::Gnu(ref mut builder) => builder.append(&header, &mut &*data).unwrap(),
@@ -282,7 +283,7 @@ impl<'a> ArArchiveBuilder<'a> {
282283
.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?;
283284
if !skip(&file_name) {
284285
self.entries.push((
285-
file_name,
286+
file_name.into_bytes(),
286287
ArchiveEntry::FromArchive { archive_index, file_range: entry.file_range() },
287288
));
288289
}

0 commit comments

Comments
 (0)