use std::{collections::HashSet, hash::Hash, io::Write};
use crate::prelude::*;
pub trait Unique {
fn unique(&self) -> Self;
}
impl<T> Unique for Vec<T> where T: Eq+Hash+Clone {
fn unique(&self) -> Self {
let mut appear: HashSet<T> = HashSet::new();
let mut ret: Vec<T> = vec![];
for x in self {
if !appear.contains(x) {
ret.push(x.clone());
appear.insert(x.clone());
}
}
ret
}
}
pub trait VariableFormat {
fn Aabb(&self) -> String;
}
impl VariableFormat for String {
fn Aabb(&self) -> String {
let mut c = self.chars();
match c.next() {
Some(first_char) => first_char.to_uppercase().collect::<String>() + c.as_str().to_lowercase().as_str(),
None => String::new(),
}
}
}
pub fn write_str_to_stdout(s: &str) -> YRes<()> {
write!(std::io::stdout(), "{}", s).map_err(|e|
err!(IOError::"write str to stdout", s, e)
)?;
std::io::stdout().flush().map_err(|e|
err!(IOError::"flush stdout", s, e)
)?;
Ok(())
}
pub fn write_bytes_to_stdout(b: &Vec<u8>) -> YRes<()> {
std::io::stdout().write_all(&b).map_err(|e|
err!(IOError::"write bytes to stdout", e)
)?;
std::io::stdout().flush().map_err(|e|
err!(IOError::"flush stdout", e)
)?;
Ok(())
}