summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorBen S <ogham@bsago.me>2014-11-24 02:01:08 +0000
committerBen S <ogham@bsago.me>2014-11-24 02:01:08 +0000
commitd72be30c30c0c57375736154fd6b5293cb04d0d8 (patch)
treea254fdddf168b96dc926f4315a81b07179c1a205 /src
parentc75bbf7f7951dc08dd1ecb71c1fceea79ba70963 (diff)
Reduce unnecessary String allocations
- Remove uses of to_string() on a &str where it wasn't necessary - Use SendStr to reduce allocations further
Diffstat (limited to 'src')
-rw-r--r--src/column.rs4
-rw-r--r--src/file.rs15
-rw-r--r--src/format.rs26
3 files changed, 23 insertions, 22 deletions
diff --git a/src/column.rs b/src/column.rs
index 132497a..0136981 100644
--- a/src/column.rs
+++ b/src/column.rs
@@ -49,8 +49,8 @@ impl Column {
impl Alignment {
pub fn pad_string(&self, string: &String, padding: uint) -> String {
match *self {
- Alignment::Left => string.clone() + " ".to_string().repeat(padding).as_slice(),
- Alignment::Right => " ".to_string().repeat(padding) + string.as_slice(),
+ Alignment::Left => string.clone() + " ".repeat(padding).as_slice(),
+ Alignment::Right => " ".repeat(padding) + string.as_slice(),
}
}
}
diff --git a/src/file.rs b/src/file.rs
index 43a7b06..dc45202 100644
--- a/src/file.rs
+++ b/src/file.rs
@@ -1,5 +1,6 @@
use std::io::{fs, IoResult};
use std::io;
+use std::str::SendStr;
use ansi_term::{Paint, Colour, Plain, Style, Red, Green, Yellow, Blue, Purple, Cyan, Fixed};
@@ -216,14 +217,14 @@ impl<'a> File<'a> {
}
}
- fn type_char(&self) -> String {
+ fn type_char(&self) -> SendStr {
return match self.stat.kind {
- io::TypeFile => ".".to_string(),
- io::TypeDirectory => Blue.paint("d"),
- io::TypeNamedPipe => Yellow.paint("|"),
- io::TypeBlockSpecial => Purple.paint("s"),
- io::TypeSymlink => Cyan.paint("l"),
- io::TypeUnknown => "?".to_string(),
+ io::TypeFile => ".".into_maybe_owned(),
+ io::TypeDirectory => Blue.paint("d").into_maybe_owned(),
+ io::TypeNamedPipe => Yellow.paint("|").into_maybe_owned(),
+ io::TypeBlockSpecial => Purple.paint("s").into_maybe_owned(),
+ io::TypeSymlink => Cyan.paint("l").into_maybe_owned(),
+ io::TypeUnknown => "?".into_maybe_owned(),
}
}
diff --git a/src/format.rs b/src/format.rs
index eb98f4e..cae08e5 100644
--- a/src/format.rs
+++ b/src/format.rs
@@ -6,7 +6,7 @@ static IEC_PREFIXES: &'static [&'static str] = &[
"", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"
];
-fn format_bytes(mut amount: f64, kilo: f64, prefixes: &[&str]) -> (String, String) {
+fn format_bytes<'s>(mut amount: f64, kilo: f64, prefixes: &[&'s str]) -> (String, &'s str) {
let mut prefix = 0;
while amount >= kilo {
amount /= kilo;
@@ -14,67 +14,67 @@ fn format_bytes(mut amount: f64, kilo: f64, prefixes: &[&str]) -> (String, Strin
}
if amount < 10.0 && prefix != 0 {
- (format!("{:.1}", amount), prefixes[prefix].to_string())
+ (format!("{:.1}", amount), prefixes[prefix])
}
else {
- (format!("{:.0}", amount), prefixes[prefix].to_string())
+ (format!("{:.0}", amount), prefixes[prefix])
}
}
#[allow(non_snake_case)]
-pub fn format_IEC_bytes(amount: u64) -> (String, String) {
+pub fn format_IEC_bytes<'s>(amount: u64) -> (String, &'s str) {
format_bytes(amount as f64, 1024.0, IEC_PREFIXES)
}
-pub fn format_metric_bytes(amount: u64) -> (String, String) {
+pub fn format_metric_bytes<'s>(amount: u64) -> (String, &'s str) {
format_bytes(amount as f64, 1000.0, METRIC_PREFIXES)
}
#[test]
fn test_0() {
let kk = format_metric_bytes(0);
- assert!(kk == ("0".to_string(), "".to_string()));
+ assert!(kk == ("0".to_string(), ""));
}
#[test]
fn test_999() {
let kk = format_metric_bytes(999);
- assert!(kk == ("999".to_string(), "".to_string()));
+ assert!(kk == ("999".to_string(), ""));
}
#[test]
fn test_1000() {
let kk = format_metric_bytes(1000);
- assert!(kk == ("1.0".to_string(), "K".to_string()));
+ assert!(kk == ("1.0".to_string(), "K"));
}
#[test]
fn test_1030() {
let kk = format_metric_bytes(1030);
- assert!(kk == ("1.0".to_string(), "K".to_string()));
+ assert!(kk == ("1.0".to_string(), "K"));
}
#[test]
fn test_1100() {
let kk = format_metric_bytes(1100);
- assert!(kk == ("1.1".to_string(), "K".to_string()));
+ assert!(kk == ("1.1".to_string(), "K"));
}
#[test]
fn test_1111() {
let kk = format_metric_bytes(1111);
- assert!(kk == ("1.1".to_string(), "K".to_string()));
+ assert!(kk == ("1.1".to_string(), "K"));
}
#[test]
fn test_104857() {
let kk = format_IEC_bytes(126456);
- assert!(kk == ("123".to_string(), "Ki".to_string()));
+ assert!(kk == ("123".to_string(), "Ki"));
}
#[test]
fn test_1048576() {
let kk = format_IEC_bytes(1048576);
- assert!(kk == ("1.0".to_string(), "Mi".to_string()));
+ assert!(kk == ("1.0".to_string(), "Mi"));
}