georust / gdal

Rust bindings for GDAL
https://crates.io/crates/gdal
MIT License
339 stars 92 forks source link

A Consistent and Principled Approach to GDAL C Pointers #445

Closed metasim closed 4 months ago

metasim commented 9 months ago

Note: This PR is likely to be abandoned. While the foreign-types changes work well with the raster-related types, the ownership model behind the vector and mdarray types is sufficiently complicated that a transition would require more interested participants than currently available, and is likely not worth the effort at this time.


Remaining Tasks:

Background

The Rust bindings to the GDAL library have been developed and maintained by many contributors over a time span of almost a decade (the first commit on record is March 25, 2014). As is normal in such a situation, inconsistencies and diverging principles arise. The scope of this PR is to address inconsistencies in the handling of pointers to C-instantiated GDAL types, making use use of the foreign-types crate as a means.

The foreign-types crate is mature and widely used, with 45 public dependents (including openssl), approximately 120,000 daily downloads, and > 65 million total downloads. It is dual licensed under Apache and MIT licenses. It provides an opinionated set of types and traits for wrapping pointers to C memory, with support for borrowed vs owned references, Clone and Drop, with optional proc-macros to eliminate some boilerplate. The included documentation and examples are somewhat sparse, but the maintainer is responsive to questions. If you dig deep enough (including the very large projects that use the crate), you can find good examples. While the names and abstractions declared in foreign-types may not be to the preferences of all users, maturity, consistency and friction-lowering were given primacy in considering foreign-types.

Note: For simplicity I use the term "pointer" to include memory addresses, handles, or any other identifier to a C-allocated resource in GDAL.

Goals

The impetus for this PR is several logged issues around pointer handling:

This PR aims to provide foundational work helpful in addressing these issues, primarily focusing on "safe consistency". Specifically:

Benefits

The intended benefits are:

Scope

This PR migrates the following types to use foreign-types

(See Remaining Tasks below)

Note: Because of this scope, many APIs will break, necessitating clear documentation and appropriate semantic version handling (albeit we're still < 1.0).


Notes on Migration

These are some thoughts I had about the migration experience, some of which are copied from the related discussion on this topic. I include them here for those interested in the deeper deails behind certain implementation decisions.

metasim commented 4 months ago

While I think the approaches herein are ones we should adopt, I think it's too late (with volunteer labor) to apply a unified approach across the whole library. While the porting raster bindings was pretty straight forward, the vector and mdarray bindings are complicated enough to require the original author of those to do the work, or explain how ownership/exclusive access should be handled to someone else to take on the work.