Description
Currently, the only way to search for objects is through Session::find_objects
, which returns a vector of all objects. If there may be a huge number of objects, it's desirable to be able to find only a bounded number of objects. This is possible with PKCS#11, but the functionality is not exposed by this crate.
Further, it may be desirable to iterate through objects incrementally, i.e. calling C_FindObjects
multiple times, with some processing in between each call. Also, the user may care whether the library makes several small calls to C_FindObjects
, or one large one, especially if the HSM is being accessed over a network.
I believe all of these needs may be met safely by the following API:
impl Session {
// ...
pub fn find_objects_init<'a>(&'a mut self, template: &[Attribute]) -> Result<FindObjects<'a>> {
// call `C_FindObjectsInit`
}
}
pub FindObjects<'a> {
session: '&mut Session // exclusive borrow to ensure at most one object search per session
}
impl<'a> FindObjects<'a> {
pub fn find_next(&self, object_count: usize) -> Result<Option<Vec<ObjectHandle>>> {
// call `C_FindObjects` once, with a buffer of size `object_count`.
// return Ok(Some(...)) if we found objects, or Ok(None) if the search is over
}
}
impl Drop for FindObjects { ... } // call `C_FindObjectsFinal` on drop
Although find_next
could return just Result<Vec<ObjectHandle>>
, I think returning an Option
is more ergonomic, since
- It's consistent with iterators and streams, which return
None
when finished - It reminds the user they need to check for termination
- It allows for looping over all objects with
while let
notation
I believe the current implementation of Session::find_objects
, as well as any other versions we might want to make (e.g. Session::find_objects_bounded(template, object_count)
), can be implemented in terms of this one.
I will make a PR with something like this soon, but first I would like to collect some feedback about the API.