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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use aoc_misc::prelude::*;

#[ derive (Clone) ]
pub enum InpStr <'inp> {
	Borrow (& 'inp str),
	RefCount (Rc <str>),
}

impl InpStr <'static> {

	#[ inline ]
	pub fn alloc (val: impl AsRef <str>) -> Self {
		Self::RefCount (Rc::from (val.as_ref ()))
	}

}

impl <'inp> InpStr <'inp> {

	#[ inline ]
	#[ must_use ]
	pub const fn borrow (val: & 'inp str) -> Self {
		Self::Borrow (val)
	}

	#[ inline ]
	#[ must_use ]
	pub fn to_owned (& self) -> String {
		self.deref ().to_owned ()
	}

	#[ inline ]
	#[ must_use ]
	pub fn borrowed (& self) -> & 'inp str {
		match * self {
			Self::Borrow (val) => val,
			Self::RefCount (_) => panic! ("Can't call borrowed () on an InpStr::RefCount"),
		}
	}

	#[ inline ]
	#[ must_use ]
	pub fn as_str (& self) -> & str {
		match * self {
			Self::Borrow (val) => val,
			Self::RefCount (ref val) => val,
		}
	}

}

impl <'inp> Debug for InpStr <'inp> {
	#[ inline ]
	fn fmt (& self, formatter: & mut fmt::Formatter) -> fmt::Result {
		Debug::fmt (& ** self, formatter)
	}
}

impl <'inp> Deref for InpStr <'inp> {
	type Target = str;
	#[ inline ]
	fn deref (& self) -> & str {
		match * self {
			Self::Borrow (val) => val,
			Self::RefCount (ref val) => val,
		}
	}
}

impl <'inp> Display for InpStr <'inp> {
	#[ inline ]
	fn fmt (& self, formatter: & mut fmt::Formatter) -> fmt::Result {
		Display::fmt (& ** self, formatter)
	}
}

impl <'inp> Eq for InpStr <'inp> {
}

impl <'inp> From <& 'inp str> for InpStr <'inp> {
	#[ inline ]
	fn from (from: & 'inp str) -> Self {
		Self::borrow (from)
	}
}

impl <'inp> Hash for InpStr <'inp> {
	#[ inline ]
	fn hash <Hshr: Hasher> (& self, hasher: & mut Hshr) {
		self.deref ().hash (hasher);
	}
}

impl <'inp> Ord for InpStr <'inp> {
	#[ inline ]
	fn cmp (& self, other: & Self) -> Ordering {
		self.deref ().cmp (& ** other)
	}
}

impl <'inp> PartialEq for InpStr <'inp> {
	#[ inline ]
	fn eq (& self, other: & Self) -> bool {
		self.deref ().eq (& ** other)
	}
}

impl <'inp> PartialEq <& str> for InpStr <'inp> {
	#[ inline ]
	fn eq (& self, other: && str) -> bool {
		self.deref ().eq (* other)
	}
}

impl <'inp> PartialEq <& str> for & InpStr <'inp> {
	#[ inline ]
	fn eq (& self, other: && str) -> bool {
		(* self).deref ().eq (* other)
	}
}

impl <'inp> PartialEq <InpStr <'inp>> for & str {
	#[ inline ]
	fn eq (& self, other: & InpStr <'inp>) -> bool {
		self.eq (&& ** other)
	}
}

impl <'inp> PartialOrd for InpStr <'inp> {
	#[ inline ]
	fn partial_cmp (& self, other: & Self) -> Option <Ordering> {
		self.deref ().partial_cmp (& ** other)
	}
}

// conversions to other types

impl <'inp> From <InpStr <'inp>> for Rc <str> {
	#[ inline ]
	fn from (val: InpStr <'inp>) -> Self {
		match val {
			InpStr::Borrow (val) => Self::from (val),
			InpStr::RefCount (val) => val,
		}
	}
}

impl <'inp> From <InpStr <'inp>> for String {
	#[ inline ]
	fn from (val: InpStr <'inp>) -> Self {
		val.to_owned ()
	}
}