-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathcow.rs
More file actions
44 lines (38 loc) · 1.13 KB
/
cow.rs
File metadata and controls
44 lines (38 loc) · 1.13 KB
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
42
43
44
use std::borrow::Cow;
/// Trait for macros to convert owned/borrowed types to `Cow`.
///
/// This is needed because `&str` and `String` do not have `From`
/// implements into `Cow<_, [u8]>`. One solution is to just call `AsRef<[u8]>`
/// before converting. However, then when a user specifies an owned type,
/// we will implicitly borrow that; this trait prevents that so that macro
/// behavior is intuitive, so that owned types stay owned.
pub trait BCowConvert<'a> {
fn convert(self) -> Cow<'a, [u8]>;
}
// TODO: Enable when specialization lands.
/*
impl<'a, T> BCowConvert<'a> for T where T: AsRef<[u8]> + 'a {
fn convert(self) -> Cow<'a, [u8]> {
self.into()
}
}*/
impl<'a> BCowConvert<'a> for &'a [u8] {
fn convert(self) -> Cow<'a, [u8]> {
self.into()
}
}
impl<'a> BCowConvert<'a> for &'a str {
fn convert(self) -> Cow<'a, [u8]> {
self.as_bytes().into()
}
}
impl BCowConvert<'static> for String {
fn convert(self) -> Cow<'static, [u8]> {
self.into_bytes().into()
}
}
impl BCowConvert<'static> for Vec<u8> {
fn convert(self) -> Cow<'static, [u8]> {
self.into()
}
}