#[experimental]
pub trait Empty {
fn empty(&self) -> bool;
}
#[experimental]
impl <'a>Empty for &'a str {
fn empty(&self) -> bool {
self.is_empty()
}
}
#[experimental]
impl Empty for String {
fn empty(&self) -> bool {
self.as_slice().is_empty()
}
}
#[experimental]
impl Empty for Vec<String> {
fn empty(&self) -> bool {
self.is_empty()
}
}
#[experimental]
impl <'a>Empty for Vec<&'a str> {
fn empty(&self) -> bool {
self.is_empty()
}
}
#[experimental]
pub fn to_opt<T: Empty>(arg: T) -> Option<T> {
if arg.empty() {
None
} else {
Some(arg)
}
}
#[cfg(test)]
mod test {
use super::to_opt;
#[test]
fn test_to_opt() {
assert_eq!(to_opt(""), None);
assert_eq!(to_opt("val"), Some("val"));
assert_eq!(to_opt("".to_string()), None);
assert_eq!(to_opt("val".to_string()), Some("val".to_string()));
assert_eq!(to_opt(Vec::<&str>::new()), None);
assert_eq!(to_opt(vec!["a"]), Some(vec!["a"]));
assert_eq!(to_opt(Vec::<String>::new()), None);
assert_eq!(to_opt(vec!["a".to_string()]),
Some(vec!["a".to_string()]));
}
}