Skip to main content

rdf_types/dataset/graph/
async.rs

1//! Asynchronous counterparts of the [`Graph`](super::Graph) traits, for
2//! graphs backed by asynchronous storage.
3use 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
13/// Async finite graph.
14pub trait AsyncFiniteGraph: TryGraph {
15	/// Asynchronous fallible triples stream.
16	type AsyncTriples<'a>: Stream<Item = Result<Triple<Self::Resource>, Self::Error>>
17	where
18		Self: 'a;
19
20	/// Returns a stream over the triples of the graph.
21	async fn async_triples(&self) -> Result<Self::AsyncTriples<'_>, Self::Error>;
22}
23
24/// Any fallible finite graph can be used, synchronously, as an asynchronous
25/// finite graph.
26impl<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
37/// Async pattern-matching-capable graph.
38pub trait AsyncPatternMatchingGraph: TryGraph {
39	/// Asynchronous fallible pattern-matching stream.
40	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	/// Returns a stream over all the triples of the graph matching the
48	/// given pattern.
49	async fn async_triple_pattern_matching<'p>(
50		&self,
51		pattern: LinearTriplePattern<&'p Self::Resource>,
52	) -> Result<Self::AsyncTriplePatternMatching<'_, 'p>, Self::Error>;
53
54	/// Checks if the graph contains the given triple.
55	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
64/// Any fallible pattern-matching-capable graph can be used, synchronously,
65/// as an asynchronous pattern-matching-capable graph.
66impl<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
81/// Async mutable graph.
82pub trait AsyncGraphMut: TryGraph {
83	/// Inserts the given triple in the graph.
84	async fn async_insert(&mut self, triple: Triple<Self::Resource>) -> Result<(), Self::Error>;
85}
86
87/// Any fallible mutable graph can be used, synchronously, as an
88/// asynchronous mutable graph.
89impl<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}