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
64
65
66
67
68
69
#![warn(clippy::all, rust_2018_idioms)]
#![allow(clippy::filter_map, clippy::find_map, clippy::shadow_unrelated, clippy::use_self)]
use std::borrow::Borrow;
use std::hash::{Hash, Hasher};
#[repr(transparent)]
pub struct UniCase<S: ?Sized>(pub S);
impl<'a> From<&'a str> for &'a UniCase<str> {
fn from(s: &'a str) -> &'a UniCase<str> {
unsafe { &*(s as *const str as *const UniCase<str>) }
}
}
pub fn u(s: &str) -> &UniCase<str> {
s.into()
}
impl<S> AsRef<str> for UniCase<S>
where S: AsRef<str> + ?Sized,
{
fn as_ref(&self) -> &str {
self.0.as_ref()
}
}
impl<S> Hash for UniCase<S>
where S: AsRef<str> + ?Sized,
{
fn hash<H: Hasher>(&self, hasher: &mut H) {
let bytes = self.0.as_ref().as_bytes();
for byte in bytes {
hasher.write_u8(byte.to_ascii_lowercase());
}
}
}
impl<S1, S2> PartialEq<UniCase<S2>> for UniCase<S1>
where S1: AsRef<str> + ?Sized,
S2: AsRef<str> + ?Sized,
{
fn eq(&self, other: &UniCase<S2>) -> bool {
self.0.as_ref().eq_ignore_ascii_case(other.0.as_ref())
}
}
impl<S> Eq for UniCase<S>
where S: AsRef<str> + ?Sized,
{}
impl Borrow<UniCase<str>> for UniCase<String> {
fn borrow(&self) -> &UniCase<str> {
self.0.as_str().into()
}
}