Mercurial > public > mercurial-scm > hg-stable
comparison rust/hg-core/src/utils/hg_path.rs @ 43914:4b3c8df189bc
rust-hg-path: implement more readable custom Debug for HgPath{,Buf}
The default prints the vector of bytes as a list of integers. I
considered instead getting rid of the Debug trait, but we use the
Debug format in lots of derived Debug instances, so we probably do
want to implement it.
Differential Revision: https://phab.mercurial-scm.org/D7604
author | Martin von Zweigbergk <martinvonz@google.com> |
---|---|
date | Wed, 11 Dec 2019 09:39:14 -0800 |
parents | 4f1543a2f5c3 |
children | baa4e7fdfd47 |
comparison
equal
deleted
inserted
replaced
43913:68af0228fedc | 43914:4b3c8df189bc |
---|---|
75 // layout-compatible with `[u8]`. | 75 // layout-compatible with `[u8]`. |
76 // When attribute privacy is implemented, `Slice` should be annotated as | 76 // When attribute privacy is implemented, `Slice` should be annotated as |
77 // `#[repr(transparent)]`. | 77 // `#[repr(transparent)]`. |
78 // Anyway, `Slice` representation and layout are considered implementation | 78 // Anyway, `Slice` representation and layout are considered implementation |
79 // detail, are not documented and must not be relied upon. | 79 // detail, are not documented and must not be relied upon. |
80 #[derive(Eq, Ord, PartialEq, PartialOrd, Debug, Hash)] | 80 #[derive(Eq, Ord, PartialEq, PartialOrd, Hash)] |
81 pub struct HgPath { | 81 pub struct HgPath { |
82 inner: [u8], | 82 inner: [u8], |
83 } | 83 } |
84 | 84 |
85 impl HgPath { | 85 impl HgPath { |
179 fn is_valid(&self) -> bool { | 179 fn is_valid(&self) -> bool { |
180 self.check_state().is_ok() | 180 self.check_state().is_ok() |
181 } | 181 } |
182 } | 182 } |
183 | 183 |
184 impl fmt::Debug for HgPath { | |
185 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
186 write!(f, "HgPath({:?})", String::from_utf8_lossy(&self.inner)) | |
187 } | |
188 } | |
189 | |
184 impl fmt::Display for HgPath { | 190 impl fmt::Display for HgPath { |
185 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | 191 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
186 write!(f, "{}", String::from_utf8_lossy(&self.inner)) | 192 write!(f, "{}", String::from_utf8_lossy(&self.inner)) |
187 } | 193 } |
188 } | 194 } |
189 | 195 |
190 #[derive(Eq, Ord, Clone, PartialEq, PartialOrd, Debug, Hash)] | 196 #[derive(Eq, Ord, Clone, PartialEq, PartialOrd, Hash)] |
191 pub struct HgPathBuf { | 197 pub struct HgPathBuf { |
192 inner: Vec<u8>, | 198 inner: Vec<u8>, |
193 } | 199 } |
194 | 200 |
195 impl HgPathBuf { | 201 impl HgPathBuf { |
205 pub fn into_vec(self) -> Vec<u8> { | 211 pub fn into_vec(self) -> Vec<u8> { |
206 self.inner | 212 self.inner |
207 } | 213 } |
208 pub fn as_ref(&self) -> &[u8] { | 214 pub fn as_ref(&self) -> &[u8] { |
209 self.inner.as_ref() | 215 self.inner.as_ref() |
216 } | |
217 } | |
218 | |
219 impl fmt::Debug for HgPathBuf { | |
220 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
221 write!(f, "HgPathBuf({:?})", String::from_utf8_lossy(&self.inner)) | |
210 } | 222 } |
211 } | 223 } |
212 | 224 |
213 impl fmt::Display for HgPathBuf { | 225 impl fmt::Display for HgPathBuf { |
214 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | 226 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |