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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use std::marker::PhantomData;
use std::ptr::NonNull;
use crate::quad::Quad;
use crate::term::TTerm;
mod _unsafe_quad;
pub(crate) use _unsafe_quad::*;
pub trait QuadStreamingMode {
type UnsafeQuad: UnsafeQuad;
}
#[derive(Debug)]
pub struct ByValue<Q: Quad>(PhantomData<Q>);
impl<Q: Quad> QuadStreamingMode for ByValue<Q> {
type UnsafeQuad = Q;
}
#[derive(Debug)]
pub struct ByRef<Q: Quad>(PhantomData<Q>);
impl<Q: Quad> QuadStreamingMode for ByRef<Q> {
type UnsafeQuad = NonNull<Q>;
}
#[derive(Debug)]
pub struct ByTermRefs<T: TTerm + ?Sized>(PhantomData<*const T>);
impl<T: TTerm + ?Sized> QuadStreamingMode for ByTermRefs<T> {
#[allow(clippy::type_complexity)]
type UnsafeQuad = TermRefs<([NonNull<T>; 3], Option<NonNull<T>>)>;
}
#[derive(Debug)]
pub struct StreamedQuad<'a, T: QuadStreamingMode> {
_phantom: PhantomData<&'a T::UnsafeQuad>,
wrapped: T::UnsafeQuad,
}
impl<'a, T> StreamedQuad<'a, T>
where
T: QuadStreamingMode,
{
pub unsafe fn wrap(wrapped: T::UnsafeQuad) -> Self {
StreamedQuad {
_phantom: PhantomData,
wrapped,
}
}
}
impl<'a, Q> StreamedQuad<'a, ByValue<Q>>
where
Q: Quad,
{
pub fn by_value(quad: Q) -> Self {
StreamedQuad {
_phantom: PhantomData,
wrapped: quad,
}
}
}
impl<'a, Q> StreamedQuad<'a, ByRef<Q>>
where
Q: Quad,
{
pub fn by_ref(quad: &'a Q) -> Self {
StreamedQuad {
_phantom: PhantomData,
wrapped: quad.into(),
}
}
}
impl<'a, T> StreamedQuad<'a, ByTermRefs<T>>
where
T: TTerm + ?Sized,
{
pub fn by_term_refs(s: &'a T, p: &'a T, o: &'a T, g: Option<&'a T>) -> Self {
StreamedQuad {
_phantom: PhantomData,
wrapped: TermRefs(([s.into(), p.into(), o.into()], g.map(|g| g.into()))),
}
}
}
impl<'a, T> StreamedQuad<'a, T>
where
T: ScopedQuadMode<'a>,
{
pub fn scoped(quad: T::SourceQuad) -> Self {
T::scoped(quad)
}
}
impl<'a, T> Quad for StreamedQuad<'a, T>
where
T: QuadStreamingMode,
{
type Term = <T::UnsafeQuad as UnsafeQuad>::Term;
fn s(&self) -> &Self::Term {
unsafe { self.wrapped.u_s() }
}
fn p(&self) -> &Self::Term {
unsafe { self.wrapped.u_p() }
}
fn o(&self) -> &Self::Term {
unsafe { self.wrapped.u_o() }
}
fn g(&self) -> Option<&Self::Term> {
unsafe { self.wrapped.u_g() }
}
}
#[macro_export]
macro_rules! make_scoped_quad_streaming_mode {
($(#[$attrs: meta])* $mode: ident, $qt: ident) => {
$(#[$attrs])*
#[derive(Debug)]
pub struct $mode(std::marker::PhantomData<$qt<'static>>);
impl $crate::quad::streaming_mode::QuadStreamingMode for $mode {
type UnsafeQuad = $qt<'static>;
}
impl<'a> $crate::quad::streaming_mode::ScopedQuadMode<'a> for $mode {
type SourceQuad = $qt<'a>;
fn scoped(quad: $qt<'a>) -> $crate::quad::streaming_mode::StreamedQuad<'a, $mode> {
unsafe {
$crate::quad::streaming_mode::StreamedQuad::wrap(std::mem::transmute(quad))
}
}
}
};
}
pub trait ScopedQuadMode<'a>: QuadStreamingMode + Sized {
type SourceQuad: Quad + 'a;
fn scoped(quad: Self::SourceQuad) -> StreamedQuad<'a, Self>;
}
pub(crate) use crate::triple::streaming_mode::FromTriple;
#[derive(Debug)]
pub struct FromQuad<T: QuadStreamingMode>(PhantomData<T>);
impl<T: QuadStreamingMode> crate::triple::streaming_mode::TripleStreamingMode for FromQuad<T> {
type UnsafeTriple = UnsafeTripleAdapter<T::UnsafeQuad>;
}
impl<'a, Q> crate::triple::streaming_mode::StreamedTriple<'a, FromQuad<Q>>
where
Q: QuadStreamingMode,
{
pub(crate) fn from_quad(quad: StreamedQuad<Q>) -> Self {
unsafe { Self::wrap(UnsafeTripleAdapter(quad.wrapped)) }
}
}