Skip to content

Conversation

wprzytula
Copy link
Collaborator

This is a bunch of small independent refactors that aim to increase type safety, readability and robustness.

Pre-review checklist

  • I have split my patch into logically separate commits.
  • All commit messages clearly explain what they change and why.
  • PR description sums up the changes and reasons why they should be introduced.
  • [ ] I have implemented Rust unit tests for the features/changes introduced.
  • [ ] I have enabled appropriate tests in Makefile in {SCYLLA,CASSANDRA}_(NO_VALGRIND_)TEST_FILTER.
  • [ ] I added appropriate Fixes: annotations to PR description.

@wprzytula wprzytula requested a review from Lorak-mmk July 15, 2025 12:15
@wprzytula wprzytula self-assigned this Jul 15, 2025
@wprzytula wprzytula added this to the 0.6 milestone Jul 15, 2025
Before our FFI framework was introduced, I had written some heuristic
safeguards in the code to avoid use-after-free errors. They are no
longer needed now, as the borrow checker fully guarantees that
no UAF can happen in those cases.
The CPP Driver unsafely dereferences the callback pointer even if it is
null. Our Rust wrapper `unwrap()`s the Option with the callback, so it
panics in such case. This commit changes the code to return an error
upon passing a null callback pointer to `cass_future_set_callback`.
Then, the callback is always non-null if set in the driver, so no panic
is possible.

This should be considered an *improvement* compared to the CPP Driver.
let-else is the idiomatic way to handle early error returns in Rust,
which in general makes the code cleaner and more readable.
@wprzytula wprzytula requested a review from Lorak-mmk July 15, 2025 13:00
The code of `cass_statement_set_paging_state_token` was unsetting the
paging state on a statement if a null paging state pointer was passed,
but it also returned an error in this case.
This is inconsistent with the behavior of other setters, which either
preserve the existing state and return an error if a null pointer is
passed, or unset the state and return CASS_OK.
For consistency and ease of use, the code is changed to unset paging
state when null is provided.
As a bonus, to mimic CPP Driver behaviour, empty strings now also
unset the paging state.

Note: CPP Driver does not check the passed pointer for null at all.
Result is more idiomatic in Rust to represent a fallible operation.
Converting an integer to a Rust enum can definitely fail, so using
`Result` is more appropriate than `Option`.
let-else is the idiomatic way to handle early error returns in Rust,
which can make the code cleaner and more readable.
The driver was using the C enum `CassCollectionType` directly in its
`CassCollection` struct, which led to necessity of handling the "else"
case (values other than the explicitly listed variants) in many places.

To bring type safety to the driver internals, this commit introduces a
new Rust `CollectionType` enum, which is used internally instead of the
C enum.
After introducing CollectionType and ensuring type safety for
collections, we can simplify the conversion from `&CassCollection` to
`CassCqlValue` by removing the `TryFrom` trait and using `From` instead.
This change reflects that the conversion is always valid as long as the
collection is well-formed.
There existed an `external.rs` file that only contained the
`cass_error_desc` function. As other error-related functions reside
in `execution_error.rs`, it makes sense to move it there as well.
The `external.rs` file is now deleted.
The unwrap can be avoided by reordering the code slightly
and operating on the `val` directly, not through
the `CassBorrowedSharedPtr`.
This commit refactors `cass_iterator_*` functions to use let-else syntax
for better readability.
The error message should be constructed by the caller if needed,
and explain the context of the error in more abstract terms, not
such low-level wrapper implementation details.
@wprzytula
Copy link
Collaborator Author

Altered the commit with changes to cass_statement_set_paging_state_token so that null strings and empty strings are both now correct arguments that unset the paging state.

@wprzytula wprzytula merged commit 9c74e2f into scylladb:master Jul 16, 2025
11 checks passed
@wprzytula wprzytula deleted the minor-refactors branch July 16, 2025 10:16
@wprzytula wprzytula modified the milestones: 0.6, 0.5.1 Jul 16, 2025
@wprzytula wprzytula mentioned this pull request Jul 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants