Skip to main content

MockUsbTransfer

Struct MockUsbTransfer 

Source
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

Source

pub fn new() -> Self

Create an empty mock with no pre-loaded responses.

Source

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.

Source

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.

Source

pub fn enqueue_error(&self, endpoint: u8, error: U3vError)

Enqueue an error the next bulk_read on endpoint fails with.

Source

pub fn take_writes(&self, endpoint: u8) -> Vec<Vec<u8>>

Return all captured write payloads for the given endpoint.

Trait Implementations§

Source§

impl Default for MockUsbTransfer

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl UsbTransfer for MockUsbTransfer

Source§

fn bulk_write( &self, endpoint: u8, data: &[u8], _timeout: Duration, ) -> Result<usize, U3vError>

Write data to the bulk OUT endpoint. Returns bytes written.
Source§

fn bulk_read( &self, endpoint: u8, buf: &mut [u8], _timeout: Duration, ) -> Result<usize, U3vError>

Read up to buf.len() bytes from the bulk IN endpoint. Returns the number of bytes actually read.
Source§

fn is_present(&self) -> Option<bool>

Whether the operating system still lists the device, asked after a transfer failed in a way that does not say whether it is gone; 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,

Reads on the bulk IN endpoint that can be queued ahead of the device, as a stream needs them. Read more
Source§

fn max_packet_size(&self, _endpoint: u8) -> Option<usize>

The largest packet the endpoint carries, from its descriptor; None when unknown. Every IN transfer a stream asks for is a multiple of it, so that a packet never straddles two transfers.
The speed the host’s USB stack reports the device runs at; None when this transport cannot tell.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more