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 = "alloc")]
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
, self.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 = "alloc")]
65 cmp
::Ordering
, convert
::TryFrom
, fmt
, iter
::FromIterator
, ops
,
69 borrow
::{Borrow, Cow, ToOwned}
,
76 bstr
::BStr
, bstring
::BString
, ext_slice
::ByteSlice
, ext_vec
::ByteVec
,
79 impl fmt
::Display
for BString
{
81 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
82 fmt
::Display
::fmt(self.as_bstr(), f
)
86 impl fmt
::Debug
for BString
{
88 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
89 fmt
::Debug
::fmt(self.as_bstr(), f
)
93 impl ops
::Deref
for BString
{
94 type Target
= Vec
<u8>;
97 fn deref(&self) -> &Vec
<u8> {
102 impl ops
::DerefMut
for BString
{
104 fn deref_mut(&mut self) -> &mut Vec
<u8> {
109 impl AsRef
<[u8]> for BString
{
111 fn as_ref(&self) -> &[u8] {
116 impl AsRef
<BStr
> for BString
{
118 fn as_ref(&self) -> &BStr
{
123 impl AsMut
<[u8]> for BString
{
125 fn as_mut(&mut self) -> &mut [u8] {
130 impl AsMut
<BStr
> for BString
{
132 fn as_mut(&mut self) -> &mut BStr
{
137 impl Borrow
<BStr
> for BString
{
139 fn borrow(&self) -> &BStr
{
144 impl ToOwned
for BStr
{
145 type Owned
= BString
;
148 fn to_owned(&self) -> BString
{
153 impl Default
for BString
{
154 fn default() -> BString
{
155 BString
::from(vec
![])
159 impl<'a
, const N
: usize> From
<&'a
[u8; N
]> for BString
{
161 fn from(s
: &'a
[u8; N
]) -> BString
{
162 BString
::from(&s
[..])
166 impl<const N
: usize> From
<[u8; N
]> for BString
{
168 fn from(s
: [u8; N
]) -> BString
{
169 BString
::from(&s
[..])
173 impl<'a
> From
<&'a
[u8]> for BString
{
175 fn from(s
: &'a
[u8]) -> BString
{
176 BString
::from(s
.to_vec())
180 impl From
<Vec
<u8>> for BString
{
182 fn from(s
: Vec
<u8>) -> BString
{
187 impl From
<BString
> for Vec
<u8> {
189 fn from(s
: BString
) -> Vec
<u8> {
194 impl<'a
> From
<&'a
str> for BString
{
196 fn from(s
: &'a
str) -> BString
{
197 BString
::from(s
.as_bytes().to_vec())
201 impl From
<String
> for BString
{
203 fn from(s
: String
) -> BString
{
204 BString
::from(s
.into_bytes())
208 impl<'a
> From
<&'a BStr
> for BString
{
210 fn from(s
: &'a BStr
) -> BString
{
211 BString
::from(s
.bytes
.to_vec())
215 impl<'a
> From
<BString
> for Cow
<'a
, BStr
> {
217 fn from(s
: BString
) -> Cow
<'a
, BStr
> {
222 impl TryFrom
<BString
> for String
{
223 type Error
= crate::FromUtf8Error
;
226 fn try_from(s
: BString
) -> Result
<String
, crate::FromUtf8Error
> {
227 s
.into_vec().into_string()
231 impl<'a
> TryFrom
<&'a BString
> for &'a
str {
232 type Error
= crate::Utf8Error
;
235 fn try_from(s
: &'a BString
) -> Result
<&'a
str, crate::Utf8Error
> {
236 s
.as_bytes().to_str()
240 impl FromIterator
<char> for BString
{
242 fn from_iter
<T
: IntoIterator
<Item
= char>>(iter
: T
) -> BString
{
243 BString
::from(iter
.into_iter().collect
::<String
>())
247 impl FromIterator
<u8> for BString
{
249 fn from_iter
<T
: IntoIterator
<Item
= u8>>(iter
: T
) -> BString
{
250 BString
::from(iter
.into_iter().collect
::<Vec
<u8>>())
254 impl<'a
> FromIterator
<&'a
str> for BString
{
256 fn from_iter
<T
: IntoIterator
<Item
= &'a
str>>(iter
: T
) -> BString
{
257 let mut buf
= vec
![];
265 impl<'a
> FromIterator
<&'a
[u8]> for BString
{
267 fn from_iter
<T
: IntoIterator
<Item
= &'a
[u8]>>(iter
: T
) -> BString
{
268 let mut buf
= vec
![];
276 impl<'a
> FromIterator
<&'a BStr
> for BString
{
278 fn from_iter
<T
: IntoIterator
<Item
= &'a BStr
>>(iter
: T
) -> BString
{
279 let mut buf
= vec
![];
287 impl FromIterator
<BString
> for BString
{
289 fn from_iter
<T
: IntoIterator
<Item
= BString
>>(iter
: T
) -> BString
{
290 let mut buf
= vec
![];
298 impl Eq
for BString {}
300 impl PartialEq
for BString
{
302 fn eq(&self, other
: &BString
) -> bool
{
303 &self[..] == &other
[..]
307 impl_partial_eq
!(BString
, Vec
<u8>);
308 impl_partial_eq
!(BString
, [u8]);
309 impl_partial_eq
!(BString
, &'a
[u8]);
310 impl_partial_eq
!(BString
, String
);
311 impl_partial_eq
!(BString
, str);
312 impl_partial_eq
!(BString
, &'a
str);
313 impl_partial_eq
!(BString
, BStr
);
314 impl_partial_eq
!(BString
, &'a BStr
);
316 impl PartialOrd
for BString
{
318 fn partial_cmp(&self, other
: &BString
) -> Option
<Ordering
> {
319 PartialOrd
::partial_cmp(self.as_bytes(), other
.as_bytes())
323 impl Ord
for BString
{
325 fn cmp(&self, other
: &BString
) -> Ordering
{
326 self.partial_cmp(other
).unwrap()
330 impl_partial_ord
!(BString
, Vec
<u8>);
331 impl_partial_ord
!(BString
, [u8]);
332 impl_partial_ord
!(BString
, &'a
[u8]);
333 impl_partial_ord
!(BString
, String
);
334 impl_partial_ord
!(BString
, str);
335 impl_partial_ord
!(BString
, &'a
str);
336 impl_partial_ord
!(BString
, BStr
);
337 impl_partial_ord
!(BString
, &'a BStr
);
341 use core
::{cmp::Ordering, convert::TryFrom, fmt, ops}
;
343 #[cfg(feature = "alloc")]
344 use alloc
::{borrow::Cow, boxed::Box, string::String, vec::Vec}
;
346 use crate::{bstr::BStr, ext_slice::ByteSlice}
;
348 impl fmt
::Display
for BStr
{
350 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
351 /// Write the given bstr (lossily) to the given formatter.
353 f
: &mut fmt
::Formatter
<'_
>,
355 ) -> Result
<(), fmt
::Error
> {
356 for chunk
in bstr
.utf8_chunks() {
357 f
.write_str(chunk
.valid())?
;
358 if !chunk
.invalid().is_empty() {
359 f
.write_str("\u{FFFD}")?
;
365 /// Write 'num' fill characters to the given formatter.
367 f
: &mut fmt
::Formatter
<'_
>,
372 f
.write_fmt(format_args
!("{}", fill
))?
;
377 if let Some(align
) = f
.align() {
378 let width
= f
.width().unwrap_or(0);
379 let nchars
= self.chars().count();
380 let remaining_pads
= width
.saturating_sub(nchars
);
382 fmt
::Alignment
::Left
=> {
383 write_bstr(f
, self)?
;
384 write_pads(f
, remaining_pads
)?
;
386 fmt
::Alignment
::Right
=> {
387 write_pads(f
, remaining_pads
)?
;
388 write_bstr(f
, self)?
;
390 fmt
::Alignment
::Center
=> {
391 let half
= remaining_pads
/ 2;
392 let second_half
= if remaining_pads
% 2 == 0 {
397 write_pads(f
, half
)?
;
398 write_bstr(f
, self)?
;
399 write_pads(f
, second_half
)?
;
404 write_bstr(f
, self)?
;
410 impl fmt
::Debug
for BStr
{
412 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
414 for (s
, e
, ch
) in self.char_indices() {
416 '
\0'
=> write
!(f
, "\\0")?
,
418 let bytes
= self[s
..e
].as_bytes();
419 if bytes
== b
"\xEF\xBF\xBD" {
420 write
!(f
, "{}", ch
.escape_debug())?
;
422 for &b
in self[s
..e
].as_bytes() {
423 write
!(f
, r
"\x{:02X}", b
)?
;
427 // ASCII control characters except \0, \n, \r, \t
433 write
!(f
, "\\x{:02x}", ch
as u32)?
;
435 '
\n'
| '
\r'
| '
\t'
| _
=> {
436 write
!(f
, "{}", ch
.escape_debug())?
;
445 impl ops
::Deref
for BStr
{
449 fn deref(&self) -> &[u8] {
454 impl ops
::DerefMut
for BStr
{
456 fn deref_mut(&mut self) -> &mut [u8] {
461 impl ops
::Index
<usize> for BStr
{
465 fn index(&self, idx
: usize) -> &u8 {
466 &self.as_bytes()[idx
]
470 impl ops
::Index
<ops
::RangeFull
> for BStr
{
474 fn index(&self, _
: ops
::RangeFull
) -> &BStr
{
479 impl ops
::Index
<ops
::Range
<usize>> for BStr
{
483 fn index(&self, r
: ops
::Range
<usize>) -> &BStr
{
484 BStr
::new(&self.as_bytes()[r
.start
..r
.end
])
488 impl ops
::Index
<ops
::RangeInclusive
<usize>> for BStr
{
492 fn index(&self, r
: ops
::RangeInclusive
<usize>) -> &BStr
{
493 BStr
::new(&self.as_bytes()[*r
.start()..=*r
.end()])
497 impl ops
::Index
<ops
::RangeFrom
<usize>> for BStr
{
501 fn index(&self, r
: ops
::RangeFrom
<usize>) -> &BStr
{
502 BStr
::new(&self.as_bytes()[r
.start
..])
506 impl ops
::Index
<ops
::RangeTo
<usize>> for BStr
{
510 fn index(&self, r
: ops
::RangeTo
<usize>) -> &BStr
{
511 BStr
::new(&self.as_bytes()[..r
.end
])
515 impl ops
::Index
<ops
::RangeToInclusive
<usize>> for BStr
{
519 fn index(&self, r
: ops
::RangeToInclusive
<usize>) -> &BStr
{
520 BStr
::new(&self.as_bytes()[..=r
.end
])
524 impl ops
::IndexMut
<usize> for BStr
{
526 fn index_mut(&mut self, idx
: usize) -> &mut u8 {
531 impl ops
::IndexMut
<ops
::RangeFull
> for BStr
{
533 fn index_mut(&mut self, _
: ops
::RangeFull
) -> &mut BStr
{
538 impl ops
::IndexMut
<ops
::Range
<usize>> for BStr
{
540 fn index_mut(&mut self, r
: ops
::Range
<usize>) -> &mut BStr
{
541 BStr
::from_bytes_mut(&mut self.bytes
[r
.start
..r
.end
])
545 impl ops
::IndexMut
<ops
::RangeInclusive
<usize>> for BStr
{
547 fn index_mut(&mut self, r
: ops
::RangeInclusive
<usize>) -> &mut BStr
{
548 BStr
::from_bytes_mut(&mut self.bytes
[*r
.start()..=*r
.end()])
552 impl ops
::IndexMut
<ops
::RangeFrom
<usize>> for BStr
{
554 fn index_mut(&mut self, r
: ops
::RangeFrom
<usize>) -> &mut BStr
{
555 BStr
::from_bytes_mut(&mut self.bytes
[r
.start
..])
559 impl ops
::IndexMut
<ops
::RangeTo
<usize>> for BStr
{
561 fn index_mut(&mut self, r
: ops
::RangeTo
<usize>) -> &mut BStr
{
562 BStr
::from_bytes_mut(&mut self.bytes
[..r
.end
])
566 impl ops
::IndexMut
<ops
::RangeToInclusive
<usize>> for BStr
{
568 fn index_mut(&mut self, r
: ops
::RangeToInclusive
<usize>) -> &mut BStr
{
569 BStr
::from_bytes_mut(&mut self.bytes
[..=r
.end
])
573 impl AsRef
<[u8]> for BStr
{
575 fn as_ref(&self) -> &[u8] {
580 impl AsRef
<BStr
> for BStr
{
582 fn as_ref(&self) -> &BStr
{
587 impl AsRef
<BStr
> for [u8] {
589 fn as_ref(&self) -> &BStr
{
594 impl AsRef
<BStr
> for str {
596 fn as_ref(&self) -> &BStr
{
601 impl AsMut
<[u8]> for BStr
{
603 fn as_mut(&mut self) -> &mut [u8] {
608 impl AsMut
<BStr
> for [u8] {
610 fn as_mut(&mut self) -> &mut BStr
{
615 impl<'a
> Default
for &'a BStr
{
616 fn default() -> &'a BStr
{
617 BStr
::from_bytes(b
"")
621 impl<'a
> Default
for &'a
mut BStr
{
622 fn default() -> &'a
mut BStr
{
623 BStr
::from_bytes_mut(&mut [])
627 impl<'a
, const N
: usize> From
<&'a
[u8; N
]> for &'a BStr
{
629 fn from(s
: &'a
[u8; N
]) -> &'a BStr
{
634 impl<'a
> From
<&'a
[u8]> for &'a BStr
{
636 fn from(s
: &'a
[u8]) -> &'a BStr
{
641 impl<'a
> From
<&'a BStr
> for &'a
[u8] {
643 fn from(s
: &'a BStr
) -> &'a
[u8] {
648 impl<'a
> From
<&'a
str> for &'a BStr
{
650 fn from(s
: &'a
str) -> &'a BStr
{
651 BStr
::from_bytes(s
.as_bytes())
655 #[cfg(feature = "alloc")]
656 impl<'a
> From
<&'a BStr
> for Cow
<'a
, BStr
> {
658 fn from(s
: &'a BStr
) -> Cow
<'a
, BStr
> {
663 #[cfg(feature = "alloc")]
664 impl From
<Box
<[u8]>> for Box
<BStr
> {
666 fn from(s
: Box
<[u8]>) -> Box
<BStr
> {
667 BStr
::from_boxed_bytes(s
)
671 #[cfg(feature = "alloc")]
672 impl From
<Box
<BStr
>> for Box
<[u8]> {
674 fn from(s
: Box
<BStr
>) -> Box
<[u8]> {
675 BStr
::into_boxed_bytes(s
)
679 impl<'a
> TryFrom
<&'a BStr
> for &'a
str {
680 type Error
= crate::Utf8Error
;
683 fn try_from(s
: &'a BStr
) -> Result
<&'a
str, crate::Utf8Error
> {
684 s
.as_bytes().to_str()
688 #[cfg(feature = "alloc")]
689 impl<'a
> TryFrom
<&'a BStr
> for String
{
690 type Error
= crate::Utf8Error
;
693 fn try_from(s
: &'a BStr
) -> Result
<String
, crate::Utf8Error
> {
694 Ok(s
.as_bytes().to_str()?
.into())
698 #[cfg(feature = "alloc")]
699 impl Clone
for Box
<BStr
> {
701 fn clone(&self) -> Self {
702 BStr
::from_boxed_bytes(self.as_bytes().into())
708 impl PartialEq
<BStr
> for BStr
{
710 fn eq(&self, other
: &BStr
) -> bool
{
711 self.as_bytes() == other
.as_bytes()
715 impl_partial_eq
!(BStr
, [u8]);
716 impl_partial_eq
!(BStr
, &'a
[u8]);
717 impl_partial_eq
!(BStr
, str);
718 impl_partial_eq
!(BStr
, &'a
str);
720 #[cfg(feature = "alloc")]
721 impl_partial_eq
!(BStr
, Vec
<u8>);
722 #[cfg(feature = "alloc")]
723 impl_partial_eq
!(&'a BStr
, Vec
<u8>);
724 #[cfg(feature = "alloc")]
725 impl_partial_eq
!(BStr
, String
);
726 #[cfg(feature = "alloc")]
727 impl_partial_eq
!(&'a BStr
, String
);
728 #[cfg(feature = "alloc")]
729 impl_partial_eq_cow
!(&'a BStr
, Cow
<'a
, BStr
>);
730 #[cfg(feature = "alloc")]
731 impl_partial_eq_cow
!(&'a BStr
, Cow
<'a
, str>);
732 #[cfg(feature = "alloc")]
733 impl_partial_eq_cow
!(&'a BStr
, Cow
<'a
, [u8]>);
735 impl PartialOrd
for BStr
{
737 fn partial_cmp(&self, other
: &BStr
) -> Option
<Ordering
> {
738 PartialOrd
::partial_cmp(self.as_bytes(), other
.as_bytes())
744 fn cmp(&self, other
: &BStr
) -> Ordering
{
745 self.partial_cmp(other
).unwrap()
749 impl_partial_ord
!(BStr
, [u8]);
750 impl_partial_ord
!(BStr
, &'a
[u8]);
751 impl_partial_ord
!(BStr
, str);
752 impl_partial_ord
!(BStr
, &'a
str);
754 #[cfg(feature = "alloc")]
755 impl_partial_ord
!(BStr
, Vec
<u8>);
756 #[cfg(feature = "alloc")]
757 impl_partial_ord
!(&'a BStr
, Vec
<u8>);
758 #[cfg(feature = "alloc")]
759 impl_partial_ord
!(BStr
, String
);
760 #[cfg(feature = "alloc")]
761 impl_partial_ord
!(&'a BStr
, String
);
764 #[cfg(feature = "serde")]
769 de
::Error
, de
::Visitor
, Deserialize
, Deserializer
, Serialize
,
773 use crate::bstr
::BStr
;
775 impl Serialize
for BStr
{
777 fn serialize
<S
>(&self, serializer
: S
) -> Result
<S
::Ok
, S
::Error
>
781 serializer
.serialize_bytes(self.as_bytes())
785 impl<'a
, 'de
: 'a
> Deserialize
<'de
> for &'a BStr
{
787 fn deserialize
<D
>(deserializer
: D
) -> Result
<&'a BStr
, D
::Error
>
789 D
: Deserializer
<'de
>,
793 impl<'de
> Visitor
<'de
> for BStrVisitor
{
794 type Value
= &'de BStr
;
796 fn expecting(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
797 f
.write_str("a borrowed byte string")
801 fn visit_borrowed_bytes
<E
: Error
>(
804 ) -> Result
<&'de BStr
, E
> {
809 fn visit_borrowed_str
<E
: Error
>(
812 ) -> Result
<&'de BStr
, E
> {
817 deserializer
.deserialize_bytes(BStrVisitor
)
822 #[cfg(all(feature = "serde", feature = "alloc"))]
824 use core
::{cmp, fmt}
;
826 use alloc
::{boxed::Box, string::String, vec::Vec}
;
829 de
::Error
, de
::SeqAccess
, de
::Visitor
, Deserialize
, Deserializer
,
830 Serialize
, Serializer
,
833 use crate::{bstr::BStr, bstring::BString}
;
835 impl Serialize
for BString
{
837 fn serialize
<S
>(&self, serializer
: S
) -> Result
<S
::Ok
, S
::Error
>
841 serializer
.serialize_bytes(self.as_bytes())
845 impl<'de
> Deserialize
<'de
> for BString
{
847 fn deserialize
<D
>(deserializer
: D
) -> Result
<BString
, D
::Error
>
849 D
: Deserializer
<'de
>,
851 struct BStringVisitor
;
853 impl<'de
> Visitor
<'de
> for BStringVisitor
{
854 type Value
= BString
;
856 fn expecting(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
857 f
.write_str("a byte string")
861 fn visit_seq
<V
: SeqAccess
<'de
>>(
864 ) -> Result
<BString
, V
::Error
> {
865 let len
= cmp
::min(visitor
.size_hint().unwrap_or(0), 256);
866 let mut bytes
= Vec
::with_capacity(len
);
867 while let Some(v
) = visitor
.next_element()?
{
870 Ok(BString
::from(bytes
))
874 fn visit_bytes
<E
: Error
>(
877 ) -> Result
<BString
, E
> {
878 Ok(BString
::from(value
))
882 fn visit_byte_buf
<E
: Error
>(
885 ) -> Result
<BString
, E
> {
886 Ok(BString
::from(value
))
890 fn visit_str
<E
: Error
>(
893 ) -> Result
<BString
, E
> {
894 Ok(BString
::from(value
))
898 fn visit_string
<E
: Error
>(
901 ) -> Result
<BString
, E
> {
902 Ok(BString
::from(value
))
906 deserializer
.deserialize_byte_buf(BStringVisitor
)
910 impl<'de
> Deserialize
<'de
> for Box
<BStr
> {
912 fn deserialize
<D
>(deserializer
: D
) -> Result
<Box
<BStr
>, D
::Error
>
914 D
: Deserializer
<'de
>,
916 struct BoxedBStrVisitor
;
918 impl<'de
> Visitor
<'de
> for BoxedBStrVisitor
{
919 type Value
= Box
<BStr
>;
921 fn expecting(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
922 f
.write_str("a boxed byte string")
926 fn visit_seq
<V
: SeqAccess
<'de
>>(
929 ) -> Result
<Box
<BStr
>, V
::Error
> {
930 let len
= cmp
::min(visitor
.size_hint().unwrap_or(0), 256);
931 let mut bytes
= Vec
::with_capacity(len
);
932 while let Some(v
) = visitor
.next_element()?
{
935 Ok(BStr
::from_boxed_bytes(bytes
.into_boxed_slice()))
939 fn visit_bytes
<E
: Error
>(
942 ) -> Result
<Box
<BStr
>, E
> {
943 Ok(BStr
::from_boxed_bytes(
944 value
.to_vec().into_boxed_slice(),
949 fn visit_byte_buf
<E
: Error
>(
952 ) -> Result
<Box
<BStr
>, E
> {
953 Ok(BStr
::from_boxed_bytes(value
.into_boxed_slice()))
957 fn visit_str
<E
: Error
>(
960 ) -> Result
<Box
<BStr
>, E
> {
961 Ok(BStr
::from_boxed_bytes(
962 value
.as_bytes().to_vec().into_boxed_slice(),
967 fn visit_string
<E
: Error
>(
970 ) -> Result
<Box
<BStr
>, E
> {
971 Ok(BStr
::from_boxed_bytes(
972 value
.into_bytes().into_boxed_slice(),
977 deserializer
.deserialize_byte_buf(BoxedBStrVisitor
)
982 #[cfg(all(test, feature = "std"))]
985 use crate::bstring
::BString
;
986 use crate::ByteSlice
;
990 assert_eq
!(&format
!("{}", &b
"abc".as_bstr()), "abc");
991 assert_eq
!(&format
!("{}", &b
"\xf0\x28\x8c\xbc".as_bstr()), "�(��");
995 fn width_bigger_than_bstr() {
996 assert_eq
!(&format
!("{:<7}!", &b
"abc".as_bstr()), "abc !");
997 assert_eq
!(&format
!("{:>7}!", &b
"abc".as_bstr()), " abc!");
998 assert_eq
!(&format
!("{:^7}!", &b
"abc".as_bstr()), " abc !");
999 assert_eq
!(&format
!("{:^6}!", &b
"abc".as_bstr()), " abc !");
1000 assert_eq
!(&format
!("{:-<7}!", &b
"abc".as_bstr()), "abc----!");
1001 assert_eq
!(&format
!("{:->7}!", &b
"abc".as_bstr()), "----abc!");
1002 assert_eq
!(&format
!("{:-^7}!", &b
"abc".as_bstr()), "--abc--!");
1003 assert_eq
!(&format
!("{:-^6}!", &b
"abc".as_bstr()), "-abc--!");
1006 &format
!("{:<7}!", &b
"\xf0\x28\x8c\xbc".as_bstr()),
1010 &format
!("{:>7}!", &b
"\xf0\x28\x8c\xbc".as_bstr()),
1014 &format
!("{:^7}!", &b
"\xf0\x28\x8c\xbc".as_bstr()),
1018 &format
!("{:^6}!", &b
"\xf0\x28\x8c\xbc".as_bstr()),
1023 &format
!("{:-<7}!", &b
"\xf0\x28\x8c\xbc".as_bstr()),
1027 &format
!("{:->7}!", &b
"\xf0\x28\x8c\xbc".as_bstr()),
1031 &format
!("{:-^7}!", &b
"\xf0\x28\x8c\xbc".as_bstr()),
1035 &format
!("{:-^6}!", &b
"\xf0\x28\x8c\xbc".as_bstr()),
1041 fn width_lesser_than_bstr() {
1042 assert_eq
!(&format
!("{:<2}!", &b
"abc".as_bstr()), "abc!");
1043 assert_eq
!(&format
!("{:>2}!", &b
"abc".as_bstr()), "abc!");
1044 assert_eq
!(&format
!("{:^2}!", &b
"abc".as_bstr()), "abc!");
1045 assert_eq
!(&format
!("{:-<2}!", &b
"abc".as_bstr()), "abc!");
1046 assert_eq
!(&format
!("{:->2}!", &b
"abc".as_bstr()), "abc!");
1047 assert_eq
!(&format
!("{:-^2}!", &b
"abc".as_bstr()), "abc!");
1050 &format
!("{:<3}!", &b
"\xf0\x28\x8c\xbc".as_bstr()),
1054 &format
!("{:>3}!", &b
"\xf0\x28\x8c\xbc".as_bstr()),
1058 &format
!("{:^3}!", &b
"\xf0\x28\x8c\xbc".as_bstr()),
1062 &format
!("{:^2}!", &b
"\xf0\x28\x8c\xbc".as_bstr()),
1067 &format
!("{:-<3}!", &b
"\xf0\x28\x8c\xbc".as_bstr()),
1071 &format
!("{:->3}!", &b
"\xf0\x28\x8c\xbc".as_bstr()),
1075 &format
!("{:-^3}!", &b
"\xf0\x28\x8c\xbc".as_bstr()),
1079 &format
!("{:-^2}!", &b
"\xf0\x28\x8c\xbc".as_bstr()),
1085 quickcheck
::quickcheck
! {
1086 fn total_length(bstr
: BString
) -> bool
{
1087 let size
= bstr
.chars().count();
1088 format
!("{:<1$}", bstr
.as_bstr(), size
).chars().count() >= size
1093 #[cfg(all(test, feature = "alloc"))]
1094 mod bstring_arbitrary
{
1095 use crate::bstring
::BString
;
1097 use quickcheck
::{Arbitrary, Gen}
;
1099 impl Arbitrary
for BString
{
1100 fn arbitrary(g
: &mut Gen
) -> BString
{
1101 BString
::from(Vec
::<u8>::arbitrary(g
))
1104 fn shrink(&self) -> Box
<dyn Iterator
<Item
= BString
>> {
1105 Box
::new(self.as_vec().shrink().map(BString
::from
))
1111 #[cfg(feature = "std")]
1113 use crate::{ByteSlice, B}
;
1116 r
#""\0\0\0 ftypisom\0\0\x02\0isomiso2avc1mp""#,
1117 format
!("{:?}", b
"\0\0\0 ftypisom\0\0\x02\0isomiso2avc1mp".as_bstr()),
1120 // Tests that if the underlying bytes contain the UTF-8 encoding of the
1121 // replacement codepoint, then we emit the codepoint just like other
1122 // non-printable Unicode characters.
1124 b
"\"\\xFF\xEF\xBF\xBD\\xFF\"".as_bstr(),
1125 // Before fixing #72, the output here would be:
1126 // \\xFF\\xEF\\xBF\\xBD\\xFF
1127 B(&format
!("{:?}", b
"\xFF\xEF\xBF\xBD\xFF".as_bstr())).as_bstr(),
1131 // See: https://github.com/BurntSushi/bstr/issues/82
1133 #[cfg(feature = "std")]
1134 fn test_cows_regression() {
1135 use std
::borrow
::Cow
;
1137 use crate::ByteSlice
;
1139 let c1
= Cow
::from(b
"hello bstr".as_bstr());
1140 let c2
= b
"goodbye bstr".as_bstr();
1143 let c3
= Cow
::from("hello str");
1144 let c4
= "goodbye str";