Skip to content

Commit 18cf8c7

Browse files
authored
More immutable methods (#2141)
The SQLite connection and statement objects are thread-safe. Let's mark more Rust methods as immutable so that applications don't need to keep wrapping `Connection` and `Statement` with `Mutex`, for example, for mutability.
2 parents a9c66e0 + 0b4b3ca commit 18cf8c7

File tree

18 files changed

+80
-75
lines changed

18 files changed

+80
-75
lines changed

.github/workflows/rust.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ jobs:
2323
runs-on: ubuntu-latest
2424
name: Run Checks
2525
env:
26+
RUST_BACKTRACE: 1
2627
RUSTFLAGS: -D warnings --cfg tokio_unstable
2728
steps:
2829
- uses: hecrj/setup-rust-action@v2
@@ -179,6 +180,7 @@ jobs:
179180
runs-on: ubuntu-latest
180181
name: Run Tests
181182
env:
183+
RUST_BACKTRACE: 1
182184
RUSTFLAGS: -D warnings --cfg tokio_unstable
183185
steps:
184186
- uses: hecrj/setup-rust-action@v2

libsql-server/tests/standalone/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ async fn insert_rows(conn: &Connection, start: u32, count: u32) -> libsql::Resul
422422

423423
async fn insert_rows_with_args(conn: &Connection, start: u32, count: u32) -> libsql::Result<()> {
424424
for i in start..(start + count) {
425-
let mut stmt = conn.prepare("INSERT INTO test(a, b) VALUES(?,?)").await?;
425+
let stmt = conn.prepare("INSERT INTO test(a, b) VALUES(?,?)").await?;
426426
stmt.execute(params![i, i]).await?;
427427
}
428428
Ok(())

libsql/benches/benchmark.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ fn bench(c: &mut Criterion) {
9393
group.bench_function("in-memory-select-1-prepared", |b| {
9494
b.to_async(&rt).iter_batched(
9595
|| block_on(conn.prepare("SELECT 1")).unwrap(),
96-
|mut stmt| async move {
96+
|stmt| async move {
9797
let mut rows = stmt.query(()).await.unwrap();
9898
let row = rows.next().await.unwrap().unwrap();
9999
assert_eq!(row.get::<i32>(0).unwrap(), 1);
@@ -113,7 +113,7 @@ fn bench(c: &mut Criterion) {
113113
group.bench_function("in-memory-select-star-from-users-limit-1-unprepared", |b| {
114114
b.to_async(&rt).iter_batched(
115115
|| block_on(conn.prepare("SELECT * FROM users LIMIT 1")).unwrap(),
116-
|mut stmt| async move {
116+
|stmt| async move {
117117
let mut rows = stmt.query(()).await.unwrap();
118118
let row = rows.next().await.unwrap().unwrap();
119119
assert_eq!(row.get::<i32>(0).unwrap(), 1);
@@ -128,7 +128,7 @@ fn bench(c: &mut Criterion) {
128128
|b| {
129129
b.to_async(&rt).iter_batched(
130130
|| block_on(conn.prepare("SELECT * FROM users LIMIT 100")).unwrap(),
131-
|mut stmt| async move {
131+
|stmt| async move {
132132
let mut rows = stmt.query(()).await.unwrap();
133133
let row = rows.next().await.unwrap().unwrap();
134134
assert_eq!(row.get::<i32>(0).unwrap(), 1);
@@ -156,7 +156,7 @@ fn bench(c: &mut Criterion) {
156156
group.bench_function("local-replica-select-1-prepared", |b| {
157157
b.to_async(&rt).iter_batched(
158158
|| block_on(conn.prepare("SELECT 1")).unwrap(),
159-
|mut stmt| async move {
159+
|stmt| async move {
160160
let mut rows = stmt.query(()).await.unwrap();
161161
let row = rows.next().await.unwrap().unwrap();
162162
assert_eq!(row.get::<i32>(0).unwrap(), 1);
@@ -188,7 +188,7 @@ fn bench(c: &mut Criterion) {
188188
|b| {
189189
b.to_async(&rt).iter_batched(
190190
|| block_on(conn.prepare("SELECT * FROM users LIMIT 1")).unwrap(),
191-
|mut stmt| async move {
191+
|stmt| async move {
192192
let mut rows = stmt.query(()).await.unwrap();
193193
let row = rows.next().await.unwrap().unwrap();
194194
assert_eq!(row.get::<i32>(0).unwrap(), 1);
@@ -204,7 +204,7 @@ fn bench(c: &mut Criterion) {
204204
|b| {
205205
b.to_async(&rt).iter_batched(
206206
|| block_on(conn.prepare("SELECT * FROM users LIMIT 100")).unwrap(),
207-
|mut stmt| async move {
207+
|stmt| async move {
208208
let mut rows = stmt.query(()).await.unwrap();
209209
let row = rows.next().await.unwrap().unwrap();
210210
assert_eq!(row.get::<i32>(0).unwrap(), 1);

libsql/examples/deserialization.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ async fn main() {
2222
.await
2323
.unwrap();
2424

25-
let mut stmt = conn
25+
let stmt = conn
2626
.prepare("INSERT INTO users (name, age, vision, avatar) VALUES (?1, ?2, ?3, ?4)")
2727
.await
2828
.unwrap();
2929
stmt.execute(("Ferris the Crab", 8, -6.5, vec![1, 2, 3]))
3030
.await
3131
.unwrap();
3232

33-
let mut stmt = conn
33+
let stmt = conn
3434
.prepare("SELECT * FROM users WHERE name = ?1")
3535
.await
3636
.unwrap();

libsql/examples/example.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ async fn main() {
2121
.await
2222
.unwrap();
2323

24-
let mut stmt = conn
24+
let stmt = conn
2525
.prepare("INSERT INTO users (email) VALUES (?1)")
2626
.await
2727
.unwrap();
2828

2929
stmt.execute(["foo@example.com"]).await.unwrap();
3030

31-
let mut stmt = conn
31+
let stmt = conn
3232
.prepare("SELECT * FROM users WHERE email = ?1")
3333
.await
3434
.unwrap();

libsql/examples/example_v2.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ async fn main() {
2121
.await
2222
.unwrap();
2323

24-
let mut stmt = conn
24+
let stmt = conn
2525
.prepare("INSERT INTO users (email) VALUES (?1)")
2626
.await
2727
.unwrap();
2828

2929
stmt.execute(["foo@example.com"]).await.unwrap();
3030

31-
let mut stmt = conn
31+
let stmt = conn
3232
.prepare("SELECT * FROM users WHERE email = ?1")
3333
.await
3434
.unwrap();

libsql/examples/flutter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ async fn main() {
3030
.await
3131
.unwrap();
3232

33-
let mut stmt = conn
33+
let stmt = conn
3434
.prepare("INSERT INTO users (email) VALUES (?1)")
3535
.await
3636
.unwrap();
3737

3838
stmt.execute(["foo@example.com"]).await.unwrap();
3939

40-
let mut stmt = conn
40+
let stmt = conn
4141
.prepare("SELECT * FROM users WHERE email = ?1")
4242
.await
4343
.unwrap();

libsql/src/connection.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ impl Connection {
170170
/// For more info on how to pass params check [`IntoParams`]'s docs and on how to
171171
/// extract values out of the rows check the [`Rows`] docs.
172172
pub async fn query(&self, sql: &str, params: impl IntoParams) -> Result<Rows> {
173-
let mut stmt = self.prepare(sql).await?;
173+
let stmt = self.prepare(sql).await?;
174174

175175
stmt.query(params).await
176176
}

libsql/src/hrana/hyper.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,25 +237,25 @@ impl Conn for HttpConnection<HttpSender> {
237237
impl crate::statement::Stmt for crate::hrana::Statement<HttpSender> {
238238
fn finalize(&mut self) {}
239239

240-
async fn execute(&mut self, params: &Params) -> crate::Result<usize> {
240+
async fn execute(&self, params: &Params) -> crate::Result<usize> {
241241
self.execute(params).await
242242
}
243243

244-
async fn query(&mut self, params: &Params) -> crate::Result<Rows> {
244+
async fn query(&self, params: &Params) -> crate::Result<Rows> {
245245
self.query(params).await
246246
}
247247

248-
async fn run(&mut self, params: &Params) -> crate::Result<()> {
248+
async fn run(&self, params: &Params) -> crate::Result<()> {
249249
self.run(params).await
250250
}
251251

252-
fn interrupt(&mut self) -> crate::Result<()> {
252+
fn interrupt(&self) -> crate::Result<()> {
253253
Err(crate::Error::Misuse(
254254
"interrupt is not supported for remote connections".to_string(),
255255
))
256256
}
257257

258-
fn reset(&mut self) {}
258+
fn reset(&self) {}
259259

260260
fn parameter_count(&self) -> usize {
261261
let stmt = &self.inner;

libsql/src/hrana/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,15 @@ where
162162
}
163163
}
164164

165-
pub async fn execute(&mut self, params: &Params) -> crate::Result<usize> {
165+
pub async fn execute(&self, params: &Params) -> crate::Result<usize> {
166166
let mut stmt = self.inner.clone();
167167
bind_params(params.clone(), &mut stmt);
168168

169169
let result = self.stream.execute_inner(stmt, self.close_stream).await?;
170170
Ok(result.affected_row_count as usize)
171171
}
172172

173-
pub async fn run(&mut self, params: &Params) -> crate::Result<()> {
173+
pub async fn run(&self, params: &Params) -> crate::Result<()> {
174174
let mut stmt = self.inner.clone();
175175
bind_params(params.clone(), &mut stmt);
176176

@@ -179,7 +179,7 @@ where
179179
}
180180

181181
pub(crate) async fn query_raw(
182-
&mut self,
182+
&self,
183183
params: &Params,
184184
) -> crate::Result<HranaRows<T::Stream, T>> {
185185
let mut stmt = self.inner.clone();
@@ -197,7 +197,7 @@ where
197197
T: HttpSend + Send + Sync + 'static,
198198
<T as HttpSend>::Stream: Send + Sync + 'static,
199199
{
200-
pub async fn query(&mut self, params: &Params) -> crate::Result<super::Rows> {
200+
pub async fn query(&self, params: &Params) -> crate::Result<super::Rows> {
201201
let rows = self.query_raw(params).await?;
202202
Ok(super::Rows::new(rows))
203203
}

0 commit comments

Comments
 (0)