Skip to content

Add FromSqlError::other convenience conversion#1703

Merged
gwenn merged 1 commit intorusqlite:masterfrom
Kixunil:fromsql-other
May 29, 2025
Merged

Add FromSqlError::other convenience conversion#1703
gwenn merged 1 commit intorusqlite:masterfrom
Kixunil:fromsql-other

Conversation

@Kixunil
Copy link
Contributor

@Kixunil Kixunil commented May 29, 2025

When implementing FromSql it is common (as demonstrated by this commit diff) to call into a lower-level parsing function (such as str::from_utf8) and then convert the returned error to FromSqlError::Other. However this required manual boxing and unsizing which made the code a bit annoying. Thus this commit introduces an other generic method intentionally named after the Other variant. This method performs the boxing and unsizing so that callers can use it instead of writing a closure with Box::new (if map_err is used).

This commit also replaces all such occurrences in the rusqlite's code which not only simplifies the code but also demonstrates the usefulness of the method. Incidentally, the change also simplifies manual matches to use map_err instead as well as replaces FromStr with the more idiomatic parse method in the those places.

When implementing `FromSql` it is common (as demonstrated by this commit
diff) to call into a lower-level parsing function (such as
`str::from_utf8`) and then convert the returned error to
`FromSqlError::Other`. However this required manual boxing and unsizing
which made the code a bit annoying. Thus this commit introduces an
`other` generic method intentionally named after the `Other` variant.
This method performs the boxing and unsizing so that callers can use it
instead of writing a closure with `Box::new` (if `map_err` is used).

This commit also replaces all such occurrences in the `rusqlite`'s code
which not only simplifies the code but also demonstrates the usefulness
of the method. Incidentally, the change also simplifies manual `match`es
to use `map_err` instead as well as replaces `FromStr` with the more
idiomatic `parse` method in the those places.
@codecov
Copy link

codecov bot commented May 29, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.84%. Comparing base (8ae9407) to head (c305911).
Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1703      +/-   ##
==========================================
+ Coverage   87.83%   87.84%   +0.01%     
==========================================
  Files          58       58              
  Lines       11389    11379      -10     
==========================================
- Hits        10003     9996       -7     
+ Misses       1386     1383       -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 merged commit 2d66c46 into rusqlite:master May 29, 2025
16 checks passed
@gwenn
Copy link
Collaborator

gwenn commented May 29, 2025

Thanks.

@Kixunil Kixunil deleted the fromsql-other branch May 29, 2025 16:01
@gwenn
Copy link
Collaborator

gwenn commented Jul 6, 2025

rusqlite version 0.37.0 released

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