pub trait CursorFactory<K: ?Sized, V: ?Sized, T, R: ?Sized> {
// Required method
fn get_cursor<'a>(&'a self) -> Box<dyn Cursor<K, V, T, R> + 'a>;
}Expand description
An object that can produce a cursor bounded by its lifetime.
BatchReader::cursor produces a cursor bounded by the BatchReader’s
lifetime. Suppose BatchReader::cursor could use an existing Cursor
implementation, such as CursorList, but it needs to create some data for
the cursor to own. Then it’s rather inconvenient because it’s necessary to
create a new type to own the data and implement the whole broad Cursor
trait to forward every call to CursorList. Plus, you end up with a
self-referencing type, so you need to use ouroboros. See SpineCursor in
the async spine for a good example.
Suppose we’re building a new interface where we want to return a dyn Cursor, and some of the implementations would suffer from the problem
above. We can instead return a CursorFactory. An implementation of this
trait can own the data that it needs to, and then implement
CursorFactory::get_cursor to create and return a cursor whose lifetime is
bounded by the CursorFactory. This reduces the boilerplate a great deal
(and we don’t need ouroboros, either).
BatchReader::cursor could be retrofitted to this interface, but it’s not
clear that it’s worth it, especially since it forces using dyn Cursor.