pub struct MockUsbTransfer { /* private fields */ }Expand description
In-memory mock of UsbTransfer for unit tests.
Pre-load expected read responses per endpoint, then execute protocol logic. After the test, inspect captured writes to verify correctness.
§Thread safety
State lives behind a Mutex, so the Send + Sync that UsbTransfer
requires is derived rather than asserted. The mock used to hold RefCell
and claim the bounds with unsafe impl, which was unsound: RefCell’s
borrow flag is not atomic, so two threads sharing one mock could hold
overlapping borrow_mut()s and alias the same &mut.
Implementations§
Source§impl MockUsbTransfer
impl MockUsbTransfer
Sourcepub fn set_present(&self, present: Option<bool>)
pub fn set_present(&self, present: Option<bool>)
What UsbTransfer::is_present answers from now on: Some(false)
for a device the operating system no longer lists. None, cannot
tell, until set.
Sourcepub fn enqueue_read(&self, endpoint: u8, data: Vec<u8>)
pub fn enqueue_read(&self, endpoint: u8, data: Vec<u8>)
Enqueue a response that will be returned by the next bulk_read
on the given endpoint.
Sourcepub fn enqueue_error(&self, endpoint: u8, error: U3vError)
pub fn enqueue_error(&self, endpoint: u8, error: U3vError)
Enqueue an error the next bulk_read on endpoint fails with.
Trait Implementations§
Source§impl Default for MockUsbTransfer
impl Default for MockUsbTransfer
Source§impl UsbTransfer for MockUsbTransfer
impl UsbTransfer for MockUsbTransfer
Source§fn bulk_write(
&self,
endpoint: u8,
data: &[u8],
_timeout: Duration,
) -> Result<usize, U3vError>
fn bulk_write( &self, endpoint: u8, data: &[u8], _timeout: Duration, ) -> Result<usize, U3vError>
data to the bulk OUT endpoint. Returns bytes written.Source§fn bulk_read(
&self,
endpoint: u8,
buf: &mut [u8],
_timeout: Duration,
) -> Result<usize, U3vError>
fn bulk_read( &self, endpoint: u8, buf: &mut [u8], _timeout: Duration, ) -> Result<usize, U3vError>
buf.len() bytes from the bulk IN endpoint.
Returns the number of bytes actually read.Source§fn is_present(&self) -> Option<bool>
fn is_present(&self) -> Option<bool>
None when this transport cannot tell. One look, no waiting.Source§fn bulk_in(self: &Arc<Self>, endpoint: u8) -> Result<Box<dyn BulkIn>, U3vError>where
Self: Sized + 'static,
fn bulk_in(self: &Arc<Self>, endpoint: u8) -> Result<Box<dyn BulkIn>, U3vError>where
Self: Sized + 'static,
endpoint that can be queued ahead of the
device, as a stream needs them. Read moreSource§fn max_packet_size(&self, _endpoint: u8) -> Option<usize>
fn max_packet_size(&self, _endpoint: u8) -> Option<usize>
None
when unknown. Every IN transfer a stream asks for is a multiple of
it, so that a packet never straddles two transfers.Source§fn link_speed(&self) -> Option<UsbSpeed>
fn link_speed(&self) -> Option<UsbSpeed>
None
when this transport cannot tell.