]> git.proxmox.com Git - cargo.git/blob - vendor/bstr/src/impls.rs
New upstream version 0.47.0
[cargo.git] / vendor / bstr / src / impls.rs
1 macro_rules! impl_partial_eq {
2 ($lhs:ty, $rhs:ty) => {
3 impl<'a, 'b> PartialEq<$rhs> for $lhs {
4 #[inline]
5 fn eq(&self, other: &$rhs) -> bool {
6 let other: &[u8] = other.as_ref();
7 PartialEq::eq(self.as_bytes(), other)
8 }
9 }
10
11 impl<'a, 'b> PartialEq<$lhs> for $rhs {
12 #[inline]
13 fn eq(&self, other: &$lhs) -> bool {
14 let this: &[u8] = self.as_ref();
15 PartialEq::eq(this, other.as_bytes())
16 }
17 }
18 };
19 }
20
21 #[cfg(feature = "std")]
22 macro_rules! impl_partial_eq_cow {
23 ($lhs:ty, $rhs:ty) => {
24 impl<'a, 'b> PartialEq<$rhs> for $lhs {
25 #[inline]
26 fn eq(&self, other: &$rhs) -> bool {
27 let other: &[u8] = (&**other).as_ref();
28 PartialEq::eq(self.as_bytes(), other)
29 }
30 }
31
32 impl<'a, 'b> PartialEq<$lhs> for $rhs {
33 #[inline]
34 fn eq(&self, other: &$lhs) -> bool {
35 let this: &[u8] = (&**other).as_ref();
36 PartialEq::eq(this, other.as_bytes())
37 }
38 }
39 };
40 }
41
42 macro_rules! impl_partial_ord {
43 ($lhs:ty, $rhs:ty) => {
44 impl<'a, 'b> PartialOrd<$rhs> for $lhs {
45 #[inline]
46 fn partial_cmp(&self, other: &$rhs) -> Option<Ordering> {
47 let other: &[u8] = other.as_ref();
48 PartialOrd::partial_cmp(self.as_bytes(), other)
49 }
50 }
51
52 impl<'a, 'b> PartialOrd<$lhs> for $rhs {
53 #[inline]
54 fn partial_cmp(&self, other: &$lhs) -> Option<Ordering> {
55 let this: &[u8] = self.as_ref();
56 PartialOrd::partial_cmp(this, other.as_bytes())
57 }
58 }
59 };
60 }
61
62 #[cfg(feature = "std")]
63 mod bstring {
64 use std::borrow::{Borrow, Cow, ToOwned};
65 use std::cmp::Ordering;
66 use std::fmt;
67 use std::iter::FromIterator;
68 use std::ops;
69
70 use bstr::BStr;
71 use bstring::BString;
72 use ext_vec::ByteVec;
73
74 impl fmt::Display for BString {
75 #[inline]
76 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
77 fmt::Display::fmt(self.as_bstr(), f)
78 }
79 }
80
81 impl fmt::Debug for BString {
82 #[inline]
83 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
84 fmt::Debug::fmt(self.as_bstr(), f)
85 }
86 }
87
88 impl ops::Deref for BString {
89 type Target = Vec<u8>;
90
91 #[inline]
92 fn deref(&self) -> &Vec<u8> {
93 &self.bytes
94 }
95 }
96
97 impl ops::DerefMut for BString {
98 #[inline]
99 fn deref_mut(&mut self) -> &mut Vec<u8> {
100 &mut self.bytes
101 }
102 }
103
104 impl AsRef<[u8]> for BString {
105 #[inline]
106 fn as_ref(&self) -> &[u8] {
107 &self.bytes
108 }
109 }
110
111 impl AsRef<BStr> for BString {
112 #[inline]
113 fn as_ref(&self) -> &BStr {
114 self.as_bstr()
115 }
116 }
117
118 impl AsMut<[u8]> for BString {
119 #[inline]
120 fn as_mut(&mut self) -> &mut [u8] {
121 &mut self.bytes
122 }
123 }
124
125 impl AsMut<BStr> for BString {
126 #[inline]
127 fn as_mut(&mut self) -> &mut BStr {
128 self.as_mut_bstr()
129 }
130 }
131
132 impl Borrow<BStr> for BString {
133 #[inline]
134 fn borrow(&self) -> &BStr {
135 self.as_bstr()
136 }
137 }
138
139 impl ToOwned for BStr {
140 type Owned = BString;
141
142 #[inline]
143 fn to_owned(&self) -> BString {
144 BString::from(self)
145 }
146 }
147
148 impl Default for BString {
149 fn default() -> BString {
150 BString::from(vec![])
151 }
152 }
153
154 impl<'a> From<&'a [u8]> for BString {
155 #[inline]
156 fn from(s: &'a [u8]) -> BString {
157 BString::from(s.to_vec())
158 }
159 }
160
161 impl From<Vec<u8>> for BString {
162 #[inline]
163 fn from(s: Vec<u8>) -> BString {
164 BString { bytes: s }
165 }
166 }
167
168 impl From<BString> for Vec<u8> {
169 #[inline]
170 fn from(s: BString) -> Vec<u8> {
171 s.bytes
172 }
173 }
174
175 impl<'a> From<&'a str> for BString {
176 #[inline]
177 fn from(s: &'a str) -> BString {
178 BString::from(s.as_bytes().to_vec())
179 }
180 }
181
182 impl From<String> for BString {
183 #[inline]
184 fn from(s: String) -> BString {
185 BString::from(s.into_bytes())
186 }
187 }
188
189 impl<'a> From<&'a BStr> for BString {
190 #[inline]
191 fn from(s: &'a BStr) -> BString {
192 BString::from(s.bytes.to_vec())
193 }
194 }
195
196 impl<'a> From<BString> for Cow<'a, BStr> {
197 #[inline]
198 fn from(s: BString) -> Cow<'a, BStr> {
199 Cow::Owned(s)
200 }
201 }
202
203 impl FromIterator<char> for BString {
204 #[inline]
205 fn from_iter<T: IntoIterator<Item = char>>(iter: T) -> BString {
206 BString::from(iter.into_iter().collect::<String>())
207 }
208 }
209
210 impl FromIterator<u8> for BString {
211 #[inline]
212 fn from_iter<T: IntoIterator<Item = u8>>(iter: T) -> BString {
213 BString::from(iter.into_iter().collect::<Vec<u8>>())
214 }
215 }
216
217 impl<'a> FromIterator<&'a str> for BString {
218 #[inline]
219 fn from_iter<T: IntoIterator<Item = &'a str>>(iter: T) -> BString {
220 let mut buf = vec![];
221 for b in iter {
222 buf.push_str(b);
223 }
224 BString::from(buf)
225 }
226 }
227
228 impl<'a> FromIterator<&'a [u8]> for BString {
229 #[inline]
230 fn from_iter<T: IntoIterator<Item = &'a [u8]>>(iter: T) -> BString {
231 let mut buf = vec![];
232 for b in iter {
233 buf.push_str(b);
234 }
235 BString::from(buf)
236 }
237 }
238
239 impl<'a> FromIterator<&'a BStr> for BString {
240 #[inline]
241 fn from_iter<T: IntoIterator<Item = &'a BStr>>(iter: T) -> BString {
242 let mut buf = vec![];
243 for b in iter {
244 buf.push_str(b);
245 }
246 BString::from(buf)
247 }
248 }
249
250 impl FromIterator<BString> for BString {
251 #[inline]
252 fn from_iter<T: IntoIterator<Item = BString>>(iter: T) -> BString {
253 let mut buf = vec![];
254 for b in iter {
255 buf.push_str(b);
256 }
257 BString::from(buf)
258 }
259 }
260
261 impl Eq for BString {}
262
263 impl PartialEq for BString {
264 #[inline]
265 fn eq(&self, other: &BString) -> bool {
266 &self[..] == &other[..]
267 }
268 }
269
270 impl_partial_eq!(BString, Vec<u8>);
271 impl_partial_eq!(BString, [u8]);
272 impl_partial_eq!(BString, &'a [u8]);
273 impl_partial_eq!(BString, String);
274 impl_partial_eq!(BString, str);
275 impl_partial_eq!(BString, &'a str);
276 impl_partial_eq!(BString, BStr);
277 impl_partial_eq!(BString, &'a BStr);
278
279 impl PartialOrd for BString {
280 #[inline]
281 fn partial_cmp(&self, other: &BString) -> Option<Ordering> {
282 PartialOrd::partial_cmp(&self.bytes, &other.bytes)
283 }
284 }
285
286 impl Ord for BString {
287 #[inline]
288 fn cmp(&self, other: &BString) -> Ordering {
289 self.partial_cmp(other).unwrap()
290 }
291 }
292
293 impl_partial_ord!(BString, Vec<u8>);
294 impl_partial_ord!(BString, [u8]);
295 impl_partial_ord!(BString, &'a [u8]);
296 impl_partial_ord!(BString, String);
297 impl_partial_ord!(BString, str);
298 impl_partial_ord!(BString, &'a str);
299 impl_partial_ord!(BString, BStr);
300 impl_partial_ord!(BString, &'a BStr);
301 }
302
303 mod bstr {
304 #[cfg(feature = "std")]
305 use std::borrow::Cow;
306
307 use core::cmp::Ordering;
308 use core::fmt;
309 use core::ops;
310
311 use bstr::BStr;
312 use ext_slice::ByteSlice;
313
314 impl fmt::Display for BStr {
315 #[inline]
316 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
317 for chunk in self.utf8_chunks() {
318 f.write_str(chunk.valid())?;
319 if !chunk.invalid().is_empty() {
320 f.write_str("\u{FFFD}")?;
321 }
322 }
323 Ok(())
324 }
325 }
326
327 impl fmt::Debug for BStr {
328 #[inline]
329 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
330 write!(f, "\"")?;
331 for (s, e, ch) in self.char_indices() {
332 if ch == '\u{FFFD}' {
333 for &b in self[s..e].as_bytes() {
334 write!(f, r"\x{:X}", b)?;
335 }
336 } else {
337 write!(f, "{}", ch.escape_debug())?;
338 }
339 }
340 write!(f, "\"")?;
341 Ok(())
342 }
343 }
344
345 impl ops::Deref for BStr {
346 type Target = [u8];
347
348 #[inline]
349 fn deref(&self) -> &[u8] {
350 &self.bytes
351 }
352 }
353
354 impl ops::DerefMut for BStr {
355 #[inline]
356 fn deref_mut(&mut self) -> &mut [u8] {
357 &mut self.bytes
358 }
359 }
360
361 impl ops::Index<usize> for BStr {
362 type Output = u8;
363
364 #[inline]
365 fn index(&self, idx: usize) -> &u8 {
366 &self.as_bytes()[idx]
367 }
368 }
369
370 impl ops::Index<ops::RangeFull> for BStr {
371 type Output = BStr;
372
373 #[inline]
374 fn index(&self, _: ops::RangeFull) -> &BStr {
375 self
376 }
377 }
378
379 impl ops::Index<ops::Range<usize>> for BStr {
380 type Output = BStr;
381
382 #[inline]
383 fn index(&self, r: ops::Range<usize>) -> &BStr {
384 BStr::new(&self.as_bytes()[r.start..r.end])
385 }
386 }
387
388 impl ops::Index<ops::RangeInclusive<usize>> for BStr {
389 type Output = BStr;
390
391 #[inline]
392 fn index(&self, r: ops::RangeInclusive<usize>) -> &BStr {
393 BStr::new(&self.as_bytes()[*r.start()..=*r.end()])
394 }
395 }
396
397 impl ops::Index<ops::RangeFrom<usize>> for BStr {
398 type Output = BStr;
399
400 #[inline]
401 fn index(&self, r: ops::RangeFrom<usize>) -> &BStr {
402 BStr::new(&self.as_bytes()[r.start..])
403 }
404 }
405
406 impl ops::Index<ops::RangeTo<usize>> for BStr {
407 type Output = BStr;
408
409 #[inline]
410 fn index(&self, r: ops::RangeTo<usize>) -> &BStr {
411 BStr::new(&self.as_bytes()[..r.end])
412 }
413 }
414
415 impl ops::Index<ops::RangeToInclusive<usize>> for BStr {
416 type Output = BStr;
417
418 #[inline]
419 fn index(&self, r: ops::RangeToInclusive<usize>) -> &BStr {
420 BStr::new(&self.as_bytes()[..=r.end])
421 }
422 }
423
424 impl ops::IndexMut<usize> for BStr {
425 #[inline]
426 fn index_mut(&mut self, idx: usize) -> &mut u8 {
427 &mut self.bytes[idx]
428 }
429 }
430
431 impl ops::IndexMut<ops::RangeFull> for BStr {
432 #[inline]
433 fn index_mut(&mut self, _: ops::RangeFull) -> &mut BStr {
434 self
435 }
436 }
437
438 impl ops::IndexMut<ops::Range<usize>> for BStr {
439 #[inline]
440 fn index_mut(&mut self, r: ops::Range<usize>) -> &mut BStr {
441 BStr::from_bytes_mut(&mut self.bytes[r.start..r.end])
442 }
443 }
444
445 impl ops::IndexMut<ops::RangeInclusive<usize>> for BStr {
446 #[inline]
447 fn index_mut(&mut self, r: ops::RangeInclusive<usize>) -> &mut BStr {
448 BStr::from_bytes_mut(&mut self.bytes[*r.start()..=*r.end()])
449 }
450 }
451
452 impl ops::IndexMut<ops::RangeFrom<usize>> for BStr {
453 #[inline]
454 fn index_mut(&mut self, r: ops::RangeFrom<usize>) -> &mut BStr {
455 BStr::from_bytes_mut(&mut self.bytes[r.start..])
456 }
457 }
458
459 impl ops::IndexMut<ops::RangeTo<usize>> for BStr {
460 #[inline]
461 fn index_mut(&mut self, r: ops::RangeTo<usize>) -> &mut BStr {
462 BStr::from_bytes_mut(&mut self.bytes[..r.end])
463 }
464 }
465
466 impl ops::IndexMut<ops::RangeToInclusive<usize>> for BStr {
467 #[inline]
468 fn index_mut(&mut self, r: ops::RangeToInclusive<usize>) -> &mut BStr {
469 BStr::from_bytes_mut(&mut self.bytes[..=r.end])
470 }
471 }
472
473 impl AsRef<[u8]> for BStr {
474 #[inline]
475 fn as_ref(&self) -> &[u8] {
476 self.as_bytes()
477 }
478 }
479
480 impl AsRef<BStr> for [u8] {
481 #[inline]
482 fn as_ref(&self) -> &BStr {
483 BStr::new(self)
484 }
485 }
486
487 impl AsRef<BStr> for str {
488 #[inline]
489 fn as_ref(&self) -> &BStr {
490 BStr::new(self)
491 }
492 }
493
494 impl AsMut<[u8]> for BStr {
495 #[inline]
496 fn as_mut(&mut self) -> &mut [u8] {
497 &mut self.bytes
498 }
499 }
500
501 impl AsMut<BStr> for [u8] {
502 #[inline]
503 fn as_mut(&mut self) -> &mut BStr {
504 BStr::new_mut(self)
505 }
506 }
507
508 impl<'a> Default for &'a BStr {
509 fn default() -> &'a BStr {
510 BStr::from_bytes(b"")
511 }
512 }
513
514 impl<'a> Default for &'a mut BStr {
515 fn default() -> &'a mut BStr {
516 BStr::from_bytes_mut(&mut [])
517 }
518 }
519
520 impl<'a> From<&'a [u8]> for &'a BStr {
521 #[inline]
522 fn from(s: &'a [u8]) -> &'a BStr {
523 BStr::from_bytes(s)
524 }
525 }
526
527 impl<'a> From<&'a str> for &'a BStr {
528 #[inline]
529 fn from(s: &'a str) -> &'a BStr {
530 BStr::from_bytes(s.as_bytes())
531 }
532 }
533
534 #[cfg(feature = "std")]
535 impl<'a> From<&'a BStr> for Cow<'a, BStr> {
536 #[inline]
537 fn from(s: &'a BStr) -> Cow<'a, BStr> {
538 Cow::Borrowed(s)
539 }
540 }
541
542 impl Eq for BStr {}
543
544 impl PartialEq<BStr> for BStr {
545 #[inline]
546 fn eq(&self, other: &BStr) -> bool {
547 self.as_bytes() == other.as_bytes()
548 }
549 }
550
551 impl_partial_eq!(BStr, [u8]);
552 impl_partial_eq!(BStr, &'a [u8]);
553 impl_partial_eq!(BStr, str);
554 impl_partial_eq!(BStr, &'a str);
555
556 #[cfg(feature = "std")]
557 impl_partial_eq!(BStr, Vec<u8>);
558 #[cfg(feature = "std")]
559 impl_partial_eq!(&'a BStr, Vec<u8>);
560 #[cfg(feature = "std")]
561 impl_partial_eq!(BStr, String);
562 #[cfg(feature = "std")]
563 impl_partial_eq!(&'a BStr, String);
564 #[cfg(feature = "std")]
565 impl_partial_eq_cow!(&'a BStr, Cow<'a, BStr>);
566 #[cfg(feature = "std")]
567 impl_partial_eq_cow!(&'a BStr, Cow<'a, str>);
568 #[cfg(feature = "std")]
569 impl_partial_eq_cow!(&'a BStr, Cow<'a, [u8]>);
570
571 impl PartialOrd for BStr {
572 #[inline]
573 fn partial_cmp(&self, other: &BStr) -> Option<Ordering> {
574 PartialOrd::partial_cmp(self.as_bytes(), other.as_bytes())
575 }
576 }
577
578 impl Ord for BStr {
579 #[inline]
580 fn cmp(&self, other: &BStr) -> Ordering {
581 self.partial_cmp(other).unwrap()
582 }
583 }
584
585 impl_partial_ord!(BStr, [u8]);
586 impl_partial_ord!(BStr, &'a [u8]);
587 impl_partial_ord!(BStr, str);
588 impl_partial_ord!(BStr, &'a str);
589
590 #[cfg(feature = "std")]
591 impl_partial_ord!(BStr, Vec<u8>);
592 #[cfg(feature = "std")]
593 impl_partial_ord!(&'a BStr, Vec<u8>);
594 #[cfg(feature = "std")]
595 impl_partial_ord!(BStr, String);
596 #[cfg(feature = "std")]
597 impl_partial_ord!(&'a BStr, String);
598 }
599
600 #[cfg(feature = "serde1-nostd")]
601 mod bstr_serde {
602 use core::fmt;
603
604 use serde::{
605 de::Error, de::Visitor, Deserialize, Deserializer, Serialize,
606 Serializer,
607 };
608
609 use bstr::BStr;
610
611 impl Serialize for BStr {
612 #[inline]
613 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
614 where
615 S: Serializer,
616 {
617 serializer.serialize_bytes(self.as_bytes())
618 }
619 }
620
621 impl<'a, 'de: 'a> Deserialize<'de> for &'a BStr {
622 #[inline]
623 fn deserialize<D>(deserializer: D) -> Result<&'a BStr, D::Error>
624 where
625 D: Deserializer<'de>,
626 {
627 struct BStrVisitor;
628
629 impl<'de> Visitor<'de> for BStrVisitor {
630 type Value = &'de BStr;
631
632 fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
633 f.write_str("a borrowed byte string")
634 }
635
636 #[inline]
637 fn visit_borrowed_bytes<E: Error>(
638 self,
639 value: &'de [u8],
640 ) -> Result<&'de BStr, E> {
641 Ok(BStr::new(value))
642 }
643
644 #[inline]
645 fn visit_borrowed_str<E: Error>(
646 self,
647 value: &'de str,
648 ) -> Result<&'de BStr, E> {
649 Ok(BStr::new(value))
650 }
651 }
652
653 deserializer.deserialize_bytes(BStrVisitor)
654 }
655 }
656 }
657
658 #[cfg(feature = "serde1")]
659 mod bstring_serde {
660 use std::cmp;
661 use std::fmt;
662
663 use serde::{
664 de::Error, de::SeqAccess, de::Visitor, Deserialize, Deserializer,
665 Serialize, Serializer,
666 };
667
668 use bstring::BString;
669
670 impl Serialize for BString {
671 #[inline]
672 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
673 where
674 S: Serializer,
675 {
676 serializer.serialize_bytes(self.as_bytes())
677 }
678 }
679
680 impl<'de> Deserialize<'de> for BString {
681 #[inline]
682 fn deserialize<D>(deserializer: D) -> Result<BString, D::Error>
683 where
684 D: Deserializer<'de>,
685 {
686 struct BStringVisitor;
687
688 impl<'de> Visitor<'de> for BStringVisitor {
689 type Value = BString;
690
691 fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
692 f.write_str("a byte string")
693 }
694
695 #[inline]
696 fn visit_seq<V: SeqAccess<'de>>(
697 self,
698 mut visitor: V,
699 ) -> Result<BString, V::Error> {
700 let len = cmp::min(visitor.size_hint().unwrap_or(0), 256);
701 let mut bytes = Vec::with_capacity(len);
702 while let Some(v) = visitor.next_element()? {
703 bytes.push(v);
704 }
705 Ok(BString::from(bytes))
706 }
707
708 #[inline]
709 fn visit_bytes<E: Error>(
710 self,
711 value: &[u8],
712 ) -> Result<BString, E> {
713 Ok(BString::from(value))
714 }
715
716 #[inline]
717 fn visit_byte_buf<E: Error>(
718 self,
719 value: Vec<u8>,
720 ) -> Result<BString, E> {
721 Ok(BString::from(value))
722 }
723
724 #[inline]
725 fn visit_str<E: Error>(
726 self,
727 value: &str,
728 ) -> Result<BString, E> {
729 Ok(BString::from(value))
730 }
731
732 #[inline]
733 fn visit_string<E: Error>(
734 self,
735 value: String,
736 ) -> Result<BString, E> {
737 Ok(BString::from(value))
738 }
739 }
740
741 deserializer.deserialize_byte_buf(BStringVisitor)
742 }
743 }
744 }
745
746 #[cfg(test)]
747 mod bstring_arbitrary {
748 use bstring::BString;
749
750 use quickcheck::{Arbitrary, Gen};
751
752 impl Arbitrary for BString {
753 fn arbitrary<G: Gen>(g: &mut G) -> BString {
754 BString::from(Vec::<u8>::arbitrary(g))
755 }
756
757 fn shrink(&self) -> Box<dyn Iterator<Item = BString>> {
758 Box::new(self.bytes.shrink().map(BString::from))
759 }
760 }
761 }