Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[db] Take FnMut in transactions #701 #702

Merged
merged 3 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions agdb/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,10 @@ impl Db {
/// Read transactions cannot be committed or rolled back but their main function is to ensure
/// that the database data does not change during their duration. Through its generic
/// parameters it also allows transforming the query results into a type `T`.
pub fn transaction<T, E>(&self, f: impl Fn(&Transaction) -> Result<T, E>) -> Result<T, E> {
pub fn transaction<T, E>(
&self,
mut f: impl FnMut(&Transaction) -> Result<T, E>,
) -> Result<T, E> {
let transaction = Transaction::new(self);

f(&transaction)
Expand All @@ -274,7 +277,7 @@ impl Db {
/// results into a type `T`.
pub fn transaction_mut<T, E: From<QueryError>>(
&mut self,
f: impl Fn(&mut TransactionMut) -> Result<T, E>,
mut f: impl FnMut(&mut TransactionMut) -> Result<T, E>,
) -> Result<T, E> {
let mut transaction = TransactionMut::new(&mut *self);
let result = f(&mut transaction);
Expand Down
4 changes: 2 additions & 2 deletions agdb/tests/test_db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl TestDb {
#[track_caller]
pub fn transaction_mut<T, E: From<QueryError> + std::fmt::Debug>(
&mut self,
f: impl Fn(&mut TransactionMut) -> Result<T, E>,
f: impl FnMut(&mut TransactionMut) -> Result<T, E>,
) {
self.db.transaction_mut(f).unwrap();
}
Expand All @@ -107,7 +107,7 @@ impl TestDb {
E: From<QueryError> + std::fmt::Debug + PartialEq,
>(
&mut self,
f: impl Fn(&mut TransactionMut) -> Result<T, E>,
f: impl FnMut(&mut TransactionMut) -> Result<T, E>,
error: E,
) {
assert_eq!(self.db.transaction_mut(f).unwrap_err(), error);
Expand Down
4 changes: 2 additions & 2 deletions docs/queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ You can run a series of queries as a transaction invoking corresponding methods
```Rust
impl Db {
// immutable transactions
pub fn transaction<T, E>(&self, f: impl Fn(&Transaction) -> Result<T, E>) -> Result<T, E>
pub fn transaction<T, E>(&self, mut f: impl FnMut(&Transaction) -> Result<T, E>) -> Result<T, E>

// mutable transactions
pub fn transaction_mut<T, E: From<QueryError>>(&mut self, f: impl Fn(&mut TransactionMut) -> Result<T, E>) -> Result<T, E>
pub fn transaction_mut<T, E: From<QueryError>>(&mut self, mut f: impl FnMut(&mut TransactionMut) -> Result<T, E>) -> Result<T, E>
}
```

Expand Down