Remove all ToStr impls, add Show impls
This commit changes the ToStr trait to: impl<T: fmt::Show> ToStr for T { fn to_str(&self) -> ~str { format!("{}", *self) } } The ToStr trait has been on the chopping block for quite awhile now, and this is the final nail in its coffin. The trait and the corresponding method are not being removed as part of this commit, but rather any implementations of the `ToStr` trait are being forbidden because of the generic impl. The new way to get the `to_str()` method to work is to implement `fmt::Show`. Formatting into a `&mut Writer` (as `format!` does) is much more efficient than `ToStr` when building up large strings. The `ToStr` trait forces many intermediate allocations to be made while the `fmt::Show` trait allows incremental buildup in the same heap allocated buffer. Additionally, the `fmt::Show` trait is much more extensible in terms of interoperation with other `Writer` instances and in more situations. By design the `ToStr` trait requires at least one allocation whereas the `fmt::Show` trait does not require any allocations. Closes #8242 Closes #9806
This commit is contained in:
parent
7cc6b5e0a3
commit
b78b749810
55 changed files with 414 additions and 607 deletions
|
@ -10,7 +10,7 @@
|
|||
|
||||
//! Operations on ASCII strings and characters
|
||||
|
||||
use to_str::{ToStr, IntoStr};
|
||||
use to_str::{IntoStr};
|
||||
use str;
|
||||
use str::Str;
|
||||
use str::StrSlice;
|
||||
|
@ -127,14 +127,6 @@ impl Ascii {
|
|||
}
|
||||
}
|
||||
|
||||
impl ToStr for Ascii {
|
||||
#[inline]
|
||||
fn to_str(&self) -> ~str {
|
||||
// self.chr is always a valid utf8 byte, no need for the check
|
||||
unsafe { str::raw::from_byte(self.chr) }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> fmt::Show for Ascii {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
(self.chr as char).fmt(f)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue