Skip to content

Commit 5de6cfb

Browse files
nwalfieldbluetech
authored andcommitted
Provide a variant of Card::transaction, which returns self on error.
- Add Card::transaction2, which is like `Card::transaction`, but also returns the reference to `self` on error. When starting a transaction, it is necessary to deal with transient errors, like `Error::ResetCard`, by reconnecting to the card, and retrying the transaction. When this functionality is wrapped in a function , this doesn't work, because mutable references can't be reborrowed. This function returns the reference, which allows this construct. - Fixes #25.
1 parent 76c2b7f commit 5de6cfb

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

pcsc/src/lib.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,31 @@ impl Card {
11731173
}
11741174
}
11751175

1176+
/// Like `Card::transaction`, but returns the reference to `self` on error.
1177+
///
1178+
/// This function is like [`Card::transaction`], but also returns
1179+
/// the reference to `self` on error. When starting a
1180+
/// transaction, it is necessary to deal with transient errors,
1181+
/// like [`Error::ResetCard`] by reconnecting to the card, and
1182+
/// retrying the transaction. When this functionality is wrapped,
1183+
/// this doesn't work, because mutable references can't be
1184+
/// reborrowed. This function returns the reference, which allows
1185+
/// this construct.
1186+
pub fn transaction2(
1187+
&mut self,
1188+
) -> Result<Transaction, (&mut Self, Error)> {
1189+
// We can't use try_pcsc, because it returns an incompatible
1190+
// type. And we can't wrap it in a closure due to lifetimes.
1191+
// Hence we inline try_pcsc!.
1192+
1193+
match unsafe { ffi::SCardBeginTransaction(self.handle) } {
1194+
ffi::SCARD_S_SUCCESS => Ok(Transaction {
1195+
card: self,
1196+
}),
1197+
err => Err((self, Error::from_raw(err))),
1198+
}
1199+
}
1200+
11761201
/// Reconnect to the card.
11771202
///
11781203
/// This function wraps `SCardReconnect` ([pcsclite][1], [MSDN][2]).

0 commit comments

Comments
 (0)