Skip to content

Commit

Permalink
refactor trezor ConnectableDeviceWrapper (use borrow instead of consu…
Browse files Browse the repository at this point in the history
…me, better naming and constraints, thanks to shamardy)
  • Loading branch information
dimxy committed Oct 18, 2023
1 parent 0f45e36 commit 5d194c8
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 19 deletions.
5 changes: 2 additions & 3 deletions mm2src/crypto/src/hw_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,11 @@ impl HwClient {
) -> MmResult<TrezorClient, HwProcessingError<Processor::Error>> {
use common::custom_futures::timeout::TimeoutError;
use common::executor::Timer;
use trezor::transport::{ConnectableDeviceWrapper, Transport};
use trezor::transport::ConnectableDeviceWrapper;

async fn try_to_connect<C>() -> HwResult<Option<TrezorClient>>
where
C: ConnectableDeviceWrapper,
<C as ConnectableDeviceWrapper>::T: Transport + Sync + Send + 'static,
C: ConnectableDeviceWrapper + 'static,
{
let mut devices = C::find_devices().await?;
if devices.is_empty() {
Expand Down
6 changes: 3 additions & 3 deletions mm2src/hw_common/src/transport/libusb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ pub struct UsbAvailableDevice {
}

impl UsbAvailableDevice {
pub fn connect(self) -> UsbResult<UsbDevice> {
pub fn connect(&self) -> UsbResult<UsbDevice> {
// This is a non-blocking function; no requests are sent over the bus.
let mut device_handle = self.device.open().map_to_mm(UsbError::ErrorOpeningDevice)?;
// Claiming of interfaces is a purely logical operation.
Expand Down Expand Up @@ -277,7 +277,7 @@ impl UsbDevice {
fn endpoint_number(&self) -> u8 { self.device_info.interface_info.endpoint_number }
}

#[derive(Debug)]
#[derive(Clone, Copy, Debug)]
pub struct UsbDeviceInfo {
pub vendor_id: u16,
pub product_id: u16,
Expand All @@ -286,7 +286,7 @@ pub struct UsbDeviceInfo {
pub interface_info: UsbDeviceInterfaceInfo,
}

#[derive(Debug)]
#[derive(Clone, Copy, Debug)]
pub struct UsbDeviceInterfaceInfo {
pub interface_number: u8,
pub endpoint_number: u8,
Expand Down
6 changes: 2 additions & 4 deletions mm2src/trezor/src/transport/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,11 @@ impl AsRef<[u8]> for SessionId {
/// Wrapper to abstract connectivity to usb and emulator devices
#[async_trait]
pub trait ConnectableDeviceWrapper {
type T;
type TransportType: Transport + Sync + Send;

async fn find_devices() -> TrezorResult<Vec<Self>>
where
Self: Sized;

async fn connect(self) -> TrezorResult<Self::T>
where
Self::T: Transport;
async fn connect(&self) -> TrezorResult<Self::TransportType>;
}
10 changes: 5 additions & 5 deletions mm2src/trezor/src/transport/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ pub struct UdpAvailableDevice {

impl UdpAvailableDevice {
/// Connect to the device.
async fn connect(self) -> TrezorResult<UdpTransport> {
let transport = UdpTransport::connect(&self).await?;
async fn connect(&self) -> TrezorResult<UdpTransport> {
let transport = UdpTransport::connect(self).await?;
Ok(transport)
}
}
Expand Down Expand Up @@ -160,14 +160,14 @@ impl Transport for UdpTransport {

#[async_trait]
impl ConnectableDeviceWrapper for UdpAvailableDevice {
type T = UdpTransport;
type TransportType = UdpTransport;

async fn find_devices() -> TrezorResult<Vec<Self>>
where
Self: Sized,
{
crate::transport::udp::find_devices().await
find_devices().await
}

async fn connect(self) -> TrezorResult<UdpTransport> { UdpAvailableDevice::connect(self).await }
async fn connect(&self) -> TrezorResult<Self::TransportType> { UdpAvailableDevice::connect(self).await }
}
8 changes: 4 additions & 4 deletions mm2src/trezor/src/transport/usb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub struct UsbAvailableDevice(UsbAvailableDeviceImpl);

impl UsbAvailableDevice {
/// Please note [`hw_common::transport::libusb::UsbAvailableDevice::connect`] spawns a thread.
async fn connect(self) -> TrezorResult<UsbTransport> {
async fn connect(&self) -> TrezorResult<UsbTransport> {
let link = UsbLink {
device: self.0.connect()?,
};
Expand All @@ -95,14 +95,14 @@ fn is_trezor(device: &UsbAvailableDeviceImpl) -> bool {

#[async_trait]
impl ConnectableDeviceWrapper for UsbAvailableDevice {
type T = UsbTransport;
type TransportType = UsbTransport;

async fn find_devices() -> TrezorResult<Vec<Self>>
where
Self: Sized,
{
crate::transport::usb::find_devices().await
find_devices().await
}

async fn connect(self) -> TrezorResult<UsbTransport> { UsbAvailableDevice::connect(self).await }
async fn connect(&self) -> TrezorResult<Self::TransportType> { UsbAvailableDevice::connect(self).await }
}

0 comments on commit 5d194c8

Please sign in to comment.