Where possible, avoid unnecessary boxing.

This commit is contained in:
Neal H. Walfield 2022-02-18 17:43:29 +01:00
parent 0c86fcb84a
commit 64119c4f29
No known key found for this signature in database
GPG key ID: 6863C9AD5B4D22D3
3 changed files with 12 additions and 9 deletions

View file

@ -106,8 +106,10 @@ impl TestCard {
let card: Result<Box<dyn CardBackend>, Error> = loop { let card: Result<Box<dyn CardBackend>, Error> = loop {
let res = PcscBackend::open_by_ident(ident, SHARE_MODE); let res = PcscBackend::open_by_ident(ident, SHARE_MODE);
if i == 3 || res.is_ok() { if i == 3 {
break res.map(Into::into); if let Ok(res) = res {
break Ok(Box::new(res));
}
} }
// sleep for 100ms // sleep for 100ms

View file

@ -13,6 +13,7 @@ use sequoia_openpgp::Cert;
use openpgp_card::algorithm::AlgoSimple; use openpgp_card::algorithm::AlgoSimple;
use openpgp_card::{card_do::Sex, KeyType}; use openpgp_card::{card_do::Sex, KeyType};
use openpgp_card::CardBackend;
use openpgp_card_sequoia::card::{Admin, Open}; use openpgp_card_sequoia::card::{Admin, Open};
use openpgp_card_sequoia::util::{make_cert, public_key_material_to_key}; use openpgp_card_sequoia::util::{make_cert, public_key_material_to_key};
@ -162,12 +163,12 @@ fn set_identity(
} }
fn print_status(ident: Option<String>, verbose: bool) -> Result<()> { fn print_status(ident: Option<String>, verbose: bool) -> Result<()> {
let mut card = if let Some(ident) = ident { let mut card: Box<dyn CardBackend> = if let Some(ident) = ident {
util::open_card(&ident)? Box::new(util::open_card(&ident)?)
} else { } else {
let mut cards = util::cards()?; let mut cards = util::cards()?;
if cards.len() == 1 { if cards.len() == 1 {
cards.pop().unwrap() Box::new(cards.pop().unwrap())
} else { } else {
return Err(anyhow::anyhow!("Found {} cards", cards.len())); return Err(anyhow::anyhow!("Found {} cards", cards.len()));
} }

View file

@ -10,13 +10,13 @@ use openpgp_card::{CardBackend, Error};
use openpgp_card_pcsc::PcscBackend; use openpgp_card_pcsc::PcscBackend;
use openpgp_card_sequoia::card::{Admin, Open, Sign, User}; use openpgp_card_sequoia::card::{Admin, Open, Sign, User};
pub(crate) fn cards() -> Result<Vec<Box<dyn CardBackend>>, Error> { pub(crate) fn cards() -> Result<Vec<impl CardBackend>, Error> {
PcscBackend::cards(None) PcscBackend::cards(None)
.map(|cards| cards.into_iter().map(Into::into).collect()) .map(|cards| cards.into_iter().collect())
} }
pub(crate) fn open_card(ident: &str) -> Result<Box<dyn CardBackend>, Error> { pub(crate) fn open_card(ident: &str) -> Result<impl CardBackend, Error> {
PcscBackend::open_by_ident(ident, None).map(Into::into) PcscBackend::open_by_ident(ident, None)
} }
pub(crate) fn verify_to_user<'app, 'open>( pub(crate) fn verify_to_user<'app, 'open>(