1use std::borrow::Cow;
2
3use ::bstr::{BStr, BString};
4
5pub trait AsBStr {
10 fn as_bstr(&self) -> &BStr;
12}
13
14pub trait AsBStrOpt {
16 fn as_bstr_opt(&self) -> Option<&BStr>;
18}
19
20impl<T: AsBStr + ?Sized> AsBStrOpt for T {
21 fn as_bstr_opt(&self) -> Option<&BStr> {
22 Some(self.as_bstr())
23 }
24}
25
26impl AsBStrOpt for Option<&BStr> {
27 fn as_bstr_opt(&self) -> Option<&BStr> {
28 *self
29 }
30}
31
32impl<T: AsBStr + ?Sized> AsBStr for &T {
33 fn as_bstr(&self) -> &BStr {
34 T::as_bstr(self)
35 }
36}
37
38impl<T: AsBStr + ?Sized> AsBStr for &mut T {
39 fn as_bstr(&self) -> &BStr {
40 T::as_bstr(self)
41 }
42}
43
44impl AsBStr for BStr {
45 fn as_bstr(&self) -> &BStr {
46 self
47 }
48}
49
50impl AsBStr for BString {
51 fn as_bstr(&self) -> &BStr {
52 BStr::new(self.as_slice())
53 }
54}
55
56impl AsBStr for str {
57 fn as_bstr(&self) -> &BStr {
58 BStr::new(self)
59 }
60}
61
62impl AsBStr for String {
63 fn as_bstr(&self) -> &BStr {
64 BStr::new(self)
65 }
66}
67
68impl AsBStr for [u8] {
69 fn as_bstr(&self) -> &BStr {
70 BStr::new(self)
71 }
72}
73
74impl AsBStr for Vec<u8> {
75 fn as_bstr(&self) -> &BStr {
76 BStr::new(self)
77 }
78}
79
80impl<const N: usize> AsBStr for [u8; N] {
81 fn as_bstr(&self) -> &BStr {
82 BStr::new(self)
83 }
84}
85
86impl<T> AsBStr for Cow<'_, T>
87where
88 T: AsBStr + ToOwned + ?Sized,
89{
90 fn as_bstr(&self) -> &BStr {
91 self.as_ref().as_bstr()
92 }
93}