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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
extern crate fraction;
extern crate num;
use self::fraction::BigFraction;
use self::num::BigInt;
use std::collections::HashMap;
use std::hash::Hash;
use crate::book::extractor::pointer::Pointer;
pub trait FromPointer<'a>: Sized {
fn from_pointer(pointer: Pointer<'a>) -> Option<Self>;
}
macro_rules! from_pointer_impl {
($t:ty) => {
impl<'a> FromPointer<'a> for $t {
fn from_pointer(pointer: Pointer<'a>) -> Option<Self> {
pointer.into::<$t>()
}
}
};
(link $t:ty) => {
impl<'a> FromPointer<'a> for &'a $t {
fn from_pointer(pointer: Pointer<'a>) -> Option<Self> {
pointer.into::<&'a $t>()
}
}
};
}
from_pointer_impl!(());
from_pointer_impl!(bool);
from_pointer_impl!(char);
from_pointer_impl! (link str);
from_pointer_impl!(String);
from_pointer_impl!(f32);
from_pointer_impl!(f64);
from_pointer_impl!(BigFraction);
from_pointer_impl! (link BigFraction);
from_pointer_impl!(u8);
from_pointer_impl!(i8);
from_pointer_impl!(u16);
from_pointer_impl!(i16);
from_pointer_impl!(u32);
from_pointer_impl!(i32);
from_pointer_impl!(u64);
from_pointer_impl!(i64);
from_pointer_impl!(usize);
from_pointer_impl!(isize);
from_pointer_impl!(BigInt);
from_pointer_impl! (link BigInt);
from_pointer_impl!(Vec<u8>);
from_pointer_impl! (link Vec<u8>);
pub trait List<'a>: Sized {
fn list_new() -> Self;
fn list_reserve(&mut self, size: usize);
fn list_update(&mut self, val: Pointer<'a>);
}
impl<'a, V> List<'a> for Vec<V>
where
V: FromPointer<'a>,
{
fn list_new() -> Self {
Vec::new()
}
fn list_reserve(&mut self, size: usize) {
self.reserve_exact(size);
}
fn list_update(&mut self, val: Pointer<'a>) {
if let Some(v) = <V as FromPointer>::from_pointer(val) {
self.push(v);
}
}
}
pub trait Dict<'a>: Sized {
fn dict_new() -> Self;
fn dict_reserve(&mut self, size: usize);
fn dict_update(&mut self, key: Pointer<'a>, val: Pointer<'a>);
}
impl<'a, K, V> Dict<'a> for HashMap<K, V>
where
K: FromPointer<'a> + Eq + Hash,
V: FromPointer<'a>,
{
fn dict_new() -> Self {
HashMap::new()
}
fn dict_reserve(&mut self, size: usize) {
self.reserve(size)
}
fn dict_update(&mut self, key: Pointer<'a>, val: Pointer<'a>) {
if let Some(k) = <K as FromPointer>::from_pointer(key) {
if let Some(v) = <V as FromPointer>::from_pointer(val) {
self.insert(k, v);
}
}
}
}