Skip to content

Tweak from_utf8_lossy to return a new MaybeOwned enum #12098

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 8, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/compiletest/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,9 +442,7 @@ fn check_error_patterns(props: &TestProps,
testfile: &Path,
ProcRes: &ProcRes) {
if props.error_patterns.is_empty() {
testfile.display().with_str(|s| {
fatal(~"no error pattern specified in " + s);
})
fatal(~"no error pattern specified in " + testfile.display().as_maybe_owned().as_slice());
}

if ProcRes.status.success() {
Expand Down
3 changes: 1 addition & 2 deletions src/etc/vim/syntax/rust.vim
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ syn keyword rustTrait Primitive Int Float ToStrRadix ToPrimitive FromPrimitive
syn keyword rustTrait GenericPath Path PosixPath WindowsPath
syn keyword rustTrait RawPtr
syn keyword rustTrait Buffer Writer Reader Seek
syn keyword rustTrait SendStr SendStrOwned SendStrStatic IntoSendStr
syn keyword rustTrait Str StrVector StrSlice OwnedStr
syn keyword rustTrait Str StrVector StrSlice OwnedStr IntoMaybeOwned
syn keyword rustTrait IterBytes
syn keyword rustTrait ToStr IntoStr
syn keyword rustTrait CloneableTuple ImmutableTuple
Expand Down
4 changes: 2 additions & 2 deletions src/libextra/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -893,8 +893,8 @@ pub fn run_test(force_ignore: bool,
spawn(proc() {
let mut task = task::task();
task.name(match desc.name {
DynTestName(ref name) => SendStrOwned(name.clone()),
StaticTestName(name) => SendStrStatic(name),
DynTestName(ref name) => name.to_owned().into_maybe_owned(),
StaticTestName(name) => name.into_maybe_owned()
});
let result_future = task.future_result();
task.spawn(testfn);
Expand Down
2 changes: 1 addition & 1 deletion src/libgreen/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ pub fn run(main: proc()) -> int {
let (port, chan) = Chan::new();
let mut opts = TaskOpts::new();
opts.notify_chan = Some(chan);
opts.name = Some(SendStrStatic("<main>"));
opts.name = Some("<main>".into_maybe_owned());
pool.spawn(opts, main);

// Wait for the main task to return, and set the process error code
Expand Down
2 changes: 1 addition & 1 deletion src/libgreen/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ mod tests {
#[test]
fn smoke_opts() {
let mut opts = TaskOpts::new();
opts.name = Some(SendStrStatic("test"));
opts.name = Some("test".into_maybe_owned());
opts.stack_size = Some(20 * 4096);
let (p, c) = Chan::new();
opts.notify_chan = Some(c);
Expand Down
2 changes: 1 addition & 1 deletion src/libnative/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ mod tests {
#[test]
fn smoke_opts() {
let mut opts = TaskOpts::new();
opts.name = Some(SendStrStatic("test"));
opts.name = Some("test".into_maybe_owned());
opts.stack_size = Some(20 * 4096);
let (p, c) = Chan::new();
opts.notify_chan = Some(c);
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ impl<'a> SourceCollector<'a> {
cur.push(p.filename().expect("source has no filename") + bytes!(".html"));
let mut w = BufferedWriter::new(if_ok!(File::create(&cur)));

let title = cur.filename_display().with_str(|s| format!("{} -- source", s));
let title = format!("{} -- source", cur.filename_display());
let page = layout::Page {
title: title,
ty: "source",
Expand Down
1 change: 0 additions & 1 deletion src/libstd/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ pub mod vec_ng;
pub mod str;

pub mod ascii;
pub mod send_str;

pub mod ptr;
pub mod owned;
Expand Down
49 changes: 29 additions & 20 deletions src/libstd/path/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ use fmt;
use iter::Iterator;
use option::{Option, None, Some};
use str;
use str::{OwnedStr, Str, StrSlice};
use str::{MaybeOwned, OwnedStr, Str, StrSlice, from_utf8_lossy};
use to_str::ToStr;
use vec;
use vec::{CloneableVector, OwnedCloneableVector, OwnedVector, Vector};
Expand Down Expand Up @@ -495,7 +495,7 @@ pub struct Display<'a, P> {

impl<'a, P: GenericPath> fmt::Show for Display<'a, P> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.with_str(|s| f.pad(s))
self.as_maybe_owned().as_slice().fmt(f)
}
}

Expand All @@ -505,33 +505,25 @@ impl<'a, P: GenericPath> ToStr for Display<'a, P> {
/// If the path is not UTF-8, invalid sequences with be replaced with the
/// unicode replacement char. This involves allocation.
fn to_str(&self) -> ~str {
if self.filename {
match self.path.filename() {
None => ~"",
Some(v) => str::from_utf8_lossy(v)
}
} else {
str::from_utf8_lossy(self.path.as_vec())
}
self.as_maybe_owned().into_owned()
}
}

impl<'a, P: GenericPath> Display<'a, P> {
/// Provides the path as a string to a closure
/// Returns the path as a possibly-owned string.
///
/// If the path is not UTF-8, invalid sequences will be replaced with the
/// unicode replacement char. This involves allocation.
#[inline]
pub fn with_str<T>(&self, f: |&str| -> T) -> T {
let opt = if self.filename { self.path.filename_str() }
else { self.path.as_str() };
match opt {
Some(s) => f(s),
None => {
let s = self.to_str();
f(s.as_slice())
pub fn as_maybe_owned(&self) -> MaybeOwned<'a> {
from_utf8_lossy(if self.filename {
match self.path.filename() {
None => &[],
Some(v) => v
}
}
} else {
self.path.as_vec()
})
}
}

Expand Down Expand Up @@ -591,6 +583,23 @@ impl BytesContainer for CString {
}
}

impl<'a> BytesContainer for str::MaybeOwned<'a> {
#[inline]
fn container_as_bytes<'b>(&'b self) -> &'b [u8] {
self.as_slice().as_bytes()
}
#[inline]
fn container_into_owned_bytes(self) -> ~[u8] {
self.into_owned().into_bytes()
}
#[inline]
fn container_as_str<'b>(&'b self) -> Option<&'b str> {
Some(self.as_slice())
}
#[inline]
fn is_str(_: Option<str::MaybeOwned>) -> bool { true }
}

#[inline(always)]
fn contains_nul(v: &[u8]) -> bool {
v.iter().any(|&x| x == 0)
Expand Down
16 changes: 4 additions & 12 deletions src/libstd/path/posix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,24 +564,16 @@ mod tests {
macro_rules! t(
($path:expr, $exp:expr) => (
{
let mut called = false;
let path = Path::new($path);
path.display().with_str(|s| {
assert_eq!(s, $exp);
called = true;
});
assert!(called);
let mo = path.display().as_maybe_owned();
assert_eq!(mo.as_slice(), $exp);
}
);
($path:expr, $exp:expr, filename) => (
{
let mut called = false;
let path = Path::new($path);
path.filename_display().with_str(|s| {
assert_eq!(s, $exp);
called = true;
});
assert!(called);
let mo = path.filename_display().as_maybe_owned();
assert_eq!(mo.as_slice(), $exp);
}
)
)
Expand Down
16 changes: 4 additions & 12 deletions src/libstd/path/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1278,20 +1278,12 @@ mod tests {
let path = Path::new(b!("\\"));
assert_eq!(path.filename_display().to_str(), ~"");

let mut called = false;
let path = Path::new("foo");
path.display().with_str(|s| {
assert_eq!(s, "foo");
called = true;
});
assert!(called);
called = false;
let mo = path.display().as_maybe_owned();
assert_eq!(mo.as_slice(), "foo");
let path = Path::new(b!("\\"));
path.filename_display().with_str(|s| {
assert_eq!(s, "");
called = true;
});
assert!(called);
let mo = path.filename_display().as_maybe_owned();
assert_eq!(mo.as_slice(), "");
}

#[test]
Expand Down
3 changes: 1 addition & 2 deletions src/libstd/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ pub use num::{Primitive, Int, Float, ToStrRadix, ToPrimitive, FromPrimitive};
pub use path::{GenericPath, Path, PosixPath, WindowsPath};
pub use ptr::RawPtr;
pub use io::{Buffer, Writer, Reader, Seek};
pub use send_str::{SendStr, SendStrOwned, SendStrStatic, IntoSendStr};
pub use str::{Str, StrVector, StrSlice, OwnedStr};
pub use str::{Str, StrVector, StrSlice, OwnedStr, IntoMaybeOwned};
pub use to_bytes::IterBytes;
pub use to_str::{ToStr, IntoStr};
pub use tuple::{CloneableTuple, ImmutableTuple};
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/rt/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use rt::local::Local;
use rt::local_heap::LocalHeap;
use rt::rtio::LocalIo;
use rt::unwind::Unwinder;
use send_str::SendStr;
use str::SendStr;
use sync::arc::UnsafeArc;
use sync::atomics::{AtomicUint, SeqCst};
use task::{TaskResult, TaskOpts};
Expand Down
Loading