Expand description
A Source yields items, and may also fail in the process.
This module also provides two specialized kinds of source:
TripleSource and QuadSource.
The Source trait provides an API similar to (a subset of) the Iterator API,
with methods such as for_each_item and try_for_each_item.
TripleSource and QuadSource provides specialized alternative
(e.g. for_each_triple and for_each_quad, respectively).
§Rationale (or Why not simply use Iterator?)
The Iterator trait is designed in such a way that items must live at least as long as the iterator itself.
This assumption may be too strong in some situations.
For example,
consider a parser using 3 buffers to store the subject, predicate,
and object of the triples it parses.
Each time it extracts a triple from the data,
it yields it (as 3 references to its internal buffers)
to the closure passed to for_each_triple.
Then, it reuses the buffers to store the data for the next triple,
and yields the new triple, as 3 references to the same buffers.
Such a parser can not implement Iterator,
because, once yielded by the iterator’s next method,
an item is free to live during further iterations.
In particular, it can be stored in a collection,
and still be alive when the next method is called again
(consider for example the Iterator::collect method).
Because many parsers (as well as other triple/quad sources) will be implemented in a manner similar to that described above, we have to provide a trait with weaker assumptions on the lifetime of the yielded triples.
The alternative would be to wrap such parsers with a layer that would copy the data from internal buffers to fresh buffers for each triples, but we do not want to impose that cost on all implementations — especially when many consumers will be happy with short-lived references.
§About the design of TripleSource and QuadSource
The design of TripleSource (resp. QuadSource) may seem overcomplicated,
but it aims to have the following properties.
- When a concrete type implements
Sourceand its items implementTriple, then it is automatically recognized as aTripleSource. - When a
TripleSourceis required, it can be used as a simple trait bound, without requiring the user to add a higher ranked trait bound (HRTB) likefor <'x> S:Item<'x>: Triple.
Re-exports§
pub use StreamError::*;
Modules§
- convert
- I define
ToQuadsandToQuads, the result type ofTripleSource::to_quadsandQuadSource::to_triplesrespectively. - filter
- I define
FilterSource, the result type ofSource::filter_items. I also defineFilterTripleSourceandFilterQuadSource, which are required to ensure that the output ofTripleSource::filter_triplesandQuadSource::filter_quadsare recognized as aTripleSourceand aQuadSource, respectively. - filter_
map - I define
FilterMapSourcethe result type ofSource::filter_map_items. - map
- I define
MapSource, the result type ofSource::map_items.
Enums§
- Stream
Error - A error that is raised by functions that move fallible
Sources into fallibleSinks.
Traits§
- Into
Source - An extension trait for iterators,
converting them to an
InfallibleSource. - Quad
Source - A quad source is a
Sourceproducing quads. - Source
- A source produces items, and may also fail in the process.
- Stream
Result Ext - Additional methods for
StreamResult - Triple
Source - A triple source is a
Sourceproducing triples.
Type Aliases§
- QSQuad
- Type alias to denote the type of quads yielded by a
QuadSource. - Stream
Result - Convenient type alias for
Resultwhose error isStreamError. - TSTriple
- Type alias to denote the type of triples yielded by a
TripleSource.