scylladb / cpp-rust-driver

API-compatible rewrite of https://github.com/scylladb/cpp-driver as a wrapper for Rust driver.
GNU Lesser General Public License v2.1
11 stars 11 forks source link

Put content of CassDataType into UnsafeCell #98

Closed Lorak-mmk closed 1 year ago

Lorak-mmk commented 1 year ago

Previously CassDataType was just an enum, held inside Arc. User was given a pointer to CassDataType using Arc::as_ptr or Arc::into_ptr. There are however some functions that mutate the data - and they were given the very same pointers. Current code was most likely sound - but I'm not completely sure, Rust reference is very confusing in this aspect. It was however very confusing - when a programmer reads or writes a function that that *mut CassDataType it is not obivious that this data lies inside Arc and so has shared ownership.

To make this more explicit this commit puts CassDataType inside UnsafeCell. Now each access needs to use .get_unchecked() and .get_mut_unchecked() methods and an unsafe block / function, so it will be easier to spot aliasing ^ mutability problems in the future.

In the future we can use Arc::get_mut_unchecked() for this purpose, but it's not yet stabilised.

Pre-review checklist

Lorak-mmk commented 1 year ago

Closing for now - I want to include this in the whole safety refactor after it's finished.