Skip to content

Make possible to pass your own pointers#1626

Merged
gwenn merged 10 commits into
rusqlite:masterfrom
gwenn:ptr
Mar 14, 2026
Merged

Make possible to pass your own pointers#1626
gwenn merged 10 commits into
rusqlite:masterfrom
gwenn:ptr

Conversation

@gwenn

@gwenn gwenn commented Jan 25, 2025

Copy link
Copy Markdown
Collaborator

See #1602, #1475, #1497

  • minimal working test / example
  • try to make the API less unsafe for Box / Rc / Arc

@codecov

codecov Bot commented Jan 25, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.05825% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.02%. Comparing base (8784e5f) to head (643d581).
⚠️ Report is 19 commits behind head on master.

Files with missing lines Patch % Lines
src/row.rs 90.90% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1626      +/-   ##
==========================================
+ Coverage   87.96%   88.02%   +0.06%     
==========================================
  Files          58       58              
  Lines       10648    10733      +85     
==========================================
+ Hits         9366     9448      +82     
- Misses       1282     1285       +3     

☔ 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.

@gwenn

gwenn commented Feb 2, 2025

Copy link
Copy Markdown
Collaborator Author

SQLite destructor provides only the raw pointer but:

  • to deallocate a Vec<T>, you also need its length and capacity
  • to deallocate a Box<[T]>, you also need its length

Only Rc<[T]> can be deallocated with only the raw pointer.

@gwenn

gwenn commented Mar 9, 2025

Copy link
Copy Markdown
Collaborator Author

@mqudsi

mqudsi commented Oct 1, 2025

Copy link
Copy Markdown
Contributor

Does SQLite need to take ownership of the pointer (i.e. can this be made to use a slice ref instead of an owning vec/box)? That would remove the need to deallocate altogether.

Alternatively, a vector of tuples of (ptr, len, capacity) can be stored within the connection itself and binary searched for a match in the free stage; it would be zero-alloc for an empty vec and the 24-byte field itself would only be included in each Connection object if you are using the array feature?

@gwenn

gwenn commented Oct 2, 2025

Copy link
Copy Markdown
Collaborator Author

Currently, you cannot retrieve the Connection from a VTabConnection / *mut sqlite3 so you will not be able to destroy the owned pointer.
There is also this API:
https://sqlite.org/c3ref/get_clientdata.html
But still, only a void* pointer, not (ptr, len, capacity).

Comment thread src/vtab/array.rs
Comment thread src/vtab/mod.rs Outdated
&self,
idx: usize,
ptr_type: &'static CStr,
) -> Option<&T> {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@gwenn : I think here you need to return Option<Rc<T>> not Option<&T> (compare to https://github.com/rusqlite/rusqlite/pull/1799/changes#diff-dff5ca49e95ff5abfe972cd64b09632f0d9c9116db60599f2f9f0af44985e1a0R948).

Overall I think my implementation has trended closer towards yours (e.g. my ValuePointer type is equivalent to your anonymous tuple here.

One thing I ended up with in my branch was a nicer type that custom modules should use, PointerType. Ultimately this just converts to the lower-level representation. But the high-level type is used as part of the API for get_pointer see here.

You may also be interested in the example custom module I implemented here.

I think we can probably consolidate the work in the two branches, or / consolidate into your PR.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I think here you need to return Option<Rc<T>> not Option<&T>

We don't: see c9b7637#diff-dff5ca49e95ff5abfe972cd64b09632f0d9c9116db60599f2f9f0af44985e1a0R930

@gwenn gwenn Feb 14, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

One thing I ended up with in my branch was a nicer type that custom modules should use, PointerType.

See TypeId

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is neat! I was unaware of sqlite_ext. Given that exists, why not use that directly or directly borrow that API?

@gwenn gwenn Feb 14, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

There seems to be no safe way to serialize a TypeId as a CStr (ptr_type).

@gwenn gwenn marked this pull request as ready for review March 8, 2026 11:41
@gwenn gwenn merged commit 487af3c into rusqlite:master Mar 14, 2026
17 checks passed
@gwenn gwenn deleted the ptr branch March 14, 2026 17:16
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.

3 participants