Skip to content

Commit

Permalink
Remove Transaction::dataset{,_mut} in favour of Deref{,Mut}
Browse files Browse the repository at this point in the history
  • Loading branch information
ttencate committed Mar 15, 2022
1 parent 492e0c8 commit 3bd87ec
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

- https://github.com/georust/gdal/pull/238

- **Breaking**: Remove `Transaction::dataset` and `Transaction::dataset_mut`. Add `Deref` and `DerefMut` implementations instead.

## 0.12

- Bump Rust edition to 2021
Expand Down
53 changes: 29 additions & 24 deletions src/dataset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::mem::MaybeUninit;
use std::{
ffi::NulError,
ffi::{CStr, CString},
ops::{Deref, DerefMut},
path::Path,
ptr,
};
Expand Down Expand Up @@ -641,11 +642,11 @@ impl Dataset {

/// For datasources which support transactions, this creates a transaction.
///
/// During the transaction, the dataset can be mutably borrowed using
/// [`Transaction::dataset_mut`] to make changes. All changes done after the start of the
/// transaction are applied to the datasource when [`commit`](Transaction::commit) is called.
/// They may be canceled by calling [`rollback`](Transaction::rollback) instead, or by dropping
/// the `Transaction` without calling `commit`.
/// Because the transaction implements `DerefMut`, it can be used in place of the original
/// `Dataset` to make modifications. All changes done after the start of the transaction are
/// applied to the datasource when [`commit`](Transaction::commit) is called. They may be
/// canceled by calling [`rollback`](Transaction::rollback) instead, or by dropping the
/// `Transaction` without calling `commit`.
///
/// Depending on the driver, using a transaction can give a huge performance improvement when
/// creating a lot of geometry at once. This is because the driver doesn't need to commit every
Expand Down Expand Up @@ -681,7 +682,7 @@ impl Dataset {
/// // Start the transaction.
/// let mut txn = dataset.start_transaction()?;
///
/// let mut layer = txn.dataset_mut().create_layer(LayerOptions {
/// let mut layer = txn.create_layer(LayerOptions {
/// name: "grid",
/// ty: gdal_sys::OGRwkbGeometryType::wkbPoint,
/// ..Default::default()
Expand Down Expand Up @@ -888,8 +889,8 @@ impl Drop for Dataset {
/// back.
///
/// The transaction holds a mutable borrow on the `Dataset` that it was created from, so during the
/// lifetime of the transaction you will need to access the dataset through
/// [`Transaction::dataset`] or [`Transaction::dataset_mut`].
/// lifetime of the transaction you will need to access the dataset by dereferencing the
/// `Transaction` through its [`Deref`] or [`DerefMut`] implementations.
#[derive(Debug)]
pub struct Transaction<'a> {
dataset: &'a mut Dataset,
Expand All @@ -904,16 +905,6 @@ impl<'a> Transaction<'a> {
}
}

/// Returns a reference to the dataset from which this `Transaction` was created.
pub fn dataset(&self) -> &Dataset {
self.dataset
}

/// Returns a mutable reference to the dataset from which this `Transaction` was created.
pub fn dataset_mut(&mut self) -> &mut Dataset {
self.dataset
}

/// Commits this transaction.
///
/// If the commit fails, will return [`OGRErr::OGRERR_FAILURE`].
Expand Down Expand Up @@ -947,6 +938,20 @@ impl<'a> Transaction<'a> {
}
}

impl<'a> Deref for Transaction<'a> {
type Target = Dataset;

fn deref(&self) -> &Self::Target {
self.dataset
}
}

impl<'a> DerefMut for Transaction<'a> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.dataset
}
}

impl<'a> Drop for Transaction<'a> {
fn drop(&mut self) {
if self.rollback_on_drop {
Expand Down Expand Up @@ -1138,8 +1143,8 @@ mod tests {
let (_temp_path, mut ds) = open_gpkg_for_update(fixture!("poly.gpkg"));
let orig_feature_count = ds.layer(0).unwrap().feature_count();

let mut txn = ds.start_transaction().unwrap();
let mut layer = txn.dataset_mut().layer(0).unwrap();
let txn = ds.start_transaction().unwrap();
let mut layer = txn.layer(0).unwrap();
layer.create_feature(polygon()).unwrap();
assert!(txn.commit().is_ok());

Expand All @@ -1151,8 +1156,8 @@ mod tests {
let (_temp_path, mut ds) = open_gpkg_for_update(fixture!("poly.gpkg"));
let orig_feature_count = ds.layer(0).unwrap().feature_count();

let mut txn = ds.start_transaction().unwrap();
let mut layer = txn.dataset_mut().layer(0).unwrap();
let txn = ds.start_transaction().unwrap();
let mut layer = txn.layer(0).unwrap();
layer.create_feature(polygon()).unwrap();
assert!(txn.rollback().is_ok());

Expand All @@ -1165,8 +1170,8 @@ mod tests {
let orig_feature_count = ds.layer(0).unwrap().feature_count();

{
let mut txn = ds.start_transaction().unwrap();
let mut layer = txn.dataset_mut().layer(0).unwrap();
let txn = ds.start_transaction().unwrap();
let mut layer = txn.layer(0).unwrap();
layer.create_feature(polygon()).unwrap();
} // txn is dropped here.

Expand Down

0 comments on commit 3bd87ec

Please sign in to comment.