yfunc_rust/
func.rs

1use std::{collections::HashSet, hash::Hash, io::Write};
2
3use crate::prelude::*;
4
5pub trait Unique {
6    fn unique(&self) -> Self;
7}
8
9impl<T> Unique for Vec<T> where T: Eq+Hash+Clone {
10
11    fn unique(&self) -> Self {
12        let mut appear: HashSet<T> = HashSet::new();
13        let mut ret: Vec<T> = vec![];
14        for x in self {
15            if !appear.contains(x) {
16                ret.push(x.clone());
17                appear.insert(x.clone());
18            }
19        }
20        ret
21    }
22
23}
24
25pub trait VariableFormat {
26    fn Aabb(&self) -> String;
27}
28
29impl VariableFormat for String {
30
31    fn Aabb(&self) -> String {
32        let mut c = self.chars();
33        match c.next() {
34            Some(first_char) => first_char.to_uppercase().collect::<String>() + c.as_str().to_lowercase().as_str(),
35            None => String::new(),
36        }
37    }
38
39}
40
41pub fn write_str_to_stdout(s: &str) -> YRes<()> {
42    write!(std::io::stdout(), "{}", s).map_err(|e|
43        err!("failed to output string").trace(
44            ctx!("write str to stdout: write! failed", e)
45        )
46    )?;
47    std::io::stdout().flush().map_err(|e|
48        err!("failed to output string").trace(
49            ctx!("write str to stdout -> flush stdout: std::io::stdout().flush failed", e)
50        )
51    )?;
52    Ok(())
53}
54
55pub fn write_bytes_to_stdout(b: &Vec<u8>) -> YRes<()> {
56    std::io::stdout().write_all(&b).map_err(|e|
57        err!("failed to output bytes").trace(
58            ctx!("write bytes to stdout: std::io::stdout().write_all failed", e)
59        )
60    )?;
61    std::io::stdout().flush().map_err(|e|
62        err!("failed to output bytes").trace(
63            ctx!("write bytes to stdout -> flush stdout: std::io::stdout().flush failed", e)
64        )
65    )?;
66    Ok(())
67}