pchampin / sophia_rs

Sophia: a Rust toolkit for RDF and Linked Data
Other
228 stars 24 forks source link

How to handle read-only Terms #68

Closed MattesWhite closed 4 years ago

MattesWhite commented 4 years ago

During #55 the question arose how should read-only Term references be handled.

Note: This only affects situation where a Term is passed in to check and compare the contents of it. Not when the Term (reference) is passed in to be copy/clone in some way.

Currently, there are two approaches in sophia:

  1. Take a reference to a monomorphized Term, e.g. Graph::triples_with_spo<'s, T, U, V>(&'s self, s: &'s Term<T>, p: &'s Term<U>, o: &'s Term<V>) -> GTripleSource<'s, Self>. This version allows to directly pass references without the need to create an intermediate representation. However, this can increase compile times and code size significantly.
  2. Take a RefTerm. This prevents monomorphization. However, it requires to build an intermediate, 76 Byte big RefTerm.

An alternative approach would be having a TermTrait, so we could pass &dyn TermTrait in such situations. This would prevent monomorphization and does not require an intermediate RefTerm. However, a definition of such a trait is still to be figured out.

pchampin commented 4 years ago

In general, I think that the API should not impose the use of trait objects. Assuming we have a Term trait, having functions accept &T where T: Term is the best option: people who want compile-time optimization can pass references to concrete types; people who want to avoid monomorphization can restrict themselves to passing exclusively dyn references. Everyone can have it their way.

I made one exception to that rule: Graph/Dataset methods return a boxed iterator (hence a trait object), because 1/ it allows for a default implementation, and 2/ the alternative was horribly cumbersome (declaring a bunch of associated types...).

pchampin commented 4 years ago

Can we close this, then?