rdf_types/dataset/graph/
async.rs1use std::pin::pin;
4
5use futures_lite::{Stream, StreamExt, stream};
6
7use crate::{
8 Triple,
9 dataset::{TryFiniteGraph, TryGraph, TryGraphMut, TryPatternMatchingGraph},
10 pattern::LinearTriplePattern,
11};
12
13pub trait AsyncFiniteGraph: TryGraph {
15 type AsyncTriples<'a>: Stream<Item = Result<Triple<Self::Resource>, Self::Error>>
17 where
18 Self: 'a;
19
20 async fn async_triples(&self) -> Result<Self::AsyncTriples<'_>, Self::Error>;
22}
23
24impl<D: TryFiniteGraph> AsyncFiniteGraph for D {
27 type AsyncTriples<'a>
28 = stream::Iter<D::TryTriples<'a>>
29 where
30 Self: 'a;
31
32 async fn async_triples(&self) -> Result<Self::AsyncTriples<'_>, Self::Error> {
33 self.try_triples().map(stream::iter)
34 }
35}
36
37pub trait AsyncPatternMatchingGraph: TryGraph {
39 type AsyncTriplePatternMatching<'a, 'p>: Stream<
41 Item = Result<Triple<Self::Resource>, Self::Error>,
42 >
43 where
44 Self: 'a,
45 Self::Resource: 'p;
46
47 async fn async_triple_pattern_matching<'p>(
50 &self,
51 pattern: LinearTriplePattern<&'p Self::Resource>,
52 ) -> Result<Self::AsyncTriplePatternMatching<'_, 'p>, Self::Error>;
53
54 async fn async_contains_triple(
56 &self,
57 triple: Triple<&Self::Resource>,
58 ) -> Result<bool, Self::Error> {
59 let mut stream = pin!(self.async_triple_pattern_matching(triple.into()).await?);
60 Ok(stream.next().await.transpose()?.is_some())
61 }
62}
63
64impl<D: TryPatternMatchingGraph> AsyncPatternMatchingGraph for D {
67 type AsyncTriplePatternMatching<'a, 'p>
68 = stream::Iter<D::TryTriplePatternMatching<'a, 'p>>
69 where
70 Self: 'a,
71 Self::Resource: 'p;
72
73 async fn async_triple_pattern_matching<'p>(
74 &self,
75 pattern: LinearTriplePattern<&'p Self::Resource>,
76 ) -> Result<Self::AsyncTriplePatternMatching<'_, 'p>, Self::Error> {
77 self.try_triple_pattern_matching(pattern).map(stream::iter)
78 }
79}
80
81pub trait AsyncGraphMut: TryGraph {
83 async fn async_insert(&mut self, triple: Triple<Self::Resource>) -> Result<(), Self::Error>;
85}
86
87impl<D: TryGraphMut> AsyncGraphMut for D {
90 async fn async_insert(&mut self, triple: Triple<Self::Resource>) -> Result<(), Self::Error> {
91 self.try_insert(triple)
92 }
93}