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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
//! Streaming modes for triple iterators and triple streams. //! //! # What this is about //! //! The [`Graph`] traits provides a number of methods for iterating over [`Triple`]s, //! as for example the [`triples`] method. //! Since [`Triple`] is itself a trait, //! the actual type returned by such iterating methods needs to be specified for each //! [`Graph`] implementation. //! This is what this module is about. //! //! # In the future, the ideal solution //! //! Ideally, the concrete type of [`Triples`] returned by iterating methods //! would be directly given by the [`Graph::Triple`] associated type. //! //! Unfortunately, in the current version of Rust, //! this is not practical: //! in many implementations, iterating methods would return *references* //! to triples that are actually owned by the graph itself. //! But the associated type [`Graph::Triple`] can not be a reference type, //! because the *lifetime* of such references can not be specified once and for all. //! //! The ideal solution to this problem would be [Generic Associated Types] (GAT), //! but until those are stabilized, we need another solution. //! //! # In the meantime, the workaround //! //! The associated type [`Graph::Triple`] is not a type implementing the [`Triple`] trait, //! it is one of the possible [`TripleStreamingMode`]s provided by this module //! (see below). //! Then iterating methods are restricted to returning a particular version of //! [`StreamedTriple<'a>`], satisfying the given streaming mode. //! The available streaming mode are: //! //! * [`ByValue<T>`]: [`StreamedTriple<'a>`] will wrap an (owned) instance of `T`; //! it is constructed with [`StreamedTriple::by_value`]; //! * [`ByRef<T>`]: [`StreamedTriple<'a>`] will wrap a reference to `T`, valid as long as `'a`; //! it is constructed with [`StreamedTriple::by_ref`]; //! * [`ByRefTerms`]: [`StreamedTriple<'a>`] will wrap an array of 3 [`Term<&'a str>`]; //! it is constructed with [`StreamedTriple::by_ref_terms`]. //! * [`ByTermRefs<TD>`]: [`StreamedTriple<'a>`] will wrap an array of 3 [`Term<TD>`] references, //! valid as long as `'a`; //! it is constructed with [`StreamedTriple::by_term_refs`]. //! //! NB: actually, another mode exists, //! but is specifically designed for the [`graph::adapter`](../../graph/adapter/index.html) module, //! should never be needed in other contexts. //! //! [`ByRef<T>`]: struct.ByRef.html //! [`ByRefTerms`]: struct.ByRefTerms.html //! [`ByTermRefs<TD>`]: struct.ByTermRefs.html //! [`ByValue<T>`]: struct.ByValue.html //! [Generic Associated Types]: https://github.com/rust-lang/rust/issues/44265 //! [`Graph`]: ../../graph/trait.Graph.html //! [`Graph::Triple`]: ../../graph/trait.Graph.html#associatedtype.Triple //! [`StreamedTriple<'a>`]: struct.StreamedTriple.html //! [`StreamedTriple::by_value`]: struct.StreamedTriple.html#method.by_value //! [`StreamedTriple::by_ref`]: struct.StreamedTriple.html#method.by_ref //! [`StreamedTriple::by_term_refs`]: struct.StreamedTriple.html#method.by_term_refs //! [`Term<TD>`]: ../../term //! [`Term<&'a str>`]: ../../term //! [`Triple`]: ../trait.Triple.html //! [`triples`]: ../../graph/trait.Graph.html#tymethod.triples //! [`TripleStreamingMode`]: trait.TripleStreamingMode.html use std::marker::PhantomData; use std::ptr::NonNull; use crate::term::{RefTerm, Term, TermData}; use crate::triple::Triple; mod _unsafe_triple; pub(crate) use _unsafe_triple::*; /// See [module](./index.html) documentation. pub trait TripleStreamingMode { type UnsafeTriple: UnsafeTriple; } /// See [module](./index.html) documentation. #[derive(Debug)] pub struct ByValue<T: Triple>(PhantomData<T>); impl<T: Triple> TripleStreamingMode for ByValue<T> { type UnsafeTriple = T; } /// See [module](./index.html) documentation. #[derive(Debug)] pub struct ByRef<T: Triple>(PhantomData<T>); impl<T: Triple> TripleStreamingMode for ByRef<T> { type UnsafeTriple = NonNull<T>; } /// See [module](./index.html) documentation. #[derive(Debug)] pub struct ByRefTerms {} impl TripleStreamingMode for ByRefTerms { type UnsafeTriple = [RefTerm<'static>; 3]; } /// See [module](./index.html) documentation. #[derive(Debug)] pub struct ByTermRefs<TD: TermData>(PhantomData<TD>); impl<TD: TermData> TripleStreamingMode for ByTermRefs<TD> { type UnsafeTriple = [NonNull<Term<TD>>; 3]; } /// See [module](./index.html) documentation. #[derive(Debug)] pub struct StreamedTriple<'a, T: TripleStreamingMode> { _phantom: PhantomData<&'a T::UnsafeTriple>, wrapped: T::UnsafeTriple, } impl<'a, T> StreamedTriple<'a, T> where T: TripleStreamingMode, { /// Raw constructor /// /// # Safety /// /// This must only be used if the unsafe triple `wrapped` /// is guaranteed to live for at least `'a` /// (the lifetime of this streamed triple). pub unsafe fn wrap(wrapped: T::UnsafeTriple) -> Self { StreamedTriple { _phantom: PhantomData, wrapped, } } } impl<'a, T> StreamedTriple<'a, ByValue<T>> where T: Triple, { pub fn by_value(triple: T) -> Self { StreamedTriple { _phantom: PhantomData, wrapped: triple, } } } impl<'a, T> StreamedTriple<'a, ByRef<T>> where T: Triple, { pub fn by_ref(triple: &'a T) -> Self { StreamedTriple { _phantom: PhantomData, wrapped: triple.into(), } } } impl<'a> StreamedTriple<'a, ByRefTerms> { pub fn by_ref_terms(s: RefTerm<'a>, p: RefTerm<'a>, o: RefTerm<'a>) -> Self { let s = unsafe { std::mem::transmute(s) }; let p = unsafe { std::mem::transmute(p) }; let o = unsafe { std::mem::transmute(o) }; StreamedTriple { _phantom: PhantomData, wrapped: [s, p, o], } } } impl<'a, T> StreamedTriple<'a, ByTermRefs<T>> where T: TermData, { pub fn by_term_refs(s: &'a Term<T>, p: &'a Term<T>, o: &'a Term<T>) -> Self { StreamedTriple { _phantom: PhantomData, wrapped: [s.into(), p.into(), o.into()], } } } impl<'a, T> Triple for StreamedTriple<'a, T> where T: TripleStreamingMode, { type TermData = <T::UnsafeTriple as UnsafeTriple>::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() } } } // adapter pub(crate) use crate::quad::streaming_mode::FromQuad; #[derive(Debug)] pub struct FromTriple<T: TripleStreamingMode>(PhantomData<T>); impl<T: TripleStreamingMode> crate::quad::streaming_mode::QuadStreamingMode for FromTriple<T> { type UnsafeQuad = UnsafeQuadAdapter<T::UnsafeTriple>; } impl<'a, T> crate::quad::streaming_mode::StreamedQuad<'a, FromTriple<T>> where T: TripleStreamingMode, { pub(crate) fn from_triple(triple: StreamedTriple<T>) -> Self { unsafe { Self::wrap(UnsafeQuadAdapter(triple.wrapped)) } } }