@@ -12,7 +12,8 @@ use std::convert::TryInto;
1212use std:: num:: NonZeroUsize ;
1313
1414// Search 10 elements at a time
15- const MAX_OBJECT_COUNT : usize = 10 ;
15+ // Safety: the value provided (10) must be non-zero
16+ const MAX_OBJECT_COUNT : NonZeroUsize = unsafe { NonZeroUsize :: new_unchecked ( 10 ) } ;
1617
1718/// Iterator over object handles, in an active session.
1819///
@@ -37,16 +38,16 @@ const MAX_OBJECT_COUNT: usize = 10;
3738/// use std::env;
3839///
3940/// # fn main() -> testresult::TestResult {
40- /// let pkcs11 = Pkcs11::new(
41- /// env::var("PKCS11_SOFTHSM2_MODULE")
42- /// .unwrap_or_else(|_| "/usr/local/lib/libsofthsm2.so".to_string()),
43- /// )?;
44- ///
45- /// pkcs11.initialize(CInitializeArgs::OsThreads)?;
46- /// let slot = pkcs11.get_slots_with_token()?.remove(0);
47- ///
48- /// let session = pkcs11.open_ro_session(slot).unwrap();
49- /// session.login(UserType::User, Some(&AuthPin::new("fedcba".into())))?;
41+ /// # let pkcs11 = Pkcs11::new(
42+ /// # env::var("PKCS11_SOFTHSM2_MODULE")
43+ /// # .unwrap_or_else(|_| "/usr/local/lib/libsofthsm2.so".to_string()),
44+ /// # )?;
45+ /// #
46+ /// # pkcs11.initialize(CInitializeArgs::OsThreads)?;
47+ /// # let slot = pkcs11.get_slots_with_token()?.remove(0);
48+ /// #
49+ /// # let session = pkcs11.open_ro_session(slot).unwrap();
50+ /// # session.login(UserType::User, Some(&AuthPin::new("fedcba".into())))?;
5051///
5152/// let token_object = vec![Attribute::Token(true)];
5253/// let wanted_attr = vec![AttributeType::Label];
@@ -84,6 +85,28 @@ pub struct ObjectHandleIterator<'a> {
8485}
8586
8687impl < ' a > ObjectHandleIterator < ' a > {
88+ /// Create a new iterator over object handles.
89+ ///
90+ /// # Arguments
91+ ///
92+ /// * `session` - The session to iterate over
93+ /// * `template` - The template to match objects against
94+ /// * `cache_size` - The number of objects to cache (type is [`NonZeroUsize`])
95+ ///
96+ /// # Returns
97+ ///
98+ /// This function will return a [`Result<ObjectHandleIterator>`] that can be used to iterate over the objects
99+ /// matching the template. The cache size corresponds to the size of the array provided to `C_FindObjects()`.
100+ ///
101+ /// # Errors
102+ ///
103+ /// This function will return an error if the call to `C_FindObjectsInit` fails.
104+ ///
105+ /// # Note
106+ ///
107+ /// The iterator `new()` method will call `C_FindObjectsInit`. It means that until the iterator is dropped,
108+ /// creating another iterator will result in an error (typically `RvError::OperationActive` ).
109+ ///
87110 fn new (
88111 session : & ' a Session ,
89112 mut template : Vec < CK_ATTRIBUTE > ,
@@ -171,11 +194,15 @@ impl<'a> Iterator for ObjectHandleIterator<'a> {
171194
172195impl Drop for ObjectHandleIterator < ' _ > {
173196 fn drop ( & mut self ) {
174- // bark but pass if C_FindObjectsFinal() is not implemented
175197 if let Some ( f) = get_pkcs11_func ! ( self . session. client( ) , C_FindObjectsFinal ) {
198+ // swallow the return value, as we can't do anything about it,
199+ // but log the error
200+ if let Rv :: Error ( error) = Rv :: from ( unsafe { f ( self . session . handle ( ) ) } ) {
201+ log:: error!( "C_FindObjectsFinal() failed with error: {:?}" , error) ;
202+ }
203+ } else {
204+ // bark but pass if C_FindObjectsFinal() is not implemented
176205 log:: error!( "C_FindObjectsFinal() is not implemented on this library" ) ;
177- // swallow the return value, as we can't do anything about it
178- let _ = unsafe { f ( self . session . handle ( ) ) } ;
179206 }
180207 }
181208}
@@ -198,7 +225,7 @@ impl Session {
198225 /// * [`Session::iter_objects_with_cache_size`] for a way to specify the cache size
199226 #[ inline( always) ]
200227 pub fn iter_objects ( & self , template : & [ Attribute ] ) -> Result < ObjectHandleIterator > {
201- self . iter_objects_with_cache_size ( template, NonZeroUsize :: new ( MAX_OBJECT_COUNT ) . unwrap ( ) )
228+ self . iter_objects_with_cache_size ( template, MAX_OBJECT_COUNT )
202229 }
203230
204231 /// Iterate over session objects matching a template, with cache size
@@ -279,8 +306,7 @@ impl Session {
279306 ///
280307 #[ inline( always) ]
281308 pub fn find_objects ( & self , template : & [ Attribute ] ) -> Result < Vec < ObjectHandle > > {
282- self . iter_objects ( template) ?
283- . collect :: < Result < Vec < ObjectHandle > > > ( )
309+ self . iter_objects ( template) ?. collect ( )
284310 }
285311
286312 /// Create a new object
0 commit comments