]>
git.proxmox.com Git - cargo.git/blob - vendor/bstr/src/impls.rs
1 macro_rules
! impl_partial_eq
{
2 ($lhs
:ty
, $rhs
:ty
) => {
3 impl<'a
, 'b
> PartialEq
<$rhs
> for $lhs
{
5 fn eq(&self, other
: &$rhs
) -> bool
{
6 let other
: &[u8] = other
.as_ref();
7 PartialEq
::eq(self.as_bytes(), other
)
11 impl<'a
, 'b
> PartialEq
<$lhs
> for $rhs
{
13 fn eq(&self, other
: &$lhs
) -> bool
{
14 let this
: &[u8] = self.as_ref();
15 PartialEq
::eq(this
, other
.as_bytes())
21 #[cfg(feature = "std")]
22 macro_rules
! impl_partial_eq_cow
{
23 ($lhs
:ty
, $rhs
:ty
) => {
24 impl<'a
, 'b
> PartialEq
<$rhs
> for $lhs
{
26 fn eq(&self, other
: &$rhs
) -> bool
{
27 let other
: &[u8] = (&**other
).as_ref();
28 PartialEq
::eq(self.as_bytes(), other
)
32 impl<'a
, 'b
> PartialEq
<$lhs
> for $rhs
{
34 fn eq(&self, other
: &$lhs
) -> bool
{
35 let this
: &[u8] = (&**other
).as_ref();
36 PartialEq
::eq(this
, other
.as_bytes())
42 macro_rules
! impl_partial_ord
{
43 ($lhs
:ty
, $rhs
:ty
) => {
44 impl<'a
, 'b
> PartialOrd
<$rhs
> for $lhs
{
46 fn partial_cmp(&self, other
: &$rhs
) -> Option
<Ordering
> {
47 let other
: &[u8] = other
.as_ref();
48 PartialOrd
::partial_cmp(self.as_bytes(), other
)
52 impl<'a
, 'b
> PartialOrd
<$lhs
> for $rhs
{
54 fn partial_cmp(&self, other
: &$lhs
) -> Option
<Ordering
> {
55 let this
: &[u8] = self.as_ref();
56 PartialOrd
::partial_cmp(this
, other
.as_bytes())
62 #[cfg(feature = "std")]
64 use std
::borrow
::{Borrow, Cow, ToOwned}
;
65 use std
::cmp
::Ordering
;
67 use std
::iter
::FromIterator
;
74 impl fmt
::Display
for BString
{
76 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
77 fmt
::Display
::fmt(self.as_bstr(), f
)
81 impl fmt
::Debug
for BString
{
83 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
84 fmt
::Debug
::fmt(self.as_bstr(), f
)
88 impl ops
::Deref
for BString
{
89 type Target
= Vec
<u8>;
92 fn deref(&self) -> &Vec
<u8> {
97 impl ops
::DerefMut
for BString
{
99 fn deref_mut(&mut self) -> &mut Vec
<u8> {
104 impl AsRef
<[u8]> for BString
{
106 fn as_ref(&self) -> &[u8] {
111 impl AsRef
<BStr
> for BString
{
113 fn as_ref(&self) -> &BStr
{
118 impl AsMut
<[u8]> for BString
{
120 fn as_mut(&mut self) -> &mut [u8] {
125 impl AsMut
<BStr
> for BString
{
127 fn as_mut(&mut self) -> &mut BStr
{
132 impl Borrow
<BStr
> for BString
{
134 fn borrow(&self) -> &BStr
{
139 impl ToOwned
for BStr
{
140 type Owned
= BString
;
143 fn to_owned(&self) -> BString
{
148 impl Default
for BString
{
149 fn default() -> BString
{
150 BString
::from(vec
![])
154 impl<'a
> From
<&'a
[u8]> for BString
{
156 fn from(s
: &'a
[u8]) -> BString
{
157 BString
::from(s
.to_vec())
161 impl From
<Vec
<u8>> for BString
{
163 fn from(s
: Vec
<u8>) -> BString
{
168 impl From
<BString
> for Vec
<u8> {
170 fn from(s
: BString
) -> Vec
<u8> {
175 impl<'a
> From
<&'a
str> for BString
{
177 fn from(s
: &'a
str) -> BString
{
178 BString
::from(s
.as_bytes().to_vec())
182 impl From
<String
> for BString
{
184 fn from(s
: String
) -> BString
{
185 BString
::from(s
.into_bytes())
189 impl<'a
> From
<&'a BStr
> for BString
{
191 fn from(s
: &'a BStr
) -> BString
{
192 BString
::from(s
.bytes
.to_vec())
196 impl<'a
> From
<BString
> for Cow
<'a
, BStr
> {
198 fn from(s
: BString
) -> Cow
<'a
, BStr
> {
203 impl FromIterator
<char> for BString
{
205 fn from_iter
<T
: IntoIterator
<Item
= char>>(iter
: T
) -> BString
{
206 BString
::from(iter
.into_iter().collect
::<String
>())
210 impl FromIterator
<u8> for BString
{
212 fn from_iter
<T
: IntoIterator
<Item
= u8>>(iter
: T
) -> BString
{
213 BString
::from(iter
.into_iter().collect
::<Vec
<u8>>())
217 impl<'a
> FromIterator
<&'a
str> for BString
{
219 fn from_iter
<T
: IntoIterator
<Item
= &'a
str>>(iter
: T
) -> BString
{
220 let mut buf
= vec
![];
228 impl<'a
> FromIterator
<&'a
[u8]> for BString
{
230 fn from_iter
<T
: IntoIterator
<Item
= &'a
[u8]>>(iter
: T
) -> BString
{
231 let mut buf
= vec
![];
239 impl<'a
> FromIterator
<&'a BStr
> for BString
{
241 fn from_iter
<T
: IntoIterator
<Item
= &'a BStr
>>(iter
: T
) -> BString
{
242 let mut buf
= vec
![];
250 impl FromIterator
<BString
> for BString
{
252 fn from_iter
<T
: IntoIterator
<Item
= BString
>>(iter
: T
) -> BString
{
253 let mut buf
= vec
![];
261 impl Eq
for BString {}
263 impl PartialEq
for BString
{
265 fn eq(&self, other
: &BString
) -> bool
{
266 &self[..] == &other
[..]
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
);
279 impl PartialOrd
for BString
{
281 fn partial_cmp(&self, other
: &BString
) -> Option
<Ordering
> {
282 PartialOrd
::partial_cmp(&self.bytes
, &other
.bytes
)
286 impl Ord
for BString
{
288 fn cmp(&self, other
: &BString
) -> Ordering
{
289 self.partial_cmp(other
).unwrap()
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
);
304 #[cfg(feature = "std")]
305 use std
::borrow
::Cow
;
307 use core
::cmp
::Ordering
;
312 use ext_slice
::ByteSlice
;
314 impl fmt
::Display
for BStr
{
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}")?
;
327 impl fmt
::Debug
for BStr
{
329 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
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
)?
;
337 write
!(f
, "{}", ch
.escape_debug())?
;
345 impl ops
::Deref
for BStr
{
349 fn deref(&self) -> &[u8] {
354 impl ops
::DerefMut
for BStr
{
356 fn deref_mut(&mut self) -> &mut [u8] {
361 impl ops
::Index
<usize> for BStr
{
365 fn index(&self, idx
: usize) -> &u8 {
366 &self.as_bytes()[idx
]
370 impl ops
::Index
<ops
::RangeFull
> for BStr
{
374 fn index(&self, _
: ops
::RangeFull
) -> &BStr
{
379 impl ops
::Index
<ops
::Range
<usize>> for BStr
{
383 fn index(&self, r
: ops
::Range
<usize>) -> &BStr
{
384 BStr
::new(&self.as_bytes()[r
.start
..r
.end
])
388 impl ops
::Index
<ops
::RangeInclusive
<usize>> for BStr
{
392 fn index(&self, r
: ops
::RangeInclusive
<usize>) -> &BStr
{
393 BStr
::new(&self.as_bytes()[*r
.start()..=*r
.end()])
397 impl ops
::Index
<ops
::RangeFrom
<usize>> for BStr
{
401 fn index(&self, r
: ops
::RangeFrom
<usize>) -> &BStr
{
402 BStr
::new(&self.as_bytes()[r
.start
..])
406 impl ops
::Index
<ops
::RangeTo
<usize>> for BStr
{
410 fn index(&self, r
: ops
::RangeTo
<usize>) -> &BStr
{
411 BStr
::new(&self.as_bytes()[..r
.end
])
415 impl ops
::Index
<ops
::RangeToInclusive
<usize>> for BStr
{
419 fn index(&self, r
: ops
::RangeToInclusive
<usize>) -> &BStr
{
420 BStr
::new(&self.as_bytes()[..=r
.end
])
424 impl ops
::IndexMut
<usize> for BStr
{
426 fn index_mut(&mut self, idx
: usize) -> &mut u8 {
431 impl ops
::IndexMut
<ops
::RangeFull
> for BStr
{
433 fn index_mut(&mut self, _
: ops
::RangeFull
) -> &mut BStr
{
438 impl ops
::IndexMut
<ops
::Range
<usize>> for BStr
{
440 fn index_mut(&mut self, r
: ops
::Range
<usize>) -> &mut BStr
{
441 BStr
::from_bytes_mut(&mut self.bytes
[r
.start
..r
.end
])
445 impl ops
::IndexMut
<ops
::RangeInclusive
<usize>> for BStr
{
447 fn index_mut(&mut self, r
: ops
::RangeInclusive
<usize>) -> &mut BStr
{
448 BStr
::from_bytes_mut(&mut self.bytes
[*r
.start()..=*r
.end()])
452 impl ops
::IndexMut
<ops
::RangeFrom
<usize>> for BStr
{
454 fn index_mut(&mut self, r
: ops
::RangeFrom
<usize>) -> &mut BStr
{
455 BStr
::from_bytes_mut(&mut self.bytes
[r
.start
..])
459 impl ops
::IndexMut
<ops
::RangeTo
<usize>> for BStr
{
461 fn index_mut(&mut self, r
: ops
::RangeTo
<usize>) -> &mut BStr
{
462 BStr
::from_bytes_mut(&mut self.bytes
[..r
.end
])
466 impl ops
::IndexMut
<ops
::RangeToInclusive
<usize>> for BStr
{
468 fn index_mut(&mut self, r
: ops
::RangeToInclusive
<usize>) -> &mut BStr
{
469 BStr
::from_bytes_mut(&mut self.bytes
[..=r
.end
])
473 impl AsRef
<[u8]> for BStr
{
475 fn as_ref(&self) -> &[u8] {
480 impl AsRef
<BStr
> for [u8] {
482 fn as_ref(&self) -> &BStr
{
487 impl AsRef
<BStr
> for str {
489 fn as_ref(&self) -> &BStr
{
494 impl AsMut
<[u8]> for BStr
{
496 fn as_mut(&mut self) -> &mut [u8] {
501 impl AsMut
<BStr
> for [u8] {
503 fn as_mut(&mut self) -> &mut BStr
{
508 impl<'a
> Default
for &'a BStr
{
509 fn default() -> &'a BStr
{
510 BStr
::from_bytes(b
"")
514 impl<'a
> Default
for &'a
mut BStr
{
515 fn default() -> &'a
mut BStr
{
516 BStr
::from_bytes_mut(&mut [])
520 impl<'a
> From
<&'a
[u8]> for &'a BStr
{
522 fn from(s
: &'a
[u8]) -> &'a BStr
{
527 impl<'a
> From
<&'a
str> for &'a BStr
{
529 fn from(s
: &'a
str) -> &'a BStr
{
530 BStr
::from_bytes(s
.as_bytes())
534 #[cfg(feature = "std")]
535 impl<'a
> From
<&'a BStr
> for Cow
<'a
, BStr
> {
537 fn from(s
: &'a BStr
) -> Cow
<'a
, BStr
> {
544 impl PartialEq
<BStr
> for BStr
{
546 fn eq(&self, other
: &BStr
) -> bool
{
547 self.as_bytes() == other
.as_bytes()
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);
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]>);
571 impl PartialOrd
for BStr
{
573 fn partial_cmp(&self, other
: &BStr
) -> Option
<Ordering
> {
574 PartialOrd
::partial_cmp(self.as_bytes(), other
.as_bytes())
580 fn cmp(&self, other
: &BStr
) -> Ordering
{
581 self.partial_cmp(other
).unwrap()
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);
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
);
600 #[cfg(feature = "serde1-nostd")]
605 de
::Error
, de
::Visitor
, Deserialize
, Deserializer
, Serialize
,
611 impl Serialize
for BStr
{
613 fn serialize
<S
>(&self, serializer
: S
) -> Result
<S
::Ok
, S
::Error
>
617 serializer
.serialize_bytes(self.as_bytes())
621 impl<'a
, 'de
: 'a
> Deserialize
<'de
> for &'a BStr
{
623 fn deserialize
<D
>(deserializer
: D
) -> Result
<&'a BStr
, D
::Error
>
625 D
: Deserializer
<'de
>,
629 impl<'de
> Visitor
<'de
> for BStrVisitor
{
630 type Value
= &'de BStr
;
632 fn expecting(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
633 f
.write_str("a borrowed byte string")
637 fn visit_borrowed_bytes
<E
: Error
>(
640 ) -> Result
<&'de BStr
, E
> {
645 fn visit_borrowed_str
<E
: Error
>(
648 ) -> Result
<&'de BStr
, E
> {
653 deserializer
.deserialize_bytes(BStrVisitor
)
658 #[cfg(feature = "serde1")]
664 de
::Error
, de
::SeqAccess
, de
::Visitor
, Deserialize
, Deserializer
,
665 Serialize
, Serializer
,
668 use bstring
::BString
;
670 impl Serialize
for BString
{
672 fn serialize
<S
>(&self, serializer
: S
) -> Result
<S
::Ok
, S
::Error
>
676 serializer
.serialize_bytes(self.as_bytes())
680 impl<'de
> Deserialize
<'de
> for BString
{
682 fn deserialize
<D
>(deserializer
: D
) -> Result
<BString
, D
::Error
>
684 D
: Deserializer
<'de
>,
686 struct BStringVisitor
;
688 impl<'de
> Visitor
<'de
> for BStringVisitor
{
689 type Value
= BString
;
691 fn expecting(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
692 f
.write_str("a byte string")
696 fn visit_seq
<V
: SeqAccess
<'de
>>(
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()?
{
705 Ok(BString
::from(bytes
))
709 fn visit_bytes
<E
: Error
>(
712 ) -> Result
<BString
, E
> {
713 Ok(BString
::from(value
))
717 fn visit_byte_buf
<E
: Error
>(
720 ) -> Result
<BString
, E
> {
721 Ok(BString
::from(value
))
725 fn visit_str
<E
: Error
>(
728 ) -> Result
<BString
, E
> {
729 Ok(BString
::from(value
))
733 fn visit_string
<E
: Error
>(
736 ) -> Result
<BString
, E
> {
737 Ok(BString
::from(value
))
741 deserializer
.deserialize_byte_buf(BStringVisitor
)
747 mod bstring_arbitrary
{
748 use bstring
::BString
;
750 use quickcheck
::{Arbitrary, Gen}
;
752 impl Arbitrary
for BString
{
753 fn arbitrary
<G
: Gen
>(g
: &mut G
) -> BString
{
754 BString
::from(Vec
::<u8>::arbitrary(g
))
757 fn shrink(&self) -> Box
<dyn Iterator
<Item
= BString
>> {
758 Box
::new(self.bytes
.shrink().map(BString
::from
))