Skip to content

Commit

Permalink
Merge pull request #9 from dtolnay/lossy
Browse files Browse the repository at this point in the history
Eliminate lossy conversions from render and tests
  • Loading branch information
dtolnay committed Apr 17, 2024
2 parents 313372f + 1da7964 commit e885f0f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
36 changes: 18 additions & 18 deletions src/render.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::write::WriteFmt;
use crate::{Flag, LibraryKind, LinkKind};
use std::ffi::OsString;
use std::ffi::{OsStr, OsString};

impl IntoIterator for Flag {
type Item = OsString;
Expand Down Expand Up @@ -28,8 +28,10 @@ impl IntoIterator for Flag {
if kind == LibraryKind::All {
flags.push(OsString::from(path));
} else {
let path = path.to_string_lossy();
flags.push(OsString::from(format!("{}={}", kind, path)));
let mut flag = OsString::new();
write!(flag, "{}=", kind);
flag.push(path);
flags.push(flag);
}
}

Expand Down Expand Up @@ -158,23 +160,15 @@ impl IntoIterator for Flag {
Flag::Extern { name, path } => {
flags.push(OsString::from("--extern"));
if let Some(path) = path {
flags.push(OsString::from(format!(
"{}={}",
name,
path.to_string_lossy(),
)));
flags.push(kv(name, path));
} else {
flags.push(OsString::from(name));
}
}

Flag::ExternLocation { name, location } => {
flags.push(OsString::from("--extern-location"));
flags.push(OsString::from(format!(
"{}={}",
name,
location.to_string_lossy(),
)));
flags.push(kv(name, location));
}

Flag::Sysroot(sysroot) => {
Expand Down Expand Up @@ -204,11 +198,7 @@ impl IntoIterator for Flag {

Flag::RemapPathPrefix { from, to } => {
flags.push(OsString::from("--remap-path-prefix"));
flags.push(OsString::from(format!(
"{}={}",
from.to_string_lossy(),
to.to_string_lossy(),
)));
flags.push(kv(from, to));
}
}

Expand All @@ -218,6 +208,16 @@ impl IntoIterator for Flag {
}
}

fn kv(k: impl AsRef<OsStr>, v: impl AsRef<OsStr>) -> OsString {
let k = k.as_ref();
let v = v.as_ref();
let mut string = OsString::with_capacity(k.len() + 1 + v.len());
string.push(k);
string.push("=");
string.push(v);
string
}

mod iter {
use std::ffi::OsString;
use std::vec;
Expand Down
6 changes: 3 additions & 3 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ fn test(encoded: &str, expected: &[Flag]) {
let next = iterator.next();
assert_eq!(Some(expected), next.as_ref());
for flag in next.unwrap() {
flags.push(flag.to_string_lossy().into_owned());
flags.push(flag);
}
}

assert_eq!(None, iterator.next());

let re_encoded = flags.join("\x1F");
let mut iterator = crate::from_encoded(OsStr::new(&re_encoded));
let re_encoded = flags.join(OsStr::new("\x1F"));
let mut iterator = crate::from_encoded(&re_encoded);

for expected in expected {
assert_eq!(Some(expected), iterator.next().as_ref());
Expand Down
7 changes: 7 additions & 0 deletions src/write.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::ffi::OsString;
use std::fmt;

pub(crate) trait WriteFmt {
Expand All @@ -9,3 +10,9 @@ impl WriteFmt for String {
fmt::Write::write_fmt(self, args).unwrap();
}
}

impl WriteFmt for OsString {
fn write_fmt(&mut self, args: fmt::Arguments) {
fmt::Write::write_fmt(self, args).unwrap();
}
}

0 comments on commit e885f0f

Please sign in to comment.