1
Fork 0

core: convert ToStr::to_str to take explicit &self

This commit is contained in:
Erick Tryzelaar 2013-02-03 20:47:26 -08:00
parent 9556629da2
commit 9adfa59d8e
26 changed files with 88 additions and 72 deletions

View file

@ -439,7 +439,7 @@ pub pure fn to_str_digits(num: f32, dig: uint) -> ~str {
impl f32: to_str::ToStr { impl f32: to_str::ToStr {
#[inline(always)] #[inline(always)]
pure fn to_str() -> ~str { to_str_digits(self, 8) } pure fn to_str(&self) -> ~str { to_str_digits(*self, 8) }
} }
impl f32: num::ToStrRadix { impl f32: num::ToStrRadix {

View file

@ -463,7 +463,7 @@ pub pure fn to_str_digits(num: f64, dig: uint) -> ~str {
impl f64: to_str::ToStr { impl f64: to_str::ToStr {
#[inline(always)] #[inline(always)]
pure fn to_str() -> ~str { to_str_digits(self, 8) } pure fn to_str(&self) -> ~str { to_str_digits(*self, 8) }
} }
impl f64: num::ToStrRadix { impl f64: num::ToStrRadix {

View file

@ -206,7 +206,7 @@ pub pure fn to_str_digits(num: float, digits: uint) -> ~str {
impl float: to_str::ToStr { impl float: to_str::ToStr {
#[inline(always)] #[inline(always)]
pure fn to_str() -> ~str { to_str_digits(self, 8) } pure fn to_str(&self) -> ~str { to_str_digits(*self, 8) }
} }
impl float: num::ToStrRadix { impl float: num::ToStrRadix {

View file

@ -287,8 +287,8 @@ pub pure fn str(i: T) -> ~str { to_str(i) }
impl T : ToStr { impl T : ToStr {
#[inline(always)] #[inline(always)]
pure fn to_str() -> ~str { pure fn to_str(&self) -> ~str {
to_str(self) to_str(*self)
} }
} }

View file

@ -249,8 +249,8 @@ pub pure fn str(i: T) -> ~str { to_str(i) }
impl T : ToStr { impl T : ToStr {
#[inline(always)] #[inline(always)]
pure fn to_str() -> ~str { pure fn to_str(&self) -> ~str {
to_str(self) to_str(*self)
} }
} }

View file

@ -368,7 +368,7 @@ impl Path {
} }
impl PosixPath : ToStr { impl PosixPath : ToStr {
pure fn to_str() -> ~str { pure fn to_str(&self) -> ~str {
let mut s = ~""; let mut s = ~"";
if self.is_absolute { if self.is_absolute {
s += "/"; s += "/";
@ -531,7 +531,7 @@ impl PosixPath : GenericPath {
impl WindowsPath : ToStr { impl WindowsPath : ToStr {
pure fn to_str() -> ~str { pure fn to_str(&self) -> ~str {
let mut s = ~""; let mut s = ~"";
match self.host { match self.host {
Some(ref h) => { s += "\\\\"; s += *h; } Some(ref h) => { s += "\\\\"; s += *h; }

View file

@ -22,52 +22,68 @@ use kinds::Copy;
use str; use str;
use vec; use vec;
pub trait ToStr { pub pure fn to_str() -> ~str; } pub trait ToStr {
pure fn to_str(&self) -> ~str;
}
impl bool: ToStr { impl bool: ToStr {
#[inline(always)] #[inline(always)]
pure fn to_str() -> ~str { ::bool::to_str(self) } pure fn to_str(&self) -> ~str { ::bool::to_str(*self) }
} }
impl (): ToStr { impl (): ToStr {
#[inline(always)] #[inline(always)]
pure fn to_str() -> ~str { ~"()" } pure fn to_str(&self) -> ~str { ~"()" }
} }
impl ~str: ToStr { impl ~str: ToStr {
#[inline(always)] #[inline(always)]
pure fn to_str() -> ~str { copy self } pure fn to_str(&self) -> ~str { copy *self }
} }
impl &str: ToStr { impl &str: ToStr {
#[inline(always)] #[inline(always)]
pure fn to_str() -> ~str { ::str::from_slice(self) } pure fn to_str(&self) -> ~str { ::str::from_slice(*self) }
} }
impl @str: ToStr { impl @str: ToStr {
#[inline(always)] #[inline(always)]
pure fn to_str() -> ~str { ::str::from_slice(self) } pure fn to_str(&self) -> ~str { ::str::from_slice(*self) }
} }
impl<A: ToStr Copy, B: ToStr Copy> (A, B): ToStr { impl<A: ToStr, B: ToStr> (A, B): ToStr {
#[inline(always)] #[inline(always)]
pure fn to_str() -> ~str { pure fn to_str(&self) -> ~str {
let (a, b) = self; // FIXME(#4760): this causes an llvm assertion
//let &(ref a, ref b) = self;
match *self {
(ref a, ref b) => {
~"(" + a.to_str() + ~", " + b.to_str() + ~")" ~"(" + a.to_str() + ~", " + b.to_str() + ~")"
} }
}
}
} }
impl<A: ToStr Copy, B: ToStr Copy, C: ToStr Copy> (A, B, C): ToStr { impl<A: ToStr, B: ToStr, C: ToStr> (A, B, C): ToStr {
#[inline(always)] #[inline(always)]
pure fn to_str() -> ~str { pure fn to_str(&self) -> ~str {
let (a, b, c) = self; // FIXME(#4760): this causes an llvm assertion
~"(" + a.to_str() + ~", " + b.to_str() + ~", " + c.to_str() + ~")" //let &(ref a, ref b, ref c) = self;
match *self {
(ref a, ref b, ref c) => {
fmt!("(%s, %s, %s)",
(*a).to_str(),
(*b).to_str(),
(*c).to_str()
)
}
}
} }
} }
impl<A: ToStr> ~[A]: ToStr { impl<A: ToStr> ~[A]: ToStr {
#[inline(always)] #[inline(always)]
pure fn to_str() -> ~str { pure fn to_str(&self) -> ~str {
unsafe { unsafe {
// Bleh -- not really unsafe // Bleh -- not really unsafe
// push_str and push_char // push_str and push_char
let mut acc = ~"[", first = true; let mut acc = ~"[", first = true;
for vec::each(self) |elt| { for self.each |elt| {
unsafe { unsafe {
if first { first = false; } if first { first = false; }
else { str::push_str(&mut acc, ~", "); } else { str::push_str(&mut acc, ~", "); }
@ -82,11 +98,11 @@ impl<A: ToStr> ~[A]: ToStr {
impl<A: ToStr> @A: ToStr { impl<A: ToStr> @A: ToStr {
#[inline(always)] #[inline(always)]
pure fn to_str() -> ~str { ~"@" + (*self).to_str() } pure fn to_str(&self) -> ~str { ~"@" + (**self).to_str() }
} }
impl<A: ToStr> ~A: ToStr { impl<A: ToStr> ~A: ToStr {
#[inline(always)] #[inline(always)]
pure fn to_str() -> ~str { ~"~" + (*self).to_str() } pure fn to_str(&self) -> ~str { ~"~" + (**self).to_str() }
} }
#[cfg(test)] #[cfg(test)]

View file

@ -222,11 +222,11 @@ pub fn check_crate(tcx: ty::ctxt,
} }
impl LiveNode: to_str::ToStr { impl LiveNode: to_str::ToStr {
pure fn to_str() -> ~str { fmt!("ln(%u)", *self) } pure fn to_str(&self) -> ~str { fmt!("ln(%u)", **self) }
} }
impl Variable: to_str::ToStr { impl Variable: to_str::ToStr {
pure fn to_str() -> ~str { fmt!("v(%u)", *self) } pure fn to_str(&self) -> ~str { fmt!("v(%u)", **self) }
} }
// ______________________________________________________________________ // ______________________________________________________________________

View file

@ -121,8 +121,8 @@ pub struct EnvValue {
} }
pub impl EnvAction { pub impl EnvAction {
fn to_str() -> ~str { fn to_str(&self) -> ~str {
match self { match *self {
EnvCopy => ~"EnvCopy", EnvCopy => ~"EnvCopy",
EnvMove => ~"EnvMove", EnvMove => ~"EnvMove",
EnvRef => ~"EnvRef" EnvRef => ~"EnvRef"

View file

@ -720,7 +720,7 @@ pub impl block {
fn ty_to_str(t: ty::t) -> ~str { fn ty_to_str(t: ty::t) -> ~str {
ty_to_str(self.tcx(), t) ty_to_str(self.tcx(), t)
} }
fn to_str() -> ~str { fn to_str(&self) -> ~str {
match self.node_info { match self.node_info {
Some(node_info) => { Some(node_info) => {
fmt!("[block %d]", node_info.id) fmt!("[block %d]", node_info.id)

View file

@ -841,7 +841,7 @@ pub impl DatumBlock {
self.bcx.tcx() self.bcx.tcx()
} }
fn to_str() -> ~str { fn to_str(&self) -> ~str {
self.datum.to_str(self.ccx()) self.datum.to_str(self.ccx())
} }
} }

View file

@ -667,7 +667,7 @@ pub impl TyVid: Vid {
} }
pub impl TyVid: ToStr { pub impl TyVid: ToStr {
pure fn to_str() -> ~str { fmt!("<V%u>", self.to_uint()) } pure fn to_str(&self) -> ~str { fmt!("<V%u>", self.to_uint()) }
} }
pub impl IntVid: Vid { pub impl IntVid: Vid {
@ -675,7 +675,7 @@ pub impl IntVid: Vid {
} }
pub impl IntVid: ToStr { pub impl IntVid: ToStr {
pure fn to_str() -> ~str { fmt!("<VI%u>", self.to_uint()) } pure fn to_str(&self) -> ~str { fmt!("<VI%u>", self.to_uint()) }
} }
pub impl FloatVid: Vid { pub impl FloatVid: Vid {
@ -683,7 +683,7 @@ pub impl FloatVid: Vid {
} }
pub impl FloatVid: ToStr { pub impl FloatVid: ToStr {
pure fn to_str() -> ~str { fmt!("<VF%u>", self.to_uint()) } pure fn to_str(&self) -> ~str { fmt!("<VF%u>", self.to_uint()) }
} }
pub impl RegionVid: Vid { pub impl RegionVid: Vid {
@ -691,19 +691,19 @@ pub impl RegionVid: Vid {
} }
pub impl RegionVid: ToStr { pub impl RegionVid: ToStr {
pure fn to_str() -> ~str { fmt!("%?", self) } pure fn to_str(&self) -> ~str { fmt!("%?", self) }
} }
pub impl FnSig : ToStr { pub impl FnSig : ToStr {
pure fn to_str() -> ~str { pure fn to_str(&self) -> ~str {
// grr, without tcx not much we can do. // grr, without tcx not much we can do.
return ~"(...)"; return ~"(...)";
} }
} }
pub impl InferTy: ToStr { pub impl InferTy: ToStr {
pure fn to_str() -> ~str { pure fn to_str(&self) -> ~str {
match self { match *self {
TyVar(ref v) => v.to_str(), TyVar(ref v) => v.to_str(),
IntVar(ref v) => v.to_str(), IntVar(ref v) => v.to_str(),
FloatVar(ref v) => v.to_str() FloatVar(ref v) => v.to_str()
@ -712,8 +712,8 @@ pub impl InferTy: ToStr {
} }
pub impl IntVarValue : ToStr { pub impl IntVarValue : ToStr {
pure fn to_str() -> ~str { pure fn to_str(&self) -> ~str {
match self { match *self {
IntType(ref v) => v.to_str(), IntType(ref v) => v.to_str(),
UintType(ref v) => v.to_str(), UintType(ref v) => v.to_str(),
} }

View file

@ -88,7 +88,7 @@ impl BigUint : Ord {
} }
impl BigUint : ToStr { impl BigUint : ToStr {
pure fn to_str() -> ~str { self.to_str_radix(10) } pure fn to_str(&self) -> ~str { self.to_str_radix(10) }
} }
impl BigUint : from_str::FromStr { impl BigUint : from_str::FromStr {
@ -605,7 +605,7 @@ impl BigInt : Ord {
} }
impl BigInt : ToStr { impl BigInt : ToStr {
pure fn to_str() -> ~str { self.to_str_radix(10) } pure fn to_str(&self) -> ~str { self.to_str_radix(10) }
} }
impl BigInt : from_str::FromStr { impl BigInt : from_str::FromStr {

View file

@ -474,7 +474,7 @@ impl Bitv {
* The resulting string has the same length as `self`, and each * The resulting string has the same length as `self`, and each
* character is either '0' or '1'. * character is either '0' or '1'.
*/ */
fn to_str() -> ~str { fn to_str(&self) -> ~str {
let mut rs = ~""; let mut rs = ~"";
for self.each() |i| { if i { rs += ~"1"; } else { rs += ~"0"; } }; for self.each() |i| { if i { rs += ~"1"; } else { rs += ~"0"; } };
rs rs

View file

@ -1172,11 +1172,11 @@ impl <A: ToJson> Option<A>: ToJson {
} }
impl Json: to_str::ToStr { impl Json: to_str::ToStr {
pure fn to_str() -> ~str { to_str(&self) } pure fn to_str(&self) -> ~str { to_str(self) }
} }
impl Error: to_str::ToStr { impl Error: to_str::ToStr {
pure fn to_str() -> ~str { pure fn to_str(&self) -> ~str {
fmt!("%u:%u: %s", self.line, self.col, *self.msg) fmt!("%u:%u: %s", self.line, self.col, *self.msg)
} }
} }

View file

@ -718,8 +718,8 @@ pub pure fn to_str(url: &Url) -> ~str {
} }
impl Url: to_str::ToStr { impl Url: to_str::ToStr {
pub pure fn to_str() -> ~str { pub pure fn to_str(&self) -> ~str {
to_str(&self) to_str(self)
} }
} }

View file

@ -353,7 +353,7 @@ pub mod chained {
} }
impl<K:Eq IterBytes Hash Copy ToStr, V: ToStr Copy> T<K, V>: ToStr { impl<K:Eq IterBytes Hash Copy ToStr, V: ToStr Copy> T<K, V>: ToStr {
pure fn to_str() -> ~str { pure fn to_str(&self) -> ~str {
unsafe { unsafe {
// Meh -- this should be safe // Meh -- this should be safe
do io::with_str_writer |wr| { self.to_writer(wr) } do io::with_str_writer |wr| { self.to_writer(wr) }

View file

@ -923,8 +923,8 @@ pub enum trait_method {
pub enum int_ty { ty_i, ty_char, ty_i8, ty_i16, ty_i32, ty_i64, } pub enum int_ty { ty_i, ty_char, ty_i8, ty_i16, ty_i32, ty_i64, }
pub impl int_ty : ToStr { pub impl int_ty : ToStr {
pure fn to_str() -> ~str { pure fn to_str(&self) -> ~str {
::ast_util::int_ty_to_str(self) ::ast_util::int_ty_to_str(*self)
} }
} }
@ -959,8 +959,8 @@ pub impl int_ty : cmp::Eq {
pub enum uint_ty { ty_u, ty_u8, ty_u16, ty_u32, ty_u64, } pub enum uint_ty { ty_u, ty_u8, ty_u16, ty_u32, ty_u64, }
pub impl uint_ty : ToStr { pub impl uint_ty : ToStr {
pure fn to_str() -> ~str { pure fn to_str(&self) -> ~str {
::ast_util::uint_ty_to_str(self) ::ast_util::uint_ty_to_str(*self)
} }
} }
@ -993,8 +993,8 @@ pub impl uint_ty : cmp::Eq {
pub enum float_ty { ty_f, ty_f32, ty_f64, } pub enum float_ty { ty_f, ty_f32, ty_f64, }
pub impl float_ty : ToStr { pub impl float_ty : ToStr {
pure fn to_str() -> ~str { pure fn to_str(&self) -> ~str {
::ast_util::float_ty_to_str(self) ::ast_util::float_ty_to_str(*self)
} }
} }
@ -1096,8 +1096,8 @@ pub enum Onceness {
} }
pub impl Onceness : ToStr { pub impl Onceness : ToStr {
pure fn to_str() -> ~str { pure fn to_str(&self) -> ~str {
match self { match *self {
Once => ~"once", Once => ~"once",
Many => ~"many" Many => ~"many"
} }
@ -1188,8 +1188,8 @@ pub enum purity {
} }
pub impl purity : ToStr { pub impl purity : ToStr {
pure fn to_str() -> ~str { pure fn to_str(&self) -> ~str {
match self { match *self {
impure_fn => ~"impure", impure_fn => ~"impure",
unsafe_fn => ~"unsafe", unsafe_fn => ~"unsafe",
pure_fn => ~"pure", pure_fn => ~"pure",

View file

@ -34,8 +34,8 @@ pub impl direction : cmp::Eq {
} }
pub impl direction: ToStr { pub impl direction: ToStr {
pure fn to_str() -> ~str { pure fn to_str(&self) -> ~str {
match self { match *self {
send => ~"Send", send => ~"Send",
recv => ~"Recv" recv => ~"Recv"
} }

View file

@ -18,7 +18,7 @@ pub mod kitty {
} }
pub impl cat : ToStr { pub impl cat : ToStr {
pure fn to_str() -> ~str { copy self.name } pure fn to_str(&self) -> ~str { copy self.name }
} }
priv impl cat { priv impl cat {

View file

@ -20,7 +20,7 @@ impl Point : ToStr { //~ ERROR implements a method not defined in the trait
Point { x: x, y: y } Point { x: x, y: y }
} }
pure fn to_str() -> ~str { pure fn to_str(&self) -> ~str {
fmt!("(%f, %f)", self.x, self.y) fmt!("(%f, %f)", self.x, self.y)
} }
} }

View file

@ -14,5 +14,5 @@ struct S {
impl S: Cmp, ToStr { //~ ERROR: expected `{` but found `,` impl S: Cmp, ToStr { //~ ERROR: expected `{` but found `,`
fn eq(&&other: S) { false } fn eq(&&other: S) { false }
fn to_str() -> ~str { ~"hi" } fn to_str(&self) -> ~str { ~"hi" }
} }

View file

@ -54,7 +54,7 @@ fn cat(in_x : uint, in_y : int, in_name: ~str) -> cat {
} }
impl cat: ToStr { impl cat: ToStr {
pure fn to_str() -> ~str { copy self.name } pure fn to_str(&self) -> ~str { copy self.name }
} }
fn print_out<T: ToStr>(thing: T, expected: ~str) { fn print_out<T: ToStr>(thing: T, expected: ~str) {

View file

@ -28,8 +28,8 @@ enum square {
} }
impl square: to_str::ToStr { impl square: to_str::ToStr {
pure fn to_str() -> ~str { pure fn to_str(&self) -> ~str {
match self { match *self {
bot => { ~"R" } bot => { ~"R" }
wall => { ~"#" } wall => { ~"#" }
rock => { ~"*" } rock => { ~"*" }

View file

@ -110,7 +110,7 @@ impl AsciiArt
// Note that the %s fmt! specifier will not call this automatically. // Note that the %s fmt! specifier will not call this automatically.
impl AsciiArt : ToStr impl AsciiArt : ToStr
{ {
pure fn to_str() -> ~str pure fn to_str(&self) -> ~str
{ {
// Convert each line into a string. // Convert each line into a string.
let lines = do self.lines.map |line| {str::from_chars(*line)}; let lines = do self.lines.map |line| {str::from_chars(*line)};

View file

@ -4,7 +4,7 @@ struct Thingy {
} }
impl ToStr for Thingy { impl ToStr for Thingy {
pure fn to_str() -> ~str { pure fn to_str(&self) -> ~str {
fmt!("{ x: %d, y: %d }", self.x, self.y) fmt!("{ x: %d, y: %d }", self.x, self.y)
} }
} }
@ -14,7 +14,7 @@ struct PolymorphicThingy<T> {
} }
impl<T:ToStr> ToStr for PolymorphicThingy<T> { impl<T:ToStr> ToStr for PolymorphicThingy<T> {
pure fn to_str() -> ~str { pure fn to_str(&self) -> ~str {
self.x.to_str() self.x.to_str()
} }
} }