|
|
|
@ -3,17 +3,18 @@ use std::{
|
|
|
|
|
hash::Hash,
|
|
|
|
|
collections::{HashSet, HashMap},
|
|
|
|
|
mem,
|
|
|
|
|
fmt,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
pub trait DedupFullExt
|
|
|
|
|
{
|
|
|
|
|
fn dedup_full(&mut self);
|
|
|
|
|
fn dedup_full(&mut self);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> DedupFullExt for Vec<T>
|
|
|
|
|
where T: Hash + Default + Eq
|
|
|
|
|
{
|
|
|
|
|
fn dedup_full(&mut self)
|
|
|
|
|
fn dedup_full(&mut self)
|
|
|
|
|
{
|
|
|
|
|
let mut set: HashMap<T, usize> = HashMap::new();
|
|
|
|
|
for (i,x) in (0..).zip(self.iter_mut())
|
|
|
|
@ -37,6 +38,70 @@ where T: Hash + Default + Eq
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pub trait ErrorLogForgetExt<T, E>: Sized
|
|
|
|
|
{
|
|
|
|
|
fn log_and_forget(self, mode: &log::Mode, level: log::Level) -> Option<T>;
|
|
|
|
|
}
|
|
|
|
|
impl<T,E> ErrorLogForgetExt<T,E> for Result<T,E>
|
|
|
|
|
where E: fmt::Display,
|
|
|
|
|
{
|
|
|
|
|
fn log_and_forget(self, mode: &log::Mode, level: log::Level) -> Option<T>
|
|
|
|
|
{
|
|
|
|
|
match self {
|
|
|
|
|
Err(e) => {
|
|
|
|
|
log!(mode.level(level), "{}", &e);
|
|
|
|
|
if let log::Level::Fatal = level {
|
|
|
|
|
std::process::exit(1);
|
|
|
|
|
}
|
|
|
|
|
None
|
|
|
|
|
},
|
|
|
|
|
Ok(v) => Some(v),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pub trait ErrorLogExt: Sized
|
|
|
|
|
{
|
|
|
|
|
fn into_log(self, mode: &log::Mode, level: log::Level) -> Self;
|
|
|
|
|
fn into_fatal(self, mode: Option<log::Mode>) -> Self
|
|
|
|
|
{
|
|
|
|
|
self.into_log(&mode.unwrap_or(log::Mode::Verbose), log::Level::Fatal)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T,E> ErrorLogExt for Result<T,E>
|
|
|
|
|
where E: fmt::Display
|
|
|
|
|
{
|
|
|
|
|
fn into_log(self, mode: &log::Mode, level: log::Level) -> Self
|
|
|
|
|
{
|
|
|
|
|
match self {
|
|
|
|
|
Err(e) => {
|
|
|
|
|
log!(mode.level(level), "{}", &e);
|
|
|
|
|
if let log::Level::Fatal = level {
|
|
|
|
|
std::process::exit(1);
|
|
|
|
|
}
|
|
|
|
|
Err(e)
|
|
|
|
|
},
|
|
|
|
|
Ok(v) => Ok(v),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub trait ErrorExt<T>
|
|
|
|
|
{
|
|
|
|
|
fn into_string(self) -> Result<T, String>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T,E> ErrorExt<T> for Result<T,E>
|
|
|
|
|
where E: fmt::Display
|
|
|
|
|
{
|
|
|
|
|
fn into_string(self) -> Result<T, String>
|
|
|
|
|
{
|
|
|
|
|
match self {
|
|
|
|
|
Ok(v) => Ok(v),
|
|
|
|
|
Err(e) => Err(format!("{}", e)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod test
|
|
|
|
|