2013-02-23 16:41:44 -06:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06: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 18:52:32 -05:00
|
|
|
/*!
|
|
|
|
|
|
|
|
The `ToStr` trait for converting to strings
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2014-02-19 20:56:33 -06:00
|
|
|
use fmt;
|
2012-12-23 16:41:37 -06:00
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// A generic trait for converting a value to a string
|
2013-02-03 22:47:26 -06:00
|
|
|
pub trait ToStr {
|
2013-05-28 16:35:52 -05:00
|
|
|
/// Converts the value of `self` to an owned string
|
2013-03-21 23:20:48 -05:00
|
|
|
fn to_str(&self) -> ~str;
|
2013-02-03 22:47:26 -06:00
|
|
|
}
|
2012-02-22 06:06:38 -06:00
|
|
|
|
2013-04-20 12:39:15 -05:00
|
|
|
/// Trait for converting a type to a string, consuming it in the process.
|
2013-12-14 08:04:22 -06:00
|
|
|
pub trait IntoStr {
|
2013-08-17 17:28:04 -05:00
|
|
|
/// Consume and convert to a string.
|
2013-06-16 04:04:53 -05:00
|
|
|
fn into_str(self) -> ~str;
|
2013-04-20 12:39:15 -05:00
|
|
|
}
|
|
|
|
|
2014-02-19 20:56:33 -06:00
|
|
|
impl<T: fmt::Show> ToStr for T {
|
|
|
|
fn to_str(&self) -> ~str { format!("{}", *self) }
|
2013-02-27 09:01:53 -06:00
|
|
|
}
|
|
|
|
|
2012-02-22 06:06:38 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2013-07-23 07:40:25 -05:00
|
|
|
use super::*;
|
2014-04-15 20:17:48 -05:00
|
|
|
use str::StrSlice;
|
2013-07-23 07:40:25 -05:00
|
|
|
|
2012-02-22 06:06:38 -06:00
|
|
|
#[test]
|
|
|
|
fn test_simple_types() {
|
2014-04-15 20:17:48 -05:00
|
|
|
assert_eq!(1i.to_str(), "1".to_owned());
|
|
|
|
assert_eq!((-1i).to_str(), "-1".to_owned());
|
|
|
|
assert_eq!(200u.to_str(), "200".to_owned());
|
|
|
|
assert_eq!(2u8.to_str(), "2".to_owned());
|
|
|
|
assert_eq!(true.to_str(), "true".to_owned());
|
|
|
|
assert_eq!(false.to_str(), "false".to_owned());
|
|
|
|
assert_eq!(().to_str(), "()".to_owned());
|
|
|
|
assert_eq!(("hi".to_owned()).to_str(), "hi".to_owned());
|
2012-02-22 06:06:38 -06:00
|
|
|
}
|
|
|
|
|
2012-07-08 02:39:20 -05:00
|
|
|
#[test]
|
2012-02-22 06:06:38 -06:00
|
|
|
fn test_vectors() {
|
2014-04-25 03:08:02 -05:00
|
|
|
let x: ~[int] = box [];
|
2014-04-15 20:17:48 -05:00
|
|
|
assert_eq!(x.to_str(), "[]".to_owned());
|
2014-04-25 03:08:02 -05:00
|
|
|
assert_eq!((box [1]).to_str(), "[1]".to_owned());
|
|
|
|
assert_eq!((box [1, 2, 3]).to_str(), "[1, 2, 3]".to_owned());
|
|
|
|
assert!((box [box [], box [1], box [1, 1]]).to_str() ==
|
2014-04-15 20:17:48 -05:00
|
|
|
"[[], [1], [1, 1]]".to_owned());
|
2012-02-22 06:06:38 -06:00
|
|
|
}
|
2013-05-12 19:34:15 -05:00
|
|
|
}
|