I'm trying to write a wrapper around gdal::vector
code, so that the caller doesn't have to deal with all these intermediate objects, and just iterate over, say, features.
Here's the code that I want to wrap (a simplified version of this gdal code from the docs):
let dataset = Dataset::open("fixtures/roads.geojson")?;
let mut layer = dataset.layer(0)?;
for feature in layer.features() { // returns FeatureIterator
...
Now I wrote this, and it won't compile, because Dataset
is borrowed by Layer
, and Layer
is borrowed by FeatureIterator
, and they move when the function returns.
struct GpkgDriver<'a> {
dataset: Dataset,
layer: Layer<'a>,
fi: FeatureIterator<'a>,
}
impl<'a> GpkgDriver<'a> {
fn from_path(path: &str) -> Result<Self, Box<dyn Error>> {
let dataset = Dataset::open(path)?;
let mut layer = dataset.layer(0)?;
let fi = layer.features();
Ok(Self { dataset, layer, fi })
}
}
In the original example in the docs, dataset
and layer
live in main()
and essentially are 'static
, but I'd like not to abuse this.
I've checked the docs, and in case of GDAL library, there's no alternative like into_owned...
.
Wrapping dataset
into Arc
or Box
doesn't resolve the issue. And I need an iterator, so no matter if I make a big function or closure, eventually dataset
will be returned and moved.
What is a reasonable workaround for this, so that the outer user don't have to keep dataset
and layer
?