Skip to content

Commit 214efdf

Browse files
committed
Enforce nonzero connections in PoolBuilder
1 parent a987eae commit 214efdf

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

src/pool.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub struct PoolBuilder {
3737
flags: OpenFlags,
3838
journal_mode: Option<JournalMode>,
3939
vfs: Option<String>,
40-
num_conns: Option<usize>,
40+
num_conns: Option<NonZeroUsize>,
4141
}
4242

4343
impl PoolBuilder {
@@ -79,7 +79,20 @@ impl PoolBuilder {
7979
/// Specify the number of sqlite connections to open as part of the pool.
8080
///
8181
/// Defaults to the number of logical CPUs of the current system.
82-
pub fn num_conns(mut self, num_conns: usize) -> Self {
82+
///
83+
/// ```
84+
/// use async_sqlite::PoolBuilder;
85+
/// use std::num::NonZeroUsize;
86+
///
87+
/// let builder = PoolBuilder::new().num_conns(NonZeroUsize::new(2).unwrap());
88+
/// ```
89+
///
90+
/// ```compile_fail
91+
/// use async_sqlite::PoolBuilder;
92+
///
93+
/// PoolBuilder::new().num_conns(0);
94+
/// ```
95+
pub fn num_conns(mut self, num_conns: NonZeroUsize) -> Self {
8396
self.num_conns = Some(num_conns);
8497
self
8598
}
@@ -97,7 +110,7 @@ impl PoolBuilder {
97110
/// ```
98111
pub async fn open(self) -> Result<Pool, Error> {
99112
let num_conns = self.get_num_conns();
100-
let opens = (0..num_conns).map(|_| {
113+
let opens = (0..num_conns.get()).map(|_| {
101114
ClientBuilder {
102115
path: self.path.clone(),
103116
flags: self.flags,
@@ -132,7 +145,7 @@ impl PoolBuilder {
132145
/// ```
133146
pub fn open_blocking(self) -> Result<Pool, Error> {
134147
let num_conns = self.get_num_conns();
135-
let clients = (0..num_conns)
148+
let clients = (0..num_conns.get())
136149
.map(|_| {
137150
ClientBuilder {
138151
path: self.path.clone(),
@@ -151,11 +164,10 @@ impl PoolBuilder {
151164
})
152165
}
153166

154-
fn get_num_conns(&self) -> usize {
167+
fn get_num_conns(&self) -> NonZeroUsize {
155168
self.num_conns.unwrap_or_else(|| {
156169
available_parallelism()
157170
.unwrap_or_else(|_| NonZeroUsize::new(1).unwrap())
158-
.into()
159171
})
160172
}
161173
}

tests/tests.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use async_sqlite::{ClientBuilder, Error, JournalMode, PoolBuilder};
2+
use std::num::NonZeroUsize;
23

34
#[test]
45
fn test_blocking_client() {
@@ -138,7 +139,7 @@ async fn test_pool() {
138139
let tmp_dir = tempfile::tempdir().unwrap();
139140
let pool = PoolBuilder::new()
140141
.path(tmp_dir.path().join("sqlite.db"))
141-
.num_conns(2)
142+
.num_conns(NonZeroUsize::new(2).unwrap())
142143
.open()
143144
.await
144145
.expect("client unable to be opened");
@@ -191,7 +192,7 @@ async fn test_pool_conn_for_each() {
191192

192193
let pool = PoolBuilder::new()
193194
.path(tmp_dir.path().join("another-sqlite.db"))
194-
.num_conns(2)
195+
.num_conns(NonZeroUsize::new(2).unwrap())
195196
.open()
196197
.await
197198
.expect("pool unable to be opened");

0 commit comments

Comments
 (0)