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
//! Streaming modes for quad iterators and quad streams.
//!
//! See [`triple::streaming_mode`](../../triple/streaming_mode/index.html)
//! for more detail.

use std::marker::PhantomData;
use std::ptr::NonNull;

use crate::quad::Quad;
use sophia_term::{RefTerm, Term, TermData};

mod _unsafe_quad;
pub(crate) use _unsafe_quad::*;

/// See [module](./index.html) documentation.
pub trait QuadStreamingMode {
    type UnsafeQuad: UnsafeQuad;
}
/// See [module](./index.html) documentation.
#[derive(Debug)]
pub struct ByValue<Q: Quad>(PhantomData<Q>);
impl<Q: Quad> QuadStreamingMode for ByValue<Q> {
    type UnsafeQuad = Q;
}
/// See [module](./index.html) documentation.
#[derive(Debug)]
pub struct ByRef<Q: Quad>(PhantomData<Q>);
impl<Q: Quad> QuadStreamingMode for ByRef<Q> {
    type UnsafeQuad = NonNull<Q>;
}
/// See [module](./index.html) documentation.
#[derive(Debug)]
pub struct ByRefTerms {}
impl QuadStreamingMode for ByRefTerms {
    type UnsafeQuad = ([RefTerm<'static>; 3], Option<RefTerm<'static>>);
}
/// See [module](./index.html) documentation.
#[derive(Debug)]
pub struct ByTermRefs<TD: TermData>(PhantomData<TD>);
impl<TD: TermData> QuadStreamingMode for ByTermRefs<TD> {
    #[allow(clippy::type_complexity)]
    type UnsafeQuad = ([NonNull<Term<TD>>; 3], Option<NonNull<Term<TD>>>);
}

/// See [module](./index.html) documentation.
#[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,
{
    /// Raw constructor
    ///
    /// # Safety
    ///
    /// This must only be used if the unsafe quad `wrapped`
    /// is guaranteed to live for at least `'a`
    /// (the lifetime of this streamed quad).
    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> StreamedQuad<'a, ByRefTerms> {
    pub fn by_ref_terms(
        s: RefTerm<'a>,
        p: RefTerm<'a>,
        o: RefTerm<'a>,
        g: Option<RefTerm<'a>>,
    ) -> Self {
        let s = unsafe { std::mem::transmute(s) };
        let p = unsafe { std::mem::transmute(p) };
        let o = unsafe { std::mem::transmute(o) };
        let g = unsafe { std::mem::transmute(g) };
        StreamedQuad {
            _phantom: PhantomData,
            wrapped: ([s, p, o], g),
        }
    }
}
impl<'a, T> StreamedQuad<'a, ByTermRefs<T>>
where
    T: TermData,
{
    pub fn by_term_refs(
        s: &'a Term<T>,
        p: &'a Term<T>,
        o: &'a Term<T>,
        g: Option<&'a Term<T>>,
    ) -> Self {
        StreamedQuad {
            _phantom: PhantomData,
            wrapped: ([s.into(), p.into(), o.into()], g.map(|g| g.into())),
        }
    }
}
impl<'a, T> Quad for StreamedQuad<'a, T>
where
    T: QuadStreamingMode,
{
    type TermData = <T::UnsafeQuad as UnsafeQuad>::TermData;
    fn s(&self) -> &Term<Self::TermData> {
        unsafe { self.wrapped.u_s() }
    }
    fn p(&self) -> &Term<Self::TermData> {
        unsafe { self.wrapped.u_p() }
    }
    fn o(&self) -> &Term<Self::TermData> {
        unsafe { self.wrapped.u_o() }
    }
    fn g(&self) -> Option<&Term<Self::TermData>> {
        unsafe { self.wrapped.u_g() }
    }
}

// adapter

pub(crate) use crate::triple::streaming_mode::FromTriple;

/// See [module](./index.html) documentation.
#[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)) }
    }
}