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
use std::{borrow::Borrow, fmt, ops};

derive_common_OBJ! {
pub struct CachedStr {
	str: String,
	old_str: Box<str>,
	accessed: bool,
}}
impl CachedStr {
	pub fn str(&mut self) -> &mut String {
		let Self { str, accessed, .. } = self;
		*accessed = true;
		str
	}
	pub fn new(s: impl Into<String>) -> Self {
		let (str, old_str) = (s.into(), "".into());
		Self { str, old_str, accessed: true }
	}
	pub fn changed(&mut self) -> bool {
		let Self { ref str, old_str, accessed } = self;
		if !*accessed || str[..] == old_str[..] {
			return false;
		}
		*accessed = false;
		*old_str = str.clone().into();
		true
	}
}
impl AsRef<str> for CachedStr {
	fn as_ref(&self) -> &str {
		&self.str
	}
}
impl Borrow<str> for CachedStr {
	fn borrow(&self) -> &str {
		&self.str
	}
}
impl ops::Deref for CachedStr {
	type Target = str;

	fn deref(&self) -> &str {
		&self.str
	}
}
impl fmt::Display for CachedStr {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		write!(f, "{}", self.str)
	}
}
impl<S: Into<String>> From<S> for CachedStr {
	fn from(s: S) -> Self {
		Self::new(s)
	}
}
impl Eq for CachedStr {}
impl PartialEq for CachedStr {
	fn eq(&self, r: &Self) -> bool {
		self.str == r.str
	}
}