@@ -9,6 +9,7 @@ use crate::session::Session;
99use cryptoki_sys:: * ;
1010use std:: collections:: HashMap ;
1111use std:: convert:: TryInto ;
12+ use std:: num:: NonZeroUsize ;
1213
1314// Search 10 elements at a time
1415const MAX_OBJECT_COUNT : usize = 10 ;
@@ -86,12 +87,8 @@ impl<'a> ObjectHandleIterator<'a> {
8687 fn new (
8788 session : & ' a Session ,
8889 mut template : Vec < CK_ATTRIBUTE > ,
89- cache_size : usize ,
90+ cache_size : NonZeroUsize ,
9091 ) -> Result < Self > {
91- if cache_size == 0 {
92- return Err ( Error :: InvalidValue ) ;
93- }
94-
9592 unsafe {
9693 Rv :: from ( get_pkcs11 ! ( session. client( ) , C_FindObjectsInit ) (
9794 session. handle ( ) ,
@@ -101,11 +98,11 @@ impl<'a> ObjectHandleIterator<'a> {
10198 . into_result ( Function :: FindObjectsInit ) ?;
10299 }
103100
104- let cache: Vec < CK_OBJECT_HANDLE > = vec ! [ 0 ; cache_size] ;
101+ let cache: Vec < CK_OBJECT_HANDLE > = vec ! [ 0 ; cache_size. get ( ) ] ;
105102 Ok ( ObjectHandleIterator {
106103 session,
107- object_count : cache_size,
108- index : cache_size,
104+ object_count : cache_size. get ( ) ,
105+ index : cache_size. get ( ) ,
109106 cache,
110107 } )
111108 }
@@ -187,6 +184,7 @@ impl Session {
187184 /// Iterate over session objects matching a template.
188185 ///
189186 /// # Arguments
187+ ///
190188 /// * `template` - The template to match objects against
191189 ///
192190 /// # Returns
@@ -195,31 +193,34 @@ impl Session {
195193 /// matching the template. Note that the cache size is managed internally and set to a default value (10)
196194 ///
197195 /// # See also
196+ ///
198197 /// * [`ObjectHandleIterator`] for more information on how to use the iterator
199198 /// * [`Session::iter_objects_with_cache_size`] for a way to specify the cache size
200199 #[ inline( always) ]
201200 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 ( ) )
203202 }
204203
205204 /// Iterate over session objects matching a template, with cache size
206205 ///
207206 /// # Arguments
207+ ///
208208 /// * `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`])
210210 ///
211211 /// # Returns
212212 ///
213213 /// This function will return a [`Result<ObjectHandleIterator>`] that can be used to iterate over the objects
214214 /// matching the template. The cache size corresponds to the size of the array provided to `C_FindObjects()`.
215215 ///
216216 /// # See also
217+ ///
217218 /// * [`ObjectHandleIterator`] for more information on how to use the iterator
218219 /// * [`Session::iter_objects`] for a simpler way to iterate over objects
219220 pub fn iter_objects_with_cache_size (
220221 & self ,
221222 template : & [ Attribute ] ,
222- cache_size : usize ,
223+ cache_size : NonZeroUsize ,
223224 ) -> Result < ObjectHandleIterator > {
224225 let template: Vec < CK_ATTRIBUTE > = template. iter ( ) . map ( Into :: into) . collect ( ) ;
225226 ObjectHandleIterator :: new ( self , template, cache_size)
@@ -229,12 +230,12 @@ impl Session {
229230 ///
230231 /// # Arguments
231232 ///
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.
234235 ///
235236 /// # Returns
236237 ///
237- /// Upon success, a vector of [ObjectHandle] wrapped in a Result.
238+ /// Upon success, a vector of [` ObjectHandle` ] wrapped in a Result.
238239 /// Upon failure, the first error encountered.
239240 ///
240241 /// # Note
0 commit comments