1use std::fmt;
4use std::ops::Bound;
5use std::u8;
6
7#[allow(unused_imports)]
8#[cfg(test)]
9use proptest::arbitrary::any_with;
10#[allow(unused_imports)]
11#[cfg(test)]
12use proptest::collection::size_range;
13#[cfg(test)]
14use proptest_derive::Arbitrary;
15
16use super::HexRepr;
17use crate::kv::codec::BytesEncoder;
18use crate::kv::codec::{self};
19use crate::proto::kvrpcpb;
20
21const _PROPTEST_KEY_MAX: usize = 1024 * 2; #[derive(Default, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
67#[cfg_attr(test, derive(Arbitrary))]
68#[repr(transparent)]
69pub struct Key(
70 #[cfg_attr(
71 test,
72 proptest(strategy = "any_with::<Vec<u8>>((size_range(_PROPTEST_KEY_MAX), ()))")
73 )]
74 pub(super) Vec<u8>,
75);
76
77impl AsRef<Key> for kvrpcpb::Mutation {
78 fn as_ref(&self) -> &Key {
79 self.key.as_ref()
80 }
81}
82
83impl Key {
84 pub const EMPTY: Self = Key(Vec::new());
86
87 #[inline]
89 pub fn is_empty(&self) -> bool {
90 self.0.is_empty()
91 }
92
93 #[inline]
95 pub(super) fn zero_terminated(&self) -> bool {
96 self.0.last().map(|i| *i == 0).unwrap_or(false)
97 }
98
99 #[inline]
103 pub(super) fn push_zero(&mut self) {
104 self.0.push(0)
105 }
106
107 #[inline]
109 pub(super) fn into_lower_bound(mut self) -> Bound<Key> {
110 if self.zero_terminated() {
111 self.0.pop().unwrap();
112 Bound::Excluded(self)
113 } else {
114 Bound::Included(self)
115 }
116 }
117
118 #[inline]
120 pub(super) fn into_upper_bound(mut self) -> Bound<Key> {
121 if self.zero_terminated() {
122 self.0.pop().unwrap();
123 Bound::Included(self)
124 } else {
125 Bound::Excluded(self)
126 }
127 }
128
129 #[inline]
131 #[must_use]
132 pub fn to_encoded(&self) -> Key {
133 let len = codec::max_encoded_bytes_size(self.0.len());
134 let mut encoded = Vec::with_capacity(len);
135 encoded.encode_bytes(&self.0, false).unwrap();
136 Key(encoded)
137 }
138
139 pub fn len(&self) -> usize {
140 self.0.len()
141 }
142}
143
144impl From<Vec<u8>> for Key {
145 fn from(v: Vec<u8>) -> Self {
146 Key(v)
147 }
148}
149
150impl From<String> for Key {
151 fn from(v: String) -> Key {
152 Key(v.into_bytes())
153 }
154}
155
156impl From<Key> for Vec<u8> {
157 fn from(key: Key) -> Self {
158 key.0
159 }
160}
161
162impl<'a> From<&'a Key> for &'a [u8] {
163 fn from(key: &'a Key) -> Self {
164 &key.0
165 }
166}
167
168impl<'a> From<&'a Vec<u8>> for &'a Key {
169 fn from(key: &'a Vec<u8>) -> Self {
170 unsafe { &*(key as *const Vec<u8> as *const Key) }
171 }
172}
173impl AsRef<Key> for Key {
174 fn as_ref(&self) -> &Key {
175 self
176 }
177}
178
179impl AsRef<Key> for Vec<u8> {
180 fn as_ref(&self) -> &Key {
181 unsafe { &*(self as *const Vec<u8> as *const Key) }
182 }
183}
184
185impl fmt::Debug for Key {
186 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
187 write!(f, "Key({})", HexRepr(&self.0))
188 }
189}