diff --git a/api/src/dataset.rs b/api/src/dataset.rs index 6dd2da62..1bbf3ce0 100644 --- a/api/src/dataset.rs +++ b/api/src/dataset.rs @@ -26,12 +26,20 @@ pub type DResult = Result::Error>; /// Type alias for fallible quad iterators produced by a dataset. /// /// See [`Dataset::quads`] for more information about how to use it. +#[deprecated( + since = "0.8.1", + note = "prototypes of `quads` and `quads_matching` have changed" +)] pub type DQuadSource<'a, D> = Box::Quad<'a>>> + 'a>; /// Type alias for terms produced by a dataset. pub type DTerm<'a, D> = <::Quad<'a> as Quad>::Term; /// Type alias for fallible term iterators produced by a dataset. /// /// See [`Dataset::subjects`] for more information about how to use it. +#[deprecated( + since = "0.8.1", + note = "prototypes of term-yielding methods have changed" +)] pub type DTermSource<'a, D> = Box>> + 'a>; /// Generic trait for RDF datasets. @@ -90,7 +98,7 @@ pub trait Dataset { /// # Ok(()) /// # } /// ``` - fn quads(&self) -> DQuadSource; + fn quads(&self) -> impl Iterator>> + '_; /// An iterator visiting all quads matching the given subject, predicate and object. /// See [`crate::term::matcher`] @@ -151,21 +159,27 @@ pub trait Dataset { /// # /// # Ok(()) } /// ``` - fn quads_matching<'s, S, P, O, G>(&'s self, sm: S, pm: P, om: O, gm: G) -> DQuadSource<'s, Self> + fn quads_matching<'s, S, P, O, G>( + &'s self, + sm: S, + pm: P, + om: O, + gm: G, + ) -> impl Iterator>> + '_ where S: TermMatcher + 's, P: TermMatcher + 's, O: TermMatcher + 's, G: GraphNameMatcher + 's, { - Box::new(self.quads().filter_ok(move |q| { + self.quads().filter_ok(move |q| { q.matched_by( sm.matcher_ref(), pm.matcher_ref(), om.matcher_ref(), gm.matcher_ref(), ) - })) + }) } /// Return `true` if this dataset contains the given quad. @@ -186,32 +200,32 @@ pub trait Dataset { /// /// NB: implementations SHOULD avoid yielding the same term multiple times, but MAY do so. /// Users MUST therefore be prepared to deal with duplicates. - fn subjects(&self) -> DTermSource { - Box::new(self.quads().map_ok(Quad::to_s)) + fn subjects(&self) -> impl Iterator>> + '_ { + self.quads().map_ok(Quad::to_s) } /// Build a fallible iterator of all the terms used as predicate in this Dataset. /// /// NB: implementations SHOULD avoid yielding the same term multiple times, but MAY do so. /// Users MUST therefore be prepared to deal with duplicates. - fn predicates(&self) -> DTermSource { - Box::new(self.quads().map_ok(Quad::to_p)) + fn predicates(&self) -> impl Iterator>> + '_ { + self.quads().map_ok(Quad::to_p) } /// Build a fallible iterator of all the terms used as object in this Dataset. /// /// NB: implementations SHOULD avoid yielding the same term multiple times, but MAY do so. /// Users MUST therefore be prepared to deal with duplicates. - fn objects(&self) -> DTermSource { - Box::new(self.quads().map_ok(Quad::to_o)) + fn objects(&self) -> impl Iterator>> + '_ { + self.quads().map_ok(Quad::to_o) } /// Build a fallible iterator of all the terms used as graph name in this Dataset. /// /// NB: implementations SHOULD avoid yielding the same term multiple times, but MAY do so. /// Users MUST therefore be prepared to deal with duplicates. - fn graph_names(&self) -> DTermSource { - Box::new(self.quads().filter_map_ok(Quad::to_g)) + fn graph_names(&self) -> impl Iterator>> + '_ { + self.quads().filter_map_ok(Quad::to_g) } /// Build a fallible iterator of all the IRIs used in this Dataset @@ -219,13 +233,11 @@ pub trait Dataset { /// /// NB: implementations SHOULD avoid yielding the same term multiple times, but MAY do so. /// Users MUST therefore be prepared to deal with duplicates. - fn iris(&self) -> DTermSource { - Box::new( - self.quads() - .flat_map_ok(iter_spog) - .flat_map_ok(Term::to_atoms) - .filter_ok(Term::is_iri), - ) + fn iris(&self) -> impl Iterator>> + '_ { + self.quads() + .flat_map_ok(iter_spog) + .flat_map_ok(Term::to_atoms) + .filter_ok(Term::is_iri) } /// Build a fallible iterator of all the blank nodes used in this Dataset @@ -233,13 +245,11 @@ pub trait Dataset { /// /// NB: implementations SHOULD avoid yielding the same term multiple times, but MAY do so. /// Users MUST therefore be prepared to deal with duplicates. - fn blank_nodes(&self) -> DTermSource { - Box::new( - self.quads() - .flat_map_ok(iter_spog) - .flat_map_ok(Term::to_atoms) - .filter_ok(Term::is_blank_node), - ) + fn blank_nodes(&self) -> impl Iterator>> + '_ { + self.quads() + .flat_map_ok(iter_spog) + .flat_map_ok(Term::to_atoms) + .filter_ok(Term::is_blank_node) } /// Build a fallible iterator of all the literals used in this Dataset @@ -247,13 +257,11 @@ pub trait Dataset { /// /// NB: implementations SHOULD avoid yielding the same term multiple times, but MAY do so. /// Users MUST therefore be prepared to deal with duplicates. - fn literals(&self) -> DTermSource { - Box::new( - self.quads() - .flat_map_ok(iter_spog) - .flat_map_ok(Term::to_atoms) - .filter_ok(Term::is_literal), - ) + fn literals(&self) -> impl Iterator>> + '_ { + self.quads() + .flat_map_ok(iter_spog) + .flat_map_ok(Term::to_atoms) + .filter_ok(Term::is_literal) } /// Build a fallible iterator of all the quoted triples used in this Dataset @@ -261,7 +269,7 @@ pub trait Dataset { /// /// NB: implementations SHOULD avoid yielding the same term multiple times, but MAY do so. /// Users MUST therefore be prepared to deal with duplicates. - fn quoted_triples<'s>(&'s self) -> DTermSource<'s, Self> + fn quoted_triples<'s>(&'s self) -> Box>> + '_> where DTerm<'s, Self>: Clone, { @@ -278,13 +286,11 @@ pub trait Dataset { /// /// NB: implementations SHOULD avoid yielding the same term multiple times, but MAY do so. /// Users MUST therefore be prepared to deal with duplicates. - fn variables(&self) -> DTermSource { - Box::new( - self.quads() - .flat_map_ok(iter_spog) - .flat_map_ok(Term::to_atoms) - .filter_ok(Term::is_variable), - ) + fn variables(&self) -> impl Iterator>> + '_ { + self.quads() + .flat_map_ok(iter_spog) + .flat_map_ok(Term::to_atoms) + .filter_ok(Term::is_variable) } /// Borrows one of the graphs of this dataset @@ -639,14 +645,14 @@ mod check_implementability { type Quad<'x> = [SimpleTerm<'x>; 4] where Self: 'x; type Error = std::convert::Infallible; - fn quads(&self) -> DQuadSource { - Box::new(self.graphs.iter().flat_map(move |(gi, tis)| { + fn quads(&self) -> impl Iterator>> + '_ { + self.graphs.iter().flat_map(move |(gi, tis)| { let g = self.make_term(*gi); tis.iter().copied().map(move |ti| { let [s, p, o] = self.triples[ti].map(|j| self.make_term(j)); Ok([s, p, o, g.clone()]) }) - })) + }) } } } @@ -770,8 +776,8 @@ mod check_implementability_lazy_term { type Quad<'x> = [MyTerm<'x>; 4] where Self: 'x; type Error = std::convert::Infallible; - fn quads(&self) -> DQuadSource { - Box::new(self.graphs.iter().flat_map(move |(gi, tis)| { + fn quads(&self) -> impl Iterator>> + '_ { + self.graphs.iter().flat_map(move |(gi, tis)| { let g = MyTerm { dataset: self, index: *gi, @@ -783,7 +789,7 @@ mod check_implementability_lazy_term { }); Ok([s, p, o, g]) }) - })) + }) } } } diff --git a/api/src/dataset/_foreign_impl.rs b/api/src/dataset/_foreign_impl.rs index 8aa110b5..5da41e9e 100644 --- a/api/src/dataset/_foreign_impl.rs +++ b/api/src/dataset/_foreign_impl.rs @@ -17,11 +17,17 @@ impl<'a, T: Dataset + ?Sized> Dataset for &'a T { type Error = T::Error; - fn quads(&self) -> DQuadSource { + fn quads(&self) -> impl Iterator>> + '_ { T::quads(*self) } - fn quads_matching<'s, S, P, O, G>(&'s self, sm: S, pm: P, om: O, gm: G) -> DQuadSource<'s, Self> + fn quads_matching<'s, S, P, O, G>( + &'s self, + sm: S, + pm: P, + om: O, + gm: G, + ) -> impl Iterator>> + 's where S: TermMatcher + 's, P: TermMatcher + 's, @@ -41,42 +47,42 @@ impl<'a, T: Dataset + ?Sized> Dataset for &'a T { T::contains(*self, s, p, o, g) } - fn subjects(&self) -> DTermSource { + fn subjects(&self) -> impl Iterator>> + '_ { T::subjects(*self) } - fn predicates(&self) -> DTermSource { + fn predicates(&self) -> impl Iterator>> + '_ { T::predicates(*self) } - fn objects(&self) -> DTermSource { + fn objects(&self) -> impl Iterator>> + '_ { T::objects(*self) } - fn graph_names(&self) -> DTermSource { + fn graph_names(&self) -> impl Iterator>> + '_ { T::graph_names(*self) } - fn iris(&self) -> DTermSource { + fn iris(&self) -> impl Iterator>> + '_ { T::iris(*self) } - fn blank_nodes(&self) -> DTermSource { + fn blank_nodes(&self) -> impl Iterator>> + '_ { T::blank_nodes(*self) } - fn literals(&self) -> DTermSource { + fn literals(&self) -> impl Iterator>> + '_ { T::literals(*self) } - fn quoted_triples<'s>(&'s self) -> DTermSource<'s, Self> + fn quoted_triples<'s>(&'s self) -> Box>> + '_> where DTerm<'s, Self>: Clone, { T::quoted_triples(*self) } - fn variables(&self) -> DTermSource { + fn variables(&self) -> impl Iterator>> + '_ { T::variables(*self) } } @@ -87,11 +93,17 @@ impl<'a, T: Dataset + ?Sized> Dataset for &'a mut T { type Error = T::Error; - fn quads(&self) -> DQuadSource { + fn quads(&self) -> impl Iterator>> + '_ { T::quads(*self) } - fn quads_matching<'s, S, P, O, G>(&'s self, sm: S, pm: P, om: O, gm: G) -> DQuadSource<'s, Self> + fn quads_matching<'s, S, P, O, G>( + &'s self, + sm: S, + pm: P, + om: O, + gm: G, + ) -> impl Iterator>> + 's where S: TermMatcher + 's, P: TermMatcher + 's, @@ -111,42 +123,42 @@ impl<'a, T: Dataset + ?Sized> Dataset for &'a mut T { T::contains(*self, s, p, o, g) } - fn subjects(&self) -> DTermSource { + fn subjects(&self) -> impl Iterator>> + '_ { T::subjects(*self) } - fn predicates(&self) -> DTermSource { + fn predicates(&self) -> impl Iterator>> + '_ { T::predicates(*self) } - fn objects(&self) -> DTermSource { + fn objects(&self) -> impl Iterator>> + '_ { T::objects(*self) } - fn graph_names(&self) -> DTermSource { + fn graph_names(&self) -> impl Iterator>> + '_ { T::graph_names(*self) } - fn iris(&self) -> DTermSource { + fn iris(&self) -> impl Iterator>> + '_ { T::iris(*self) } - fn blank_nodes(&self) -> DTermSource { + fn blank_nodes(&self) -> impl Iterator>> + '_ { T::blank_nodes(*self) } - fn literals(&self) -> DTermSource { + fn literals(&self) -> impl Iterator>> + '_ { T::literals(*self) } - fn quoted_triples<'s>(&'s self) -> DTermSource<'s, Self> + fn quoted_triples<'s>(&'s self) -> Box>> + '_> where DTerm<'s, Self>: Clone, { T::quoted_triples(*self) } - fn variables(&self) -> DTermSource { + fn variables(&self) -> impl Iterator>> + '_ { T::variables(*self) } } @@ -245,8 +257,8 @@ impl Dataset for [Q] { type Error = Infallible; type Quad<'x> = Spog> where Self: 'x; - fn quads(&self) -> DQuadSource { - Box::new(self.iter().map(Quad::spog).map(Ok)) + fn quads(&self) -> impl Iterator>> + '_ { + self.iter().map(Quad::spog).map(Ok) } } @@ -256,7 +268,7 @@ impl Dataset for Vec { type Error = Infallible; type Quad<'x> = Spog> where Self: 'x; - fn quads(&self) -> DQuadSource { + fn quads(&self) -> impl Iterator>> + '_ { self[..].quads() } } @@ -417,8 +429,8 @@ impl Dataset for HashSet { type Error = Infallible; type Quad<'x> = Spog> where Self: 'x; - fn quads(&self) -> DQuadSource { - Box::new(self.iter().map(Quad::spog).map(Ok)) + fn quads(&self) -> impl Iterator>> + '_ { + self.iter().map(Quad::spog).map(Ok) } } @@ -569,8 +581,8 @@ impl Dataset for BTreeSet { type Error = Infallible; type Quad<'x> = Spog> where Self: 'x; - fn quads(&self) -> DQuadSource { - Box::new(self.iter().map(Quad::spog).map(Ok)) + fn quads(&self) -> impl Iterator>> + '_ { + self.iter().map(Quad::spog).map(Ok) } } diff --git a/api/src/dataset/adapter.rs b/api/src/dataset/adapter.rs index 7b0827a5..7c86783c 100644 --- a/api/src/dataset/adapter.rs +++ b/api/src/dataset/adapter.rs @@ -31,16 +31,21 @@ where type Quad<'x> = Spog> where Self: 'x; type Error = T::Error; - fn quads(&self) -> DQuadSource { - Box::new( - self.0 - .triples() - // NB: for some reason, .map_ok(...) below does not compile since 1.66 nightly - .map(|r| r.map(Triple::into_quad)), - ) - } - - fn quads_matching<'s, S, P, O, G>(&'s self, sm: S, pm: P, om: O, gm: G) -> DQuadSource<'s, Self> + fn quads(&self) -> impl Iterator>> + '_ { + self.0 + .triples() + // NB: for some reason, .map_ok(...) below does not compile since 1.66 nightly + .map(|r| r.map(Triple::into_quad)) + } + + #[allow(refining_impl_trait)] + fn quads_matching<'s, S, P, O, G>( + &'s self, + sm: S, + pm: P, + om: O, + gm: G, + ) -> Box>> + 's> where S: TermMatcher + 's, P: TermMatcher + 's, @@ -73,42 +78,42 @@ where } } - fn subjects(&self) -> DTermSource { + fn subjects(&self) -> impl Iterator>> + '_ { self.0.subjects() } - fn predicates(&self) -> DTermSource { + fn predicates(&self) -> impl Iterator>> + '_ { self.0.predicates() } - fn objects(&self) -> DTermSource { + fn objects(&self) -> impl Iterator>> + '_ { self.0.objects() } - fn graph_names(&self) -> DTermSource { - Box::new(std::iter::empty()) + fn graph_names(&self) -> impl Iterator>> + '_ { + std::iter::empty() } - fn iris(&self) -> DTermSource { + fn iris(&self) -> impl Iterator>> + '_ { self.0.iris() } - fn blank_nodes(&self) -> DTermSource { + fn blank_nodes(&self) -> impl Iterator>> + '_ { self.0.blank_nodes() } - fn literals(&self) -> DTermSource { + fn literals(&self) -> impl Iterator>> + '_ { self.0.literals() } - fn quoted_triples<'s>(&'s self) -> DTermSource<'s, Self> + fn quoted_triples<'s>(&'s self) -> Box>> + '_> where GTerm<'s, T>: Clone, { self.0.quoted_triples() } - fn variables(&self) -> DTermSource { + fn variables(&self) -> impl Iterator>> + '_ { self.0.variables() } } diff --git a/api/src/dataset/test.rs b/api/src/dataset/test.rs index 4cce8c11..eeaba069 100644 --- a/api/src/dataset/test.rs +++ b/api/src/dataset/test.rs @@ -340,9 +340,8 @@ macro_rules! test_dataset_impl { fn quads() -> Result<(), Box> { let d: $dataset_impl = $dataset_collector(some_quads()).unwrap(); - let quads = d.quads(); - let hint = quads.size_hint(); - for iter in [quads, d.quads_matching(Any, Any, Any, Any)] { + for iter in [Box::new(d.quads()) as Box>, Box::new(d.quads_matching(Any, Any, Any, Any))] { + let hint = iter.size_hint(); let v: Vec<_> = iter.map(Result::unwrap).collect(); assert_eq!(v.len(), d.quads().count()); assert_consistent_hint(v.len(), hint); diff --git a/api/src/graph.rs b/api/src/graph.rs index 94017606..d66fed65 100644 --- a/api/src/graph.rs +++ b/api/src/graph.rs @@ -23,12 +23,20 @@ pub type GResult = Result::Error>; /// Type alias for fallible triple iterators produced by a graph. /// /// See [`Graph::triples`] for more information about how to use it. +#[deprecated( + since = "0.8.1", + note = "prototypes of `triples` and `triples_matching` have changed" +)] pub type GTripleSource<'a, G> = Box::Triple<'a>>> + 'a>; /// Type alias for terms produced by a graph. pub type GTerm<'a, G> = <::Triple<'a> as Triple>::Term; /// Type alias for fallible term iterators produced by a graph. /// /// See [`Graph::subjects`] for more information about how to use it. +#[deprecated( + since = "0.8.1", + note = "prototypes of term-yielding methods have changed" +)] pub type GTermSource<'a, G> = Box>> + 'a>; /// Generic trait for RDF graphs. @@ -87,7 +95,7 @@ pub trait Graph { /// # Ok(()) /// # } /// ``` - fn triples(&self) -> GTripleSource; + fn triples(&self) -> impl Iterator>> + '_; /// An iterator visiting all triples matching the given subject, predicate and object. /// See [`crate::term::matcher`]. @@ -144,17 +152,19 @@ pub trait Graph { /// # /// # Ok(()) } /// ``` - fn triples_matching<'s, S, P, O>(&'s self, sm: S, pm: P, om: O) -> GTripleSource<'s, Self> + fn triples_matching<'s, S, P, O>( + &'s self, + sm: S, + pm: P, + om: O, + ) -> impl Iterator>> + 's where S: TermMatcher + 's, P: TermMatcher + 's, O: TermMatcher + 's, { - Box::new( - self.triples().filter_ok(move |t| { - t.matched_by(sm.matcher_ref(), pm.matcher_ref(), om.matcher_ref()) - }), - ) + self.triples() + .filter_ok(move |t| t.matched_by(sm.matcher_ref(), pm.matcher_ref(), om.matcher_ref())) } /// Return `true` if this graph contains the given triple. @@ -174,24 +184,24 @@ pub trait Graph { /// /// NB: implementations SHOULD avoid yielding the same term multiple times, but MAY do so. /// Users MUST therefore be prepared to deal with duplicates. - fn subjects(&self) -> GTermSource { - Box::new(self.triples().map_ok(Triple::to_s)) + fn subjects(&self) -> impl Iterator>> + '_ { + self.triples().map_ok(Triple::to_s) } /// Build a fallible iterator of all the terms used as predicate in this Graph. /// /// NB: implementations SHOULD avoid yielding the same term multiple times, but MAY do so. /// Users MUST therefore be prepared to deal with duplicates. - fn predicates(&self) -> GTermSource { - Box::new(self.triples().map_ok(Triple::to_p)) + fn predicates(&self) -> impl Iterator>> + '_ { + self.triples().map_ok(Triple::to_p) } /// Build a fallible iterator of all the terms used as object in this Graph. /// /// NB: implementations SHOULD avoid yielding the same term multiple times, but MAY do so. /// Users MUST therefore be prepared to deal with duplicates. - fn objects(&self) -> GTermSource { - Box::new(self.triples().map_ok(Triple::to_o)) + fn objects(&self) -> impl Iterator>> + '_ { + self.triples().map_ok(Triple::to_o) } /// Build a fallible iterator of all the IRIs used in this Graph @@ -199,13 +209,11 @@ pub trait Graph { /// /// NB: implementations SHOULD avoid yielding the same term multiple times, but MAY do so. /// Users MUST therefore be prepared to deal with duplicates. - fn iris(&self) -> GTermSource { - Box::new( - self.triples() - .flat_map_ok(Triple::to_spo) - .flat_map_ok(Term::to_atoms) - .filter_ok(Term::is_iri), - ) + fn iris(&self) -> impl Iterator>> + '_ { + self.triples() + .flat_map_ok(Triple::to_spo) + .flat_map_ok(Term::to_atoms) + .filter_ok(Term::is_iri) } /// Build a fallible iterator of all the blank nodes used in this Graph @@ -213,13 +221,11 @@ pub trait Graph { /// /// NB: implementations SHOULD avoid yielding the same term multiple times, but MAY do so. /// Users MUST therefore be prepared to deal with duplicates. - fn blank_nodes(&self) -> GTermSource { - Box::new( - self.triples() - .flat_map_ok(Triple::to_spo) - .flat_map_ok(Term::to_atoms) - .filter_ok(Term::is_blank_node), - ) + fn blank_nodes(&self) -> impl Iterator>> + '_ { + self.triples() + .flat_map_ok(Triple::to_spo) + .flat_map_ok(Term::to_atoms) + .filter_ok(Term::is_blank_node) } /// Build a fallible iterator of all the literals used in this Graph @@ -227,13 +233,11 @@ pub trait Graph { /// /// NB: implementations SHOULD avoid yielding the same term multiple times, but MAY do so. /// Users MUST therefore be prepared to deal with duplicates. - fn literals(&self) -> GTermSource { - Box::new( - self.triples() - .flat_map_ok(Triple::to_spo) - .flat_map_ok(Term::to_atoms) - .filter_ok(Term::is_literal), - ) + fn literals(&self) -> impl Iterator>> + '_ { + self.triples() + .flat_map_ok(Triple::to_spo) + .flat_map_ok(Term::to_atoms) + .filter_ok(Term::is_literal) } /// Build a fallible iterator of all the quoted triples used in this Graph @@ -241,7 +245,7 @@ pub trait Graph { /// /// NB: implementations SHOULD avoid yielding the same term multiple times, but MAY do so. /// Users MUST therefore be prepared to deal with duplicates. - fn quoted_triples<'s>(&'s self) -> GTermSource<'s, Self> + fn quoted_triples<'s>(&'s self) -> Box>> + '_> where GTerm<'s, Self>: Clone, { @@ -258,13 +262,11 @@ pub trait Graph { /// /// NB: implementations SHOULD avoid yielding the same term multiple times, but MAY do so. /// Users MUST therefore be prepared to deal with duplicates. - fn variables(&self) -> GTermSource { - Box::new( - self.triples() - .flat_map_ok(Triple::to_spo) - .flat_map_ok(Term::to_atoms) - .filter_ok(Term::is_variable), - ) + fn variables(&self) -> impl Iterator>> + '_ { + self.triples() + .flat_map_ok(Triple::to_spo) + .flat_map_ok(Term::to_atoms) + .filter_ok(Term::is_variable) } /// [`Dataset`](crate::dataset::Dataset) adapter borrowing this graph @@ -571,13 +573,11 @@ mod check_implementability { type Triple<'x> = [SimpleTerm<'x>; 3] where Self: 'x; type Error = std::convert::Infallible; - fn triples(&self) -> GTripleSource { - Box::new( - self.triples - .iter() - .filter(|t| t.asserted) - .map(|t| Ok(self.make_triple(t.spo))), - ) + fn triples(&self) -> impl Iterator>> + '_ { + self.triples + .iter() + .filter(|t| t.asserted) + .map(|t| Ok(self.make_triple(t.spo))) } } } @@ -700,13 +700,13 @@ mod check_implementability_lazy_term { type Error = std::convert::Infallible; - fn triples(&self) -> GTripleSource { - Box::new(self.triples.iter().filter(|t| t.asserted).map(|t| { + fn triples(&self) -> impl Iterator>> + '_ { + self.triples.iter().filter(|t| t.asserted).map(|t| { Ok(t.spo.map(|i| MyTerm { graph: self, index: i, })) - })) + }) } } } diff --git a/api/src/graph/_foreign_impl.rs b/api/src/graph/_foreign_impl.rs index 43844c85..ac1dc9e4 100644 --- a/api/src/graph/_foreign_impl.rs +++ b/api/src/graph/_foreign_impl.rs @@ -17,11 +17,16 @@ impl<'a, T: Graph + ?Sized> Graph for &'a T { type Error = T::Error; - fn triples(&self) -> GTripleSource { + fn triples(&self) -> impl Iterator>> + '_ { T::triples(*self) } - fn triples_matching<'s, S, P, O>(&'s self, sm: S, pm: P, om: O) -> GTripleSource<'s, Self> + fn triples_matching<'s, S, P, O>( + &'s self, + sm: S, + pm: P, + om: O, + ) -> impl Iterator>> + 's where S: TermMatcher + 's, P: TermMatcher + 's, @@ -39,38 +44,38 @@ impl<'a, T: Graph + ?Sized> Graph for &'a T { T::contains(*self, s, p, o) } - fn subjects(&self) -> GTermSource { + fn subjects(&self) -> impl Iterator>> + '_ { T::subjects(*self) } - fn predicates(&self) -> GTermSource { + fn predicates(&self) -> impl Iterator>> + '_ { T::predicates(*self) } - fn objects(&self) -> GTermSource { + fn objects(&self) -> impl Iterator>> + '_ { T::objects(*self) } - fn iris(&self) -> GTermSource { + fn iris(&self) -> impl Iterator>> + '_ { T::iris(*self) } - fn blank_nodes(&self) -> GTermSource { + fn blank_nodes(&self) -> impl Iterator>> + '_ { T::blank_nodes(*self) } - fn literals(&self) -> GTermSource { + fn literals(&self) -> impl Iterator>> + '_ { T::literals(*self) } - fn quoted_triples<'s>(&'s self) -> GTermSource<'s, Self> + fn quoted_triples<'s>(&'s self) -> Box>> + '_> where GTerm<'s, Self>: Clone, { T::quoted_triples(*self) } - fn variables(&self) -> GTermSource { + fn variables(&self) -> impl Iterator>> + '_ { T::variables(*self) } } @@ -81,11 +86,16 @@ impl<'a, T: Graph + ?Sized> Graph for &'a mut T { type Error = T::Error; - fn triples(&self) -> GTripleSource { + fn triples(&self) -> impl Iterator>> + '_ { T::triples(*self) } - fn triples_matching<'s, S, P, O>(&'s self, sm: S, pm: P, om: O) -> GTripleSource<'s, Self> + fn triples_matching<'s, S, P, O>( + &'s self, + sm: S, + pm: P, + om: O, + ) -> impl Iterator>> + 's where S: TermMatcher + 's, P: TermMatcher + 's, @@ -103,38 +113,38 @@ impl<'a, T: Graph + ?Sized> Graph for &'a mut T { T::contains(*self, s, p, o) } - fn subjects(&self) -> GTermSource { + fn subjects(&self) -> impl Iterator>> + '_ { T::subjects(*self) } - fn predicates(&self) -> GTermSource { + fn predicates(&self) -> impl Iterator>> + '_ { T::predicates(*self) } - fn objects(&self) -> GTermSource { + fn objects(&self) -> impl Iterator>> + '_ { T::objects(*self) } - fn iris(&self) -> GTermSource { + fn iris(&self) -> impl Iterator>> + '_ { T::iris(*self) } - fn blank_nodes(&self) -> GTermSource { + fn blank_nodes(&self) -> impl Iterator>> + '_ { T::blank_nodes(*self) } - fn literals(&self) -> GTermSource { + fn literals(&self) -> impl Iterator>> + '_ { T::literals(*self) } - fn quoted_triples<'s>(&'s self) -> GTermSource<'s, Self> + fn quoted_triples<'s>(&'s self) -> Box>> + '_> where GTerm<'s, Self>: Clone, { T::quoted_triples(*self) } - fn variables(&self) -> GTermSource { + fn variables(&self) -> impl Iterator>> + '_ { T::variables(*self) } } @@ -206,8 +216,8 @@ impl Graph for [T] { type Error = Infallible; type Triple<'x> = [TBorrowTerm<'x, T>; 3] where Self: 'x; - fn triples(&self) -> GTripleSource { - Box::new(self.iter().map(Triple::spo).map(Ok)) + fn triples(&self) -> impl Iterator>> + '_ { + self.iter().map(Triple::spo).map(Ok) } } @@ -217,7 +227,7 @@ impl Graph for Vec { type Error = Infallible; type Triple<'x> = [TBorrowTerm<'x, T>; 3] where Self: 'x; - fn triples(&self) -> GTripleSource { + fn triples(&self) -> impl Iterator>> + '_ { self[..].triples() } } @@ -281,8 +291,8 @@ impl Graph for HashSet { type Error = Infallible; type Triple<'x> = [TBorrowTerm<'x, T>; 3] where Self: 'x; - fn triples(&self) -> GTripleSource { - Box::new(self.iter().map(Triple::spo).map(Ok)) + fn triples(&self) -> impl Iterator>> + '_ { + self.iter().map(Triple::spo).map(Ok) } } @@ -342,8 +352,8 @@ impl Graph for BTreeSet { type Error = Infallible; type Triple<'x> = [TBorrowTerm<'x, T>; 3] where Self: 'x; - fn triples(&self) -> GTripleSource { - Box::new(self.iter().map(Triple::spo).map(Ok)) + fn triples(&self) -> impl Iterator>> + '_ { + self.iter().map(Triple::spo).map(Ok) } } diff --git a/api/src/graph/adapter.rs b/api/src/graph/adapter.rs index d6694ddb..dad1f400 100644 --- a/api/src/graph/adapter.rs +++ b/api/src/graph/adapter.rs @@ -30,61 +30,62 @@ impl Graph for UnionGraph { type Triple<'x> = [DTerm<'x, T>; 3] where Self: 'x; type Error = T::Error; - fn triples(&self) -> GTripleSource { - Box::new( - self.0 - .quads() - // NB: for some reason, .map_ok(...) below does not compile since 1.66 nightly - .map(|r| r.map(Quad::into_triple)), - ) + fn triples(&self) -> impl Iterator>> + '_ { + self.0 + .quads() + // NB: for some reason, .map_ok(...) below does not compile since 1.66 nightly + .map(|r| r.map(Quad::into_triple)) } - fn triples_matching<'s, S, P, O>(&'s self, sm: S, pm: P, om: O) -> GTripleSource<'s, Self> + fn triples_matching<'s, S, P, O>( + &'s self, + sm: S, + pm: P, + om: O, + ) -> impl Iterator>> + 's where S: TermMatcher + 's, P: TermMatcher + 's, O: TermMatcher + 's, { - Box::new( - self.0 - .quads_matching(sm, pm, om, Any) - // NB: for some reason, .map_ok(...) below does not compile since 1.66 nightly - .map(|r| r.map(Quad::into_triple)), - ) + self.0 + .quads_matching(sm, pm, om, Any) + // NB: for some reason, .map_ok(...) below does not compile since 1.66 nightly + .map(|r| r.map(Quad::into_triple)) } - fn subjects(&self) -> GTermSource { + fn subjects(&self) -> impl Iterator>> + '_ { self.0.subjects() } - fn predicates(&self) -> GTermSource { + fn predicates(&self) -> impl Iterator>> + '_ { self.0.predicates() } - fn objects(&self) -> GTermSource { + fn objects(&self) -> impl Iterator>> + '_ { self.0.objects() } - fn iris(&self) -> GTermSource { + fn iris(&self) -> impl Iterator>> + '_ { self.0.iris() } - fn blank_nodes(&self) -> GTermSource { + fn blank_nodes(&self) -> impl Iterator>> + '_ { self.0.blank_nodes() } - fn literals(&self) -> GTermSource { + fn literals(&self) -> impl Iterator>> + '_ { self.0.literals() } - fn quoted_triples<'s>(&'s self) -> GTermSource<'s, Self> + fn quoted_triples<'s>(&'s self) -> Box>> + '_> where GTerm<'s, Self>: Clone, { self.0.quoted_triples() } - fn variables(&self) -> GTermSource { + fn variables(&self) -> impl Iterator>> + '_ { self.0.variables() } } @@ -117,27 +118,28 @@ impl Graph for PartialUnionGraph { type Triple<'x> = [DTerm<'x, D>; 3] where Self: 'x; type Error = D::Error; - fn triples(&self) -> GTripleSource { - Box::new( - self.d - .quads_matching(Any, Any, Any, self.m) - // NB: for some reason, .map_ok(...) below does not compile since 1.66 nightly - .map(|r| r.map(Quad::into_triple)), - ) + fn triples(&self) -> impl Iterator>> + '_ { + self.d + .quads_matching(Any, Any, Any, self.m) + // NB: for some reason, .map_ok(...) below does not compile since 1.66 nightly + .map(|r| r.map(Quad::into_triple)) } - fn triples_matching<'s, S, P, O>(&'s self, sm: S, pm: P, om: O) -> GTripleSource<'s, Self> + fn triples_matching<'s, S, P, O>( + &'s self, + sm: S, + pm: P, + om: O, + ) -> impl Iterator>> + 's where S: TermMatcher + 's, P: TermMatcher + 's, O: TermMatcher + 's, { - Box::new( - self.d - .quads_matching(sm, pm, om, self.m) - // NB: for some reason, .map_ok(...) below does not compile since 1.66 nightly - .map(|r| r.map(Quad::into_triple)), - ) + self.d + .quads_matching(sm, pm, om, self.m) + // NB: for some reason, .map_ok(...) below does not compile since 1.66 nightly + .map(|r| r.map(Quad::into_triple)) } } @@ -181,27 +183,28 @@ impl Graph for DatasetGraph { type Triple<'x> = [DTerm<'x, D>; 3] where Self: 'x; type Error = D::Error; - fn triples(&self) -> GTripleSource { - Box::new( - self.d - .quads_matching(Any, Any, Any, [self.g()]) - // NB: for some reason, .map_ok(...) below does not compile since 1.66 nightly - .map(|r| r.map(Quad::into_triple)), - ) + fn triples(&self) -> impl Iterator>> + '_ { + self.d + .quads_matching(Any, Any, Any, [self.g()]) + // NB: for some reason, .map_ok(...) below does not compile since 1.66 nightly + .map(|r| r.map(Quad::into_triple)) } - fn triples_matching<'s, S, P, O>(&'s self, sm: S, pm: P, om: O) -> GTripleSource<'s, Self> + fn triples_matching<'s, S, P, O>( + &'s self, + sm: S, + pm: P, + om: O, + ) -> impl Iterator>> + 's where S: TermMatcher + 's, P: TermMatcher + 's, O: TermMatcher + 's, { - Box::new( - self.d - .quads_matching(sm, pm, om, [self.g()]) - // NB: for some reason, .map_ok(...) below does not compile since 1.66 nightly - .map(|r| r.map(Quad::into_triple)), - ) + self.d + .quads_matching(sm, pm, om, [self.g()]) + // NB: for some reason, .map_ok(...) below does not compile since 1.66 nightly + .map(|r| r.map(Quad::into_triple)) } } diff --git a/api/src/graph/test.rs b/api/src/graph/test.rs index da8d53f9..4bbb99aa 100644 --- a/api/src/graph/test.rs +++ b/api/src/graph/test.rs @@ -318,7 +318,7 @@ macro_rules! test_graph_impl { fn triples() -> Result<(), Box> { let g: $graph_impl = $graph_collector(some_triples()).unwrap(); - for iter in [g.triples(), g.triples_matching(Any, Any, Any)] { + for iter in [Box::new(g.triples()) as Box>, Box::new(g.triples_matching(Any, Any, Any))] { let hint = iter.size_hint(); let v: Vec<_> = iter.map(Result::unwrap).collect(); assert_eq!(v.len(), SOME_TRIPLES_COUNT); diff --git a/inmem/src/dataset.rs b/inmem/src/dataset.rs index ca44abbc..e537f84b 100644 --- a/inmem/src/dataset.rs +++ b/inmem/src/dataset.rs @@ -2,7 +2,7 @@ use std::collections::BTreeSet; use std::iter::{empty, once}; -use sophia_api::dataset::{CollectibleDataset, SetDataset}; +use sophia_api::dataset::{CollectibleDataset, DResult, SetDataset}; use sophia_api::prelude::*; use sophia_api::quad::Gspo; use sophia_api::term::GraphName; @@ -34,22 +34,23 @@ impl Dataset for GenericLightDataset { type Quad<'x> = Gspo<::BorrowTerm<'x>> where Self: 'x; type Error = TI::Error; - fn quads(&self) -> sophia_api::dataset::DQuadSource { - Box::new(self.quads.iter().map(|[gi, ti @ ..]| { + fn quads(&self) -> impl Iterator>> + '_ { + self.quads.iter().map(|[gi, ti @ ..]| { Ok(( self.terms.get_graph_name(*gi), ti.map(|i| self.terms.get_term(i)), )) - })) + }) } + #[allow(refining_impl_trait)] fn quads_matching<'s, S, P, O, G>( &'s self, sm: S, pm: P, om: O, gm: G, - ) -> sophia_api::dataset::DQuadSource<'s, Self> + ) -> Box>> + 's> where S: sophia_api::term::matcher::TermMatcher + 's, P: sophia_api::term::matcher::TermMatcher + 's, @@ -223,22 +224,23 @@ impl Dataset for GenericFastDataset { type Quad<'x> = Gspo<::BorrowTerm<'x>> where Self: 'x; type Error = TI::Error; - fn quads(&self) -> sophia_api::dataset::DQuadSource { - Box::new(self.gspo.iter().map(|[gi, ti @ ..]| { + fn quads(&self) -> impl Iterator>> + '_ { + self.gspo.iter().map(|[gi, ti @ ..]| { Ok(( self.terms.get_graph_name(*gi), ti.map(|i| self.terms.get_term(i)), )) - })) + }) } + #[allow(refining_impl_trait)] fn quads_matching<'s, S, P, O, G>( &'s self, sm: S, pm: P, om: O, gm: G, - ) -> sophia_api::dataset::DQuadSource<'s, Self> + ) -> Box>> + 's> where S: sophia_api::term::matcher::TermMatcher + 's, P: sophia_api::term::matcher::TermMatcher + 's, diff --git a/inmem/src/graph.rs b/inmem/src/graph.rs index 434d86e0..27d7310a 100644 --- a/inmem/src/graph.rs +++ b/inmem/src/graph.rs @@ -2,7 +2,7 @@ use std::collections::BTreeSet; use std::iter::{empty, once}; -use sophia_api::graph::{CollectibleGraph, SetGraph}; +use sophia_api::graph::{CollectibleGraph, GResult, SetGraph}; use sophia_api::prelude::*; use crate::index::*; @@ -33,20 +33,19 @@ impl Graph for GenericLightGraph { type Triple<'x> = [::BorrowTerm<'x>; 3] where Self: 'x; type Error = TI::Error; - fn triples(&self) -> sophia_api::graph::GTripleSource { - Box::new( - self.triples - .iter() - .map(|ti| Ok(ti.map(|i| self.terms.get_term(i)))), - ) + fn triples(&self) -> impl Iterator>> + '_ { + self.triples + .iter() + .map(|ti| Ok(ti.map(|i| self.terms.get_term(i)))) } + #[allow(refining_impl_trait)] fn triples_matching<'s, S, P, O>( &'s self, sm: S, pm: P, om: O, - ) -> sophia_api::graph::GTripleSource<'s, Self> + ) -> Box>> + '_> where S: sophia_api::term::matcher::TermMatcher + 's, P: sophia_api::term::matcher::TermMatcher + 's, @@ -168,20 +167,19 @@ impl Graph for GenericFastGraph { type Triple<'x> = [::BorrowTerm<'x>; 3] where Self: 'x; type Error = TI::Error; - fn triples(&self) -> sophia_api::graph::GTripleSource { - Box::new( - self.spo - .iter() - .map(|ti| Ok(ti.map(|i| self.terms.get_term(i)))), - ) + fn triples(&self) -> impl Iterator>> + '_ { + self.spo + .iter() + .map(|ti| Ok(ti.map(|i| self.terms.get_term(i)))) } + #[allow(refining_impl_trait)] fn triples_matching<'s, S, P, O>( &'s self, sm: S, pm: P, om: O, - ) -> sophia_api::graph::GTripleSource<'s, Self> + ) -> Box>> + '_> where S: sophia_api::term::matcher::TermMatcher + 's, P: sophia_api::term::matcher::TermMatcher + 's,