@@ -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
4343impl 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}
0 commit comments