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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use super::*;
extern crate alloc;
impl<T: ?Sized + ToOwned> Bake for alloc::borrow::Cow<'_, T>
where
for<'a> &'a T: Bake,
{
fn bake(&self, ctx: &CrateEnv) -> TokenStream {
ctx.insert("alloc");
let t = <&T as Bake>::bake(&&**self, ctx);
quote! {
alloc::borrow::Cow::Borrowed(#t)
}
}
}
#[test]
fn cow() {
test_bake!(
alloc::borrow::Cow<'static, str>,
const: alloc::borrow::Cow::Borrowed("hi"),
alloc
);
}
impl<T> Bake for Vec<T>
where
T: Bake,
{
fn bake(&self, ctx: &CrateEnv) -> TokenStream {
ctx.insert("alloc");
let data = self.iter().map(|d| d.bake(ctx));
quote! {
alloc::vec![#(#data,)*]
}
}
}
#[test]
fn vec() {
test_bake!(Vec<u8>, alloc::vec![1u8, 2u8,], alloc);
}
impl Bake for String {
fn bake(&self, ctx: &CrateEnv) -> TokenStream {
ctx.insert("alloc");
quote! {
alloc::string::String::from(#self)
}
}
}
#[test]
fn string() {
test_bake!(String, alloc::string::String::from("hello"), alloc);
}
macro_rules! smart_pointer {
($type:ty, $constuctor:path) => {
impl<T> Bake for $type
where
T: Bake,
{
fn bake(&self, ctx: &CrateEnv) -> TokenStream {
ctx.insert("alloc");
let data = std::ops::Deref::deref(self).bake(ctx);
quote! {
$constuctor(#data)
}
}
}
};
}
smart_pointer!(alloc::boxed::Box<T>, alloc::boxed::Box::new);
smart_pointer!(alloc::rc::Rc<T>, alloc::rc::Rc::new);
smart_pointer!(alloc::sync::Arc<T>, alloc::arc::Arc::new);
#[test]
fn rc() {
test_bake!(alloc::rc::Rc<char>, alloc::rc::Rc::new('b'), alloc);
}