1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use std::collections::{btree_map, hash_map};
use std::ffi::OsString;
use std::ops::Add;
use std::path::PathBuf;
pub trait EntryExt<'a> {
type Value: 'a;
fn or_default_(self) -> &'a mut Self::Value;
}
impl<'a, K: Ord + 'a, V: Default + 'a> EntryExt<'a> for btree_map::Entry<'a, K, V> {
type Value = V;
fn or_default_(self) -> &'a mut V {
self.or_insert_with(V::default)
}
}
impl<'a, K: 'a, V: Default + 'a> EntryExt<'a> for hash_map::Entry<'a, K, V> {
type Value = V;
fn or_default_(self) -> &'a mut V {
self.or_insert_with(V::default)
}
}
pub fn tuple_3_add<A: Add<X>, B: Add<Y>, C: Add<Z>, X, Y, Z>(left: (A, B, C), right: (X, Y, Z)) -> (A::Output, B::Output, C::Output) {
(left.0 + right.0, left.1 + right.1, left.2 + right.2)
}
pub fn tuple_4_add<A: Add<X>, B: Add<Y>, C: Add<Z>, D: Add<W>, X, Y, Z, W>(left: (A, B, C, D), right: (X, Y, Z, W)) -> (A::Output, B::Output, C::Output, D::Output) {
(left.0 + right.0, left.1 + right.1, left.2 + right.2, left.3 + right.3)
}
pub trait IntoStringLossy {
fn into_string_lossy(self) -> String;
}
impl IntoStringLossy for OsString {
fn into_string_lossy(self) -> String {
self.into_string().unwrap_or_else(|s| s.to_string_lossy().into_owned())
}
}
impl IntoStringLossy for Vec<u8> {
fn into_string_lossy(self) -> String {
String::from_utf8(self).unwrap_or_else(|e| String::from_utf8_lossy(&e.into_bytes()).into_owned())
}
}
impl IntoStringLossy for PathBuf {
fn into_string_lossy(self) -> String {
self.into_os_string().into_string_lossy()
}
}