Skip to content

Commit c01a97b

Browse files
committed
path2: Remove .with_display_str and friends
Rewrite these methods as methods on Display and FilenameDisplay. This turns do path.with_display_str |s| { ... } into do path.display().with_str |s| { ... }
1 parent d6d9b92 commit c01a97b

File tree

12 files changed

+132
-127
lines changed

12 files changed

+132
-127
lines changed

src/compiletest/runtest.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ fn check_error_patterns(props: &TestProps,
334334
testfile: &Path,
335335
ProcRes: &ProcRes) {
336336
if props.error_patterns.is_empty() {
337-
do testfile.with_display_str |s| {
337+
do testfile.display().with_str |s| {
338338
fatal(~"no error pattern specified in " + s);
339339
}
340340
}

src/librustdoc/html/render.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ impl<'self> SourceCollector<'self> {
454454
let w = cur.open_writer(io::CreateOrTruncate);
455455
let mut w = BufferedWriter::new(w);
456456

457-
let title = cur.with_filename_display_str(|s| format!("{} -- source", s.unwrap()));
457+
let title = cur.filename_display().with_str(|s| format!("{} -- source", s));
458458
let page = layout::Page {
459459
title: title,
460460
ty: "source",

src/librustpkg/installed_packages.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub fn list_installed_packages(f: &fn(&PkgId) -> bool) -> bool {
4040
let rel_p = lib.path_relative_from(&parent).unwrap();
4141
debug2!("Rel: {}", rel_p.display());
4242
let rel_path = rel_p.join(basename);
43-
do rel_path.with_display_str |s| {
43+
do rel_path.display().with_str |s| {
4444
debug2!("Rel name: {}", s);
4545
f(&PkgId::new(s));
4646
}

src/librustpkg/package_id.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl PkgId {
8888

8989
pub fn hash(&self) -> ~str {
9090
// FIXME (#9639): hash should take a &[u8] so we can hash the real path
91-
do self.path.with_display_str |s| {
91+
do self.path.display().with_str |s| {
9292
let vers = self.version.to_str();
9393
format!("{}-{}-{}", s, hash(s + vers), vers)
9494
}

src/librustpkg/package_source.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl PkgSrc {
114114

115115
}
116116

117-
debug2!("Checking dirs: {:?}", to_try.map(|p| p.to_display_str()).connect(":"));
117+
debug2!("Checking dirs: {:?}", to_try.map(|p| p.display().to_str()).connect(":"));
118118

119119
let path = to_try.iter().find(|&d| os::path_exists(d));
120120

src/librustpkg/rustpkg.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ impl CtxMethods for BuildContext {
337337
"list" => {
338338
io::println("Installed packages:");
339339
do installed_packages::list_installed_packages |pkg_id| {
340-
do pkg_id.path.with_display_str |s| {
340+
do pkg_id.path.display().with_str |s| {
341341
println(s);
342342
}
343343
true
@@ -564,7 +564,7 @@ impl CtxMethods for BuildContext {
564564
&pkg_src.destination_workspace,
565565
&id).map(|s| Path::new(*s));
566566
debug2!("install: id = {}, about to call discover_outputs, {:?}",
567-
id.to_str(), result.map(|p| p.to_display_str()));
567+
id.to_str(), result.map(|p| p.display().to_str()));
568568
installed_files = installed_files + result;
569569
note(format!("Installed package {} to {}",
570570
id.to_str(),

src/librustpkg/workspace.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub fn each_pkg_parent_workspace(cx: &Context, pkgid: &PkgId, action: &fn(&Path)
2727
fail2!("Package {} not found in any of \
2828
the following workspaces: {}",
2929
pkgid.path.display(),
30-
rust_path().map(|p| p.to_display_str()).to_str());
30+
rust_path().map(|p| p.display().to_str()).to_str());
3131
}
3232
for ws in workspaces.iter() {
3333
if action(ws) {

src/libstd/io.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ use ptr;
6565
use result::{Result, Ok, Err};
6666
use str::{StrSlice, OwnedStr};
6767
use str;
68+
use to_str::ToStr;
6869
use uint;
6970
use vec::{MutableVector, ImmutableVector, OwnedVector, OwnedCopyableVector, CopyableVector};
7071
use vec;
@@ -1068,7 +1069,7 @@ pub fn file_reader(path: &Path) -> Result<@Reader, ~str> {
10681069
};
10691070

10701071
if f as uint == 0u {
1071-
do path.with_display_str |p| {
1072+
do path.display().with_str |p| {
10721073
Err(~"error opening " + p)
10731074
}
10741075
} else {
@@ -1753,7 +1754,7 @@ pub fn read_whole_file_str(file: &Path) -> Result<~str, ~str> {
17531754
if str::is_utf8(bytes) {
17541755
Ok(str::from_utf8(bytes))
17551756
} else {
1756-
Err(file.to_display_str() + " is not UTF-8")
1757+
Err(file.display().to_str() + " is not UTF-8")
17571758
}
17581759
}
17591760
}

src/libstd/path/mod.rs

Lines changed: 63 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ use iter::Iterator;
7575
use option::{Option, None, Some};
7676
use str;
7777
use str::{OwnedStr, Str, StrSlice};
78+
use to_str::ToStr;
7879
use vec;
7980
use vec::{CopyableVector, OwnedCopyableVector, OwnedVector, Vector};
8081
use vec::{ImmutableEqVector, ImmutableVector};
@@ -190,59 +191,6 @@ pub trait GenericPath: Clone + GenericPathUnsafe {
190191
/// Converts the Path into an owned byte vector
191192
fn into_vec(self) -> ~[u8];
192193

193-
/// Provides the path as a string
194-
///
195-
/// If the path is not UTF-8, invalid sequences will be replaced with the unicode
196-
/// replacement char. This involves allocation.
197-
#[inline]
198-
fn with_display_str<T>(&self, f: &fn(&str) -> T) -> T {
199-
match self.as_str() {
200-
Some(s) => f(s),
201-
None => {
202-
let s = self.to_display_str();
203-
f(s.as_slice())
204-
}
205-
}
206-
}
207-
208-
/// Returns the path as a string
209-
///
210-
/// If the path is not UTF-8, invalid sequences will be replaced with the unicode
211-
/// replacement char. This involves allocation.
212-
///
213-
/// This is similar to `with_display_str()` except it will always allocate a new ~str.
214-
fn to_display_str(&self) -> ~str {
215-
from_utf8_with_replacement(self.as_vec())
216-
}
217-
218-
/// Provides the filename as a string
219-
///
220-
/// If the filename is not UTF-8, invalid sequences will be replaced with the unicode
221-
/// replacement char. This involves allocation.
222-
#[inline]
223-
fn with_filename_display_str<T>(&self, f: &fn(Option<&str>) -> T) -> T {
224-
match self.filename_str() {
225-
s@Some(_) => f(s),
226-
None => {
227-
let o = self.to_filename_display_str();
228-
f(o.map(|s|s.as_slice()))
229-
}
230-
}
231-
}
232-
233-
/// Returns the filename as a string
234-
///
235-
/// If the filename is not UTF-8, invalid sequences will be replaced with the unicode
236-
/// replacement char. This involves allocation.
237-
///
238-
/// This is similar to `to_filename_display_str` except it will always allocate a new ~str.
239-
fn to_filename_display_str(&self) -> Option<~str> {
240-
match self.filename() {
241-
None => None,
242-
Some(v) => Some(from_utf8_with_replacement(v))
243-
}
244-
}
245-
246194
/// Returns an object that implements `fmt::Default` for printing paths
247195
///
248196
/// This will print the equivalent of `to_display_str()` when used with a {} format parameter.
@@ -764,16 +712,75 @@ pub struct FilenameDisplay<'self, P> {
764712

765713
impl<'self, P: GenericPath> fmt::Default for Display<'self, P> {
766714
fn fmt(d: &Display<P>, f: &mut fmt::Formatter) {
767-
do d.path.with_display_str |s| {
715+
do d.with_str |s| {
768716
f.pad(s)
769717
}
770718
}
771719
}
772720

721+
impl<'self, P: GenericPath> ToStr for Display<'self, P> {
722+
/// Returns the path as a string
723+
///
724+
/// If the path is not UTF-8, invalid sequences with be replaced with the
725+
/// unicode replacement char. This involves allocation.
726+
fn to_str(&self) -> ~str {
727+
from_utf8_with_replacement(self.path.as_vec())
728+
}
729+
}
730+
731+
impl<'self, P: GenericPath> Display<'self, P> {
732+
/// Provides the path as a string to a closure
733+
///
734+
/// If the path is not UTF-8, invalid sequences will be replaced with the
735+
/// unicode replacement char. This involves allocation.
736+
#[inline]
737+
pub fn with_str<T>(&self, f: &fn(&str) -> T) -> T {
738+
match self.path.as_str() {
739+
Some(s) => f(s),
740+
None => {
741+
let s = self.to_str();
742+
f(s.as_slice())
743+
}
744+
}
745+
}
746+
}
747+
773748
impl<'self, P: GenericPath> fmt::Default for FilenameDisplay<'self, P> {
774749
fn fmt(d: &FilenameDisplay<P>, f: &mut fmt::Formatter) {
775-
do d.path.with_filename_display_str |s| {
776-
f.pad(s.unwrap_or(""))
750+
do d.with_str |s| {
751+
f.pad(s)
752+
}
753+
}
754+
}
755+
756+
impl<'self, P: GenericPath> ToStr for FilenameDisplay<'self, P> {
757+
/// Returns the filename as a string. If there is no filename, ~"" will be
758+
/// returned.
759+
///
760+
/// If the filename is not UTF-8, invalid sequences will be replaced with
761+
/// the unicode replacement char. This involves allocation.
762+
fn to_str(&self) -> ~str {
763+
match self.path.filename() {
764+
None => ~"",
765+
Some(v) => from_utf8_with_replacement(v)
766+
}
767+
}
768+
}
769+
770+
impl<'self, P: GenericPath> FilenameDisplay<'self, P> {
771+
/// Provides the filename as a string to a closure. If there is no
772+
/// filename, "" will be provided.
773+
///
774+
/// If the filename is not UTF-8, invalid sequences will be replaced with
775+
/// the unicode replacement char. This involves allocation.
776+
#[inline]
777+
pub fn with_str<T>(&self, f: &fn(&str) -> T) -> T {
778+
match self.path.filename_str() {
779+
Some(s) => f(s),
780+
None => {
781+
let s = self.to_str();
782+
f(s.as_slice())
783+
}
777784
}
778785
}
779786
}

src/libstd/path/posix.rs

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -745,50 +745,53 @@ mod tests {
745745
746746
#[test]
747747
fn test_display_str() {
748-
assert_eq!(Path::new("foo").to_display_str(), ~"foo");
749-
assert_eq!(Path::new(b!("foo", 0x80)).to_display_str(), ~"foo\uFFFD");
750-
assert_eq!(Path::new(b!("foo", 0xff, "bar")).to_display_str(), ~"foo\uFFFDbar");
751-
assert_eq!(Path::new(b!("foo", 0xff, "/bar")).to_filename_display_str(), Some(~"bar"));
752-
assert_eq!(Path::new(b!("foo/", 0xff, "bar")).to_filename_display_str(),
753-
Some(~"\uFFFDbar"));
754-
assert_eq!(Path::new(b!("/")).to_filename_display_str(), None);
755-
756-
let mut called = false;
757-
do Path::new("foo").with_display_str |s| {
758-
assert_eq!(s, "foo");
759-
called = true;
760-
};
761-
assert!(called);
762-
called = false;
763-
do Path::new(b!("foo", 0x80)).with_display_str |s| {
764-
assert_eq!(s, "foo\uFFFD");
765-
called = true;
766-
};
767-
assert!(called);
768-
called = false;
769-
do Path::new(b!("foo", 0xff, "bar")).with_display_str |s| {
770-
assert_eq!(s, "foo\uFFFDbar");
771-
called = true;
772-
};
773-
assert!(called);
774-
called = false;
775-
do Path::new(b!("foo", 0xff, "/bar")).with_filename_display_str |s| {
776-
assert_eq!(s, Some("bar"));
777-
called = true;
778-
}
779-
assert!(called);
780-
called = false;
781-
do Path::new(b!("foo/", 0xff, "bar")).with_filename_display_str |s| {
782-
assert_eq!(s, Some("\uFFFDbar"));
783-
called = true;
784-
}
785-
assert!(called);
786-
called = false;
787-
do Path::new(b!("/")).with_filename_display_str |s| {
788-
assert!(s.is_none());
789-
called = true;
790-
}
791-
assert!(called);
748+
macro_rules! t(
749+
($path:expr, $disp:ident, $exp:expr) => (
750+
{
751+
let path = Path::new($path);
752+
assert_eq!(path.$disp().to_str(), ~$exp);
753+
}
754+
)
755+
)
756+
t!("foo", display, "foo");
757+
t!(b!("foo", 0x80), display, "foo\uFFFD");
758+
t!(b!("foo", 0xff, "bar"), display, "foo\uFFFDbar");
759+
t!(b!("foo", 0xff, "/bar"), filename_display, "bar");
760+
t!(b!("foo/", 0xff, "bar"), filename_display, "\uFFFDbar");
761+
t!(b!("/"), filename_display, "");
762+
763+
macro_rules! t(
764+
($path:expr, $exp:expr) => (
765+
{
766+
let mut called = false;
767+
let path = Path::new($path);
768+
do path.display().with_str |s| {
769+
assert_eq!(s, $exp);
770+
called = true;
771+
};
772+
assert!(called);
773+
}
774+
);
775+
($path:expr, $exp:expr, filename) => (
776+
{
777+
let mut called = false;
778+
let path = Path::new($path);
779+
do path.filename_display().with_str |s| {
780+
assert_eq!(s, $exp);
781+
called = true;
782+
783+
};
784+
assert!(called);
785+
}
786+
)
787+
)
788+
789+
t!("foo", "foo");
790+
t!(b!("foo", 0x80), "foo\uFFFD");
791+
t!(b!("foo", 0xff, "bar"), "foo\uFFFDbar");
792+
t!(b!("foo", 0xff, "/bar"), "bar", filename);
793+
t!(b!("foo/", 0xff, "bar"), "\uFFFDbar", filename);
794+
t!(b!("/"), "", filename);
792795
}
793796

794797
#[test]

src/libstd/path/windows.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -350,16 +350,6 @@ impl GenericPath for Path {
350350
self.repr.into_bytes()
351351
}
352352

353-
#[inline]
354-
fn with_display_str<T>(&self, f: &fn(&str) -> T) -> T {
355-
f(self.repr.as_slice())
356-
}
357-
358-
#[inline]
359-
fn to_display_str(&self) -> ~str {
360-
self.repr.clone()
361-
}
362-
363353
#[inline]
364354
fn dirname<'a>(&'a self) -> &'a [u8] {
365355
self.dirname_str().unwrap().as_bytes()
@@ -1462,18 +1452,22 @@ mod tests {
14621452
14631453
#[test]
14641454
fn test_display_str() {
1465-
assert_eq!(Path::new("foo").to_display_str(), ~"foo");
1466-
assert_eq!(Path::new(b!("\\")).to_filename_display_str(), None);
1455+
let path = Path::new("foo");
1456+
assert_eq!(path.display().to_str(), ~"foo");
1457+
let path = Path::new(b!("\\"));
1458+
assert_eq!(path.filename_display().to_str(), ~"");
14671459

14681460
let mut called = false;
1469-
do Path::new("foo").with_display_str |s| {
1461+
let path = Path::new("foo");
1462+
do path.display().with_str |s| {
14701463
assert_eq!(s, "foo");
14711464
called = true;
14721465
};
14731466
assert!(called);
14741467
called = false;
1475-
do Path::new(b!("\\")).with_filename_display_str |s| {
1476-
assert!(s.is_none());
1468+
let path = Path::new(b!("\\"));
1469+
do path.filename_display().with_str |s| {
1470+
assert_eq!(s, "");
14771471
called = true;
14781472
}
14791473
assert!(called);

0 commit comments

Comments
 (0)