Skip to content

Commit ac1feee

Browse files
committed
pcsc: some edits to transaction2
1 parent 5de6cfb commit ac1feee

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

pcsc/src/lib.rs

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,28 +1173,38 @@ 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.
1176+
/// Start a new exclusive transaction with the card.
1177+
///
1178+
/// Operations on the card for the duration of the transaction
1179+
/// can only be performed through the returned `Transaction`.
1180+
///
1181+
/// This function is like [`Card::transaction`], but also returns the
1182+
/// reference to `self` on error. When starting a transaction, you might
1183+
/// want to deal with transient errors, like [`Error::ResetCard`], by
1184+
/// reconnecting to the card, and retrying the transaction. When this
1185+
/// functionality is wrapped, this doesn't work, because mutable references
1186+
/// can't be reborrowed (at least in current Rust). This function returns
1187+
/// the reference, which allows this construct.
1188+
///
1189+
/// This function wraps `SCardBeginTransaction` ([pcsclite][1],
1190+
/// [MSDN][2]).
1191+
///
1192+
/// [1]: https://pcsclite.apdu.fr/api/group__API.html#gaddb835dce01a0da1d6ca02d33ee7d861
1193+
/// [2]: https://msdn.microsoft.com/en-us/library/aa379469.aspx
11861194
pub fn transaction2(
11871195
&mut self,
11881196
) -> 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!.
1197+
unsafe {
1198+
let err = ffi::SCardBeginTransaction(
1199+
self.handle,
1200+
);
1201+
if err != ffi::SCARD_S_SUCCESS {
1202+
return Err((self, Error::from_raw(err)));
1203+
}
11921204

1193-
match unsafe { ffi::SCardBeginTransaction(self.handle) } {
1194-
ffi::SCARD_S_SUCCESS => Ok(Transaction {
1205+
return Ok(Transaction {
11951206
card: self,
1196-
}),
1197-
err => Err((self, Error::from_raw(err))),
1207+
})
11981208
}
11991209
}
12001210

0 commit comments

Comments
 (0)