1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use std::borrow::{Cow, ToOwned};
use ByteString;
pub trait BorrowBytes {
fn borrow_bytes<'a>(&'a self) -> &'a [u8];
}
impl BorrowBytes for String {
fn borrow_bytes(&self) -> &[u8] { self.as_bytes() }
}
impl BorrowBytes for str {
fn borrow_bytes(&self) -> &[u8] { self.as_bytes() }
}
impl BorrowBytes for ByteString {
fn borrow_bytes(&self) -> &[u8] { &**self }
}
impl BorrowBytes for [u8] {
fn borrow_bytes(&self) -> &[u8] { self }
}
impl<'a, B: ?Sized> BorrowBytes for Cow<'a, B>
where B: BorrowBytes + ToOwned, <B as ToOwned>::Owned: BorrowBytes {
fn borrow_bytes(&self) -> &[u8] {
match *self {
Cow::Borrowed(v) => v.borrow_bytes(),
Cow::Owned(ref v) => v.borrow_bytes(),
}
}
}
impl<'a, T: ?Sized + BorrowBytes> BorrowBytes for &'a T {
fn borrow_bytes(&self) -> &[u8] { (*self).borrow_bytes() }
}