2013-02-23 22:41:44 +00:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-04 00:48:01 +00:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-09-19 23:52:32 +00:00
|
|
|
/*!
|
|
|
|
|
|
|
|
The `ToStr` trait for converting to strings
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2013-08-01 07:16:42 +00:00
|
|
|
use option::{Some, None};
|
2013-05-11 13:39:11 +00:00
|
|
|
use str::OwnedStr;
|
2013-09-08 15:01:16 +00:00
|
|
|
use iter::Iterator;
|
2013-06-21 12:29:53 +00:00
|
|
|
use vec::ImmutableVector;
|
2012-12-23 22:41:37 +00:00
|
|
|
|
2013-05-28 21:35:52 +00:00
|
|
|
/// A generic trait for converting a value to a string
|
2013-02-04 04:47:26 +00:00
|
|
|
pub trait ToStr {
|
2013-05-28 21:35:52 +00:00
|
|
|
/// Converts the value of `self` to an owned string
|
2013-03-22 04:20:48 +00:00
|
|
|
fn to_str(&self) -> ~str;
|
2013-02-04 04:47:26 +00:00
|
|
|
}
|
2012-02-22 12:06:38 +00:00
|
|
|
|
2013-04-20 17:39:15 +00:00
|
|
|
/// Trait for converting a type to a string, consuming it in the process.
|
2013-12-14 14:04:22 +00:00
|
|
|
pub trait IntoStr {
|
2013-08-17 22:28:04 +00:00
|
|
|
/// Consume and convert to a string.
|
2013-06-16 09:04:53 +00:00
|
|
|
fn into_str(self) -> ~str;
|
2013-04-20 17:39:15 +00:00
|
|
|
}
|
|
|
|
|
2013-02-14 19:47:00 +00:00
|
|
|
impl ToStr for () {
|
2013-06-18 21:45:18 +00:00
|
|
|
#[inline]
|
2013-03-22 04:20:48 +00:00
|
|
|
fn to_str(&self) -> ~str { ~"()" }
|
2012-02-22 12:06:38 +00:00
|
|
|
}
|
|
|
|
|
2013-12-10 07:16:18 +00:00
|
|
|
impl<'a,A:ToStr> ToStr for &'a [A] {
|
2013-06-18 21:45:18 +00:00
|
|
|
#[inline]
|
2013-03-22 04:20:48 +00:00
|
|
|
fn to_str(&self) -> ~str {
|
2013-06-07 01:54:14 +00:00
|
|
|
let mut acc = ~"[";
|
|
|
|
let mut first = true;
|
2013-08-03 16:45:23 +00:00
|
|
|
for elt in self.iter() {
|
2013-05-11 14:01:53 +00:00
|
|
|
if first {
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
acc.push_str(", ");
|
|
|
|
}
|
|
|
|
acc.push_str(elt.to_str());
|
2013-02-27 15:01:53 +00:00
|
|
|
}
|
2013-05-11 14:01:53 +00:00
|
|
|
acc.push_char(']');
|
2013-04-08 20:50:34 +00:00
|
|
|
acc
|
2013-02-27 15:01:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-21 01:07:17 +00:00
|
|
|
impl<A:ToStr> ToStr for ~[A] {
|
2013-06-18 21:45:18 +00:00
|
|
|
#[inline]
|
2013-03-22 04:20:48 +00:00
|
|
|
fn to_str(&self) -> ~str {
|
2013-06-07 01:54:14 +00:00
|
|
|
let mut acc = ~"[";
|
|
|
|
let mut first = true;
|
2013-08-03 16:45:23 +00:00
|
|
|
for elt in self.iter() {
|
2013-05-11 14:01:53 +00:00
|
|
|
if first {
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
acc.push_str(", ");
|
|
|
|
}
|
|
|
|
acc.push_str(elt.to_str());
|
2013-02-27 15:01:53 +00:00
|
|
|
}
|
2013-05-11 14:01:53 +00:00
|
|
|
acc.push_char(']');
|
2013-04-08 20:50:34 +00:00
|
|
|
acc
|
2013-02-27 15:01:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-22 12:06:38 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2013-07-23 12:40:25 +00:00
|
|
|
use super::*;
|
|
|
|
|
2012-02-22 12:06:38 +00:00
|
|
|
#[test]
|
|
|
|
fn test_simple_types() {
|
2013-05-19 02:02:45 +00:00
|
|
|
assert_eq!(1i.to_str(), ~"1");
|
|
|
|
assert_eq!((-1i).to_str(), ~"-1");
|
|
|
|
assert_eq!(200u.to_str(), ~"200");
|
|
|
|
assert_eq!(2u8.to_str(), ~"2");
|
|
|
|
assert_eq!(true.to_str(), ~"true");
|
|
|
|
assert_eq!(false.to_str(), ~"false");
|
|
|
|
assert_eq!(().to_str(), ~"()");
|
|
|
|
assert_eq!((~"hi").to_str(), ~"hi");
|
2012-02-22 12:06:38 +00:00
|
|
|
}
|
|
|
|
|
2012-07-08 07:39:20 +00:00
|
|
|
#[test]
|
2012-02-22 12:06:38 +00:00
|
|
|
fn test_vectors() {
|
2012-06-29 23:26:56 +00:00
|
|
|
let x: ~[int] = ~[];
|
2013-05-19 02:02:45 +00:00
|
|
|
assert_eq!(x.to_str(), ~"[]");
|
|
|
|
assert_eq!((~[1]).to_str(), ~"[1]");
|
|
|
|
assert_eq!((~[1, 2, 3]).to_str(), ~"[1, 2, 3]");
|
2013-03-29 01:39:09 +00:00
|
|
|
assert!((~[~[], ~[1], ~[1, 1]]).to_str() ==
|
2013-03-06 21:58:02 +00:00
|
|
|
~"[[], [1], [1, 1]]");
|
2012-02-22 12:06:38 +00:00
|
|
|
}
|
2013-05-13 00:34:15 +00:00
|
|
|
}
|