Skip to content

[MOD-10008] Reduce the amount of unsafe code by using more idiomatic Rust definitions#6257

Merged
LukeMathWalker merged 8 commits intomasterfrom
buffer-writer-bytemuck
Jun 16, 2025
Merged

[MOD-10008] Reduce the amount of unsafe code by using more idiomatic Rust definitions#6257
LukeMathWalker merged 8 commits intomasterfrom
buffer-writer-bytemuck

Conversation

@LukeMathWalker
Copy link
Collaborator

Describe the changes in the pull request

Correctness is our paramount concern when manipulating C types in Rust.
When defining idiomatic Rust wrappers, we are caught between a rock and a hard place:

  • If we create thin new types around the generated bindings (in the ffi crate) we get layout correctness for free. If the C code changes, the bindings will change and any discrepancy will cause a compilation error in Rust. At the same time, we're working with raw pointers everywhere, thus forcing us to use a lot of unsafe code where mistakes can be made more easily.
  • If we use a "Rust-native" definition, we reduce the amount of unsafe code but we risk layout drift.

This PR tries to strike a balance for the buffer.* module.
We stick to wrapping the ffi type for Buffer, but we use more idiomatic Rust types for reader and writer. We add compile-time assertions to guard against layout drift.

Mark if applicable

  • This PR introduces API changes
  • This PR introduces serialization changes

@github-actions github-actions bot added the size:L label Jun 4, 2025
@LukeMathWalker LukeMathWalker force-pushed the buffer-writer-bytemuck branch 3 times, most recently from ebebf31 to 9f863c4 Compare June 4, 2025 15:06
@LukeMathWalker LukeMathWalker requested a review from zeenix June 4, 2025 15:06
@LukeMathWalker LukeMathWalker force-pushed the buffer-writer-bytemuck branch from 9f863c4 to 46cab95 Compare June 4, 2025 15:07
@LukeMathWalker LukeMathWalker requested a review from chesedo June 4, 2025 15:07
@LukeMathWalker LukeMathWalker force-pushed the buffer-writer-bytemuck branch from 46cab95 to e7033d4 Compare June 4, 2025 15:15
zeenix
zeenix previously approved these changes Jun 4, 2025
Copy link
Contributor

@zeenix zeenix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would have been better to do the splitting & merging of files in separate commit(s) so it's easier to see the main change for reviewers.

LGTM otherwise.

}

// Check, at compile-time, that `BufferReader` and `ffi::BufferReader` have the same representation.
const _: () = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not an ignored test instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't think I could have a const fn as a test, but today I learned it's possible.
Done in afe41ac

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not what I meant. :) I didn't know that either.

I was talking about:

#[test]
#[ignore]

which tests the code to compile but not actually run.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While that would be technically correct (i.e. the current test desugars to a no-op at runtime), I think it'd be confusing to see that #[ignore] for folks who are not too well-versed into the semantics of const evaluation.
Since the penalty is negligible, I prefer to leave it as is.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just wondering if declaring the test fn as const is doing what you are thinking. Did you find any docs on this. I couldn't. I'd be fine with just turning it back to const _: () = that you originally did.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to, but then it only checks it if tests are built. So yeah, probably better to revert back. Done in 5e61a40

Copy link
Collaborator

@chesedo chesedo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: small comment improvement

@codecov
Copy link

codecov bot commented Jun 4, 2025

Codecov Report

Attention: Patch coverage is 89.55224% with 7 lines in your changes missing coverage. Please review.

Project coverage is 88.79%. Comparing base (242a33d) to head (c9d1afa).
Report is 22 commits behind head on master.

Files with missing lines Patch % Lines
src/redisearch_rs/buffer/src/reader.rs 84.00% 3 Missing and 1 partial ⚠️
src/redisearch_rs/buffer/src/writer.rs 90.32% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6257      +/-   ##
==========================================
- Coverage   88.87%   88.79%   -0.08%     
==========================================
  Files         239      241       +2     
  Lines       40258    40623     +365     
  Branches     3165     3265     +100     
==========================================
+ Hits        35780    36073     +293     
- Misses       4443     4513      +70     
- Partials       35       37       +2     
Flag Coverage Δ
flow 82.16% <ø> (-0.71%) ⬇️
unit 46.12% <89.55%> (+0.32%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

chesedo
chesedo previously approved these changes Jun 5, 2025
@LukeMathWalker LukeMathWalker changed the title Reduce the amount of unsafe code by using more idiomatic Rust definitions [MOD-10008] Reduce the amount of unsafe code by using more idiomatic Rust definitions Jun 5, 2025
@LukeMathWalker LukeMathWalker requested a review from kei-nan June 5, 2025 11:45
const POSITION_OFFSET_MATCHES: bool =
offset_of!(BufferReader<'static>, position) == offset_of!(ffi::BufferReader, pos);

// Conditional compilation failure on mismatch
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should be done if the panic occurs?
What steps should we do?
Revert this PR?

Maybe add a comment with some directions.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 1eab83f

@LukeMathWalker LukeMathWalker requested a review from kei-nan June 16, 2025 08:05
@LukeMathWalker LukeMathWalker force-pushed the buffer-writer-bytemuck branch from 1eab83f to c9d1afa Compare June 16, 2025 10:32
@LukeMathWalker
Copy link
Collaborator Author

I've rebased on the latest master. Can you give it a final look @kei-nan?

@LukeMathWalker LukeMathWalker added this pull request to the merge queue Jun 16, 2025
Merged via the queue into master with commit 0cc4d7b Jun 16, 2025
15 checks passed
@LukeMathWalker LukeMathWalker deleted the buffer-writer-bytemuck branch June 16, 2025 14:57
@LukeMathWalker LukeMathWalker restored the buffer-writer-bytemuck branch February 6, 2026 09:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants