CeresDB / sqlness

An ergonomic, opinionated framework for SQL integration test.
https://crates.io/crates/sqlness
Apache License 2.0
22 stars 7 forks source link

feat: mutable borrow `Database`'s implementor #30

Closed MichaelScofield closed 1 year ago

MichaelScofield commented 1 year ago

... so that we can change its internal state.

Which issue does this PR close?

Closes #

Rationale for this change

We need to impl Database like this:

struct Foo {
  s: String,
}

impl Database for Foo {
  async fn query(&self, query: String) -> Box<dyn Display> {
    if query == "blah" {
      self.s = "x"; // has to mutable borrow `self` to make this happened
    }
    ...
  }
}

What changes are included in this PR?

Are there any user-facing changes?

yes, implementors need to change their methods names

How does this change test

CLAassistant commented 1 year ago

CLA assistant check
All committers have signed the CLA.

jiacai2050 commented 1 year ago

Have you thought about other solutions like interior mutability(Cell/Mutex)?

If we pass mutable reference, then we can't execute query concurrently for one database, although not implemented yet, but may will in future.

waynexia commented 1 year ago

I agree with @jiacai2050. Passing mutable reference here also prevents us from implementing "interceptors" that may hold the database's reference (though it's not implemented too 😩). Maybe Mutex is a better choice for this case.