@@ -9,6 +9,7 @@ use crate::session::Session;
9
9
use cryptoki_sys:: * ;
10
10
use std:: collections:: HashMap ;
11
11
use std:: convert:: TryInto ;
12
+ use std:: num:: NonZeroUsize ;
12
13
13
14
// Search 10 elements at a time
14
15
const MAX_OBJECT_COUNT : usize = 10 ;
@@ -86,12 +87,8 @@ impl<'a> ObjectHandleIterator<'a> {
86
87
fn new (
87
88
session : & ' a Session ,
88
89
mut template : Vec < CK_ATTRIBUTE > ,
89
- cache_size : usize ,
90
+ cache_size : NonZeroUsize ,
90
91
) -> Result < Self > {
91
- if cache_size == 0 {
92
- return Err ( Error :: InvalidValue ) ;
93
- }
94
-
95
92
unsafe {
96
93
Rv :: from ( get_pkcs11 ! ( session. client( ) , C_FindObjectsInit ) (
97
94
session. handle ( ) ,
@@ -101,11 +98,11 @@ impl<'a> ObjectHandleIterator<'a> {
101
98
. into_result ( Function :: FindObjectsInit ) ?;
102
99
}
103
100
104
- let cache: Vec < CK_OBJECT_HANDLE > = vec ! [ 0 ; cache_size] ;
101
+ let cache: Vec < CK_OBJECT_HANDLE > = vec ! [ 0 ; cache_size. get ( ) ] ;
105
102
Ok ( ObjectHandleIterator {
106
103
session,
107
- object_count : cache_size,
108
- index : cache_size,
104
+ object_count : cache_size. get ( ) ,
105
+ index : cache_size. get ( ) ,
109
106
cache,
110
107
} )
111
108
}
@@ -187,6 +184,7 @@ impl Session {
187
184
/// Iterate over session objects matching a template.
188
185
///
189
186
/// # Arguments
187
+ ///
190
188
/// * `template` - The template to match objects against
191
189
///
192
190
/// # Returns
@@ -195,31 +193,34 @@ impl Session {
195
193
/// matching the template. Note that the cache size is managed internally and set to a default value (10)
196
194
///
197
195
/// # See also
196
+ ///
198
197
/// * [`ObjectHandleIterator`] for more information on how to use the iterator
199
198
/// * [`Session::iter_objects_with_cache_size`] for a way to specify the cache size
200
199
#[ inline( always) ]
201
200
pub fn iter_objects ( & self , template : & [ Attribute ] ) -> Result < ObjectHandleIterator > {
202
- self . iter_objects_with_cache_size ( template, MAX_OBJECT_COUNT )
201
+ self . iter_objects_with_cache_size ( template, NonZeroUsize :: new ( MAX_OBJECT_COUNT ) . unwrap ( ) )
203
202
}
204
203
205
204
/// Iterate over session objects matching a template, with cache size
206
205
///
207
206
/// # Arguments
207
+ ///
208
208
/// * `template` - The template to match objects against
209
- /// * `cache_size` - The number of objects to cache. Note that 0 is an invalid value and will return an error.
209
+ /// * `cache_size` - The number of objects to cache (type is [`NonZeroUsize`])
210
210
///
211
211
/// # Returns
212
212
///
213
213
/// This function will return a [`Result<ObjectHandleIterator>`] that can be used to iterate over the objects
214
214
/// matching the template. The cache size corresponds to the size of the array provided to `C_FindObjects()`.
215
215
///
216
216
/// # See also
217
+ ///
217
218
/// * [`ObjectHandleIterator`] for more information on how to use the iterator
218
219
/// * [`Session::iter_objects`] for a simpler way to iterate over objects
219
220
pub fn iter_objects_with_cache_size (
220
221
& self ,
221
222
template : & [ Attribute ] ,
222
- cache_size : usize ,
223
+ cache_size : NonZeroUsize ,
223
224
) -> Result < ObjectHandleIterator > {
224
225
let template: Vec < CK_ATTRIBUTE > = template. iter ( ) . map ( Into :: into) . collect ( ) ;
225
226
ObjectHandleIterator :: new ( self , template, cache_size)
@@ -229,12 +230,12 @@ impl Session {
229
230
///
230
231
/// # Arguments
231
232
///
232
- /// * `template` - A [Attribute] of search parameters that will be used
233
- /// to find objects.
233
+ /// * `template` - A reference to [Attribute] of search parameters that will be used
234
+ /// to find objects.
234
235
///
235
236
/// # Returns
236
237
///
237
- /// Upon success, a vector of [ObjectHandle] wrapped in a Result.
238
+ /// Upon success, a vector of [` ObjectHandle` ] wrapped in a Result.
238
239
/// Upon failure, the first error encountered.
239
240
///
240
241
/// # Note
0 commit comments