Thanks!
X
Paypal
Github sponshorship
Patreon

articles

geos 7.0 release: More type safety, update dependencies and use std TryFrom

The new geos version is now here! As a reminder, geos is a binding of the C++ geos library which provides tools to perform geospatial operations. I'm very proud of this release for a few reasons that I'll now describe.

More type safety

Thanks to a contributor, we realized that the FFI bindings were not always perfectly matching the C headers. It was mostly differences between mutable and constant pointers, but it was still a huge deal! The biggest impact was on the Geometry type: in some cases, you can return a Geometry which doesn't own its data, it's just a "view" over the data of another Geometry. For example:

Runuse geos::{Geom, Geometry};

let geom = Geometry::new_from_wkt("MULTIPOINT(1 1, 2 2, 3 3, 4 4)")
                    .expect("Invalid geometry");
let point_nb3 = geom
    .get_geometry_n(2)
    .expect("failed to get third point");

In here, point_nb3 is just a "view" over some of the data owned by geom. Before this release, I kept around a field is_owned: bool inside Geometry to know if I should free the data or not, and the data was always a mutable pointer. Now I think you understand the issue: get_geometry_n returns a non-mutable pointer, meaning
that I can't store it inside the Geometry type. Which is why I created a new ConstGeometry type to store such cases.

So now, we have both Geometry and ConstGeometry. To make the whole still as easy as possible to use, I created a new Geom trait and updated all functions which expected a Geometry to now expect a type implementing Geom. The Geom trait provides all the previous methods of Geometry that didn't require a mutable object.

Switch to libstd TryInto/TryFrom

When I released the previous version of geos, the TryFrom and TryInto traits were still not stable, so I provided a custom TryInto trait which was basically the same, but not coming from the libstd. This is now "fixed", meaning that we get the interoperability between TryFrom and TryInto. As a reminder:

Runtype::try_into(another_type) == another_type::try_from(type)

Update dependencies

geos provides conversions from/to other crates' types which allow to perform geospatial operations as well through features, such as wkt and geo-types. I used this opportunity to update those two dependencies, so now we are up to the last version.

Conclusion

That's it for this release! It's been a very big job to make it all come together, especially making the whole new ConstGeometry type and the new Geom trait work as I wanted them to with the most little impact possible on users. I'm very pleased with the end result, so I guess the mission is now complete!

If you're interested to check the pull requests behind this release, here they are:

Posted on the 31/07/2020 at 12:00 by @GuillaumeGomez
Back to articles list