Struct TransferPool

Source
pub struct TransferPool<C: UsbContext> { /* private fields */ }
Expand description

Represents a pool of asynchronous transfers, that can be polled to completion

Implementations§

Source§

impl<C: UsbContext> TransferPool<C>

Source

pub fn new(device: Arc<DeviceHandle<C>>) -> Result<Self, Error>

Examples found in repository?
examples/read_async.rs (line 38)
17fn main() {
18    let args: Vec<String> = std::env::args().collect();
19
20    if args.len() < 4 {
21        eprintln!("Usage: read_async <base-10/0xbase-16> <base-10/0xbase-16> <endpoint>");
22        return;
23    }
24
25    let vid = convert_argument(args[1].as_ref());
26    let pid = convert_argument(args[2].as_ref());
27    let endpoint: u8 = FromStr::from_str(args[3].as_ref()).unwrap();
28
29    let ctx = Context::new().expect("Could not initialize libusb");
30    let device = Arc::new(
31        ctx.open_device_with_vid_pid(vid, pid)
32            .expect("Could not find device"),
33    );
34
35    const NUM_TRANSFERS: usize = 32;
36    const BUF_SIZE: usize = 64;
37
38    let mut async_pool = TransferPool::new(device).expect("Failed to create async pool!");
39
40    while async_pool.pending() < NUM_TRANSFERS {
41        async_pool
42            .submit_bulk(endpoint, Vec::with_capacity(BUF_SIZE))
43            .expect("Failed to submit transfer");
44    }
45
46    let timeout = Duration::from_secs(10);
47    loop {
48        let data = async_pool.poll(timeout).expect("Transfer failed");
49        println!("Got data: {} {:?}", data.len(), data);
50        async_pool
51            .submit_bulk(endpoint, data)
52            .expect("Failed to resubmit transfer");
53    }
54}
More examples
Hide additional examples
examples/read_write_async.rs (line 30)
8fn main() {
9    let args: Vec<String> = std::env::args().collect();
10
11    if args.len() < 5 {
12        eprintln!("Usage: read_write_async <vendor-id> <product-id> <out-endpoint> <in-endpoint> (all numbers hex)");
13        return;
14    }
15
16    let vid = u16::from_str_radix(args[1].as_ref(), 16).unwrap();
17    let pid = u16::from_str_radix(args[2].as_ref(), 16).unwrap();
18    let out_endpoint = u8::from_str_radix(args[3].as_ref(), 16).unwrap();
19    let in_endpoint = u8::from_str_radix(args[4].as_ref(), 16).unwrap();
20
21    let ctx = Context::new().expect("Could not initialize libusb");
22    let device = Arc::new(
23        ctx.open_device_with_vid_pid(vid, pid)
24            .expect("Could not find device"),
25    );
26
27    thread::spawn({
28        let device = device.clone();
29        move || {
30            let mut write_pool = TransferPool::new(device).expect("Failed to create async pool!");
31
32            let mut i = 0u8;
33
34            loop {
35                let mut buf = if write_pool.pending() < 8 {
36                    Vec::with_capacity(64)
37                } else {
38                    write_pool
39                        .poll(Duration::from_secs(5))
40                        .expect("Failed to poll OUT transfer")
41                };
42
43                buf.clear();
44                buf.push(i);
45                buf.resize(64, 0x2);
46
47                write_pool
48                    .submit_bulk(out_endpoint, buf)
49                    .expect("Failed to submit OUT transfer");
50                println!("Wrote {}", i);
51                i = i.wrapping_add(1);
52            }
53        }
54    });
55
56    let mut read_pool = TransferPool::new(device).expect("Failed to create async pool!");
57
58    while read_pool.pending() < 8 {
59        read_pool
60            .submit_bulk(in_endpoint, Vec::with_capacity(1024))
61            .expect("Failed to submit IN transfer");
62    }
63
64    loop {
65        let data = read_pool
66            .poll(Duration::from_secs(10))
67            .expect("Failed to poll IN transfer");
68        println!("Got data: {} {:?}", data.len(), data[0]);
69        read_pool
70            .submit_bulk(in_endpoint, data)
71            .expect("Failed to resubmit IN transfer");
72    }
73}
Source

pub fn submit_bulk(&mut self, endpoint: u8, buf: Vec<u8>) -> Result<(), Error>

Examples found in repository?
examples/read_async.rs (line 42)
17fn main() {
18    let args: Vec<String> = std::env::args().collect();
19
20    if args.len() < 4 {
21        eprintln!("Usage: read_async <base-10/0xbase-16> <base-10/0xbase-16> <endpoint>");
22        return;
23    }
24
25    let vid = convert_argument(args[1].as_ref());
26    let pid = convert_argument(args[2].as_ref());
27    let endpoint: u8 = FromStr::from_str(args[3].as_ref()).unwrap();
28
29    let ctx = Context::new().expect("Could not initialize libusb");
30    let device = Arc::new(
31        ctx.open_device_with_vid_pid(vid, pid)
32            .expect("Could not find device"),
33    );
34
35    const NUM_TRANSFERS: usize = 32;
36    const BUF_SIZE: usize = 64;
37
38    let mut async_pool = TransferPool::new(device).expect("Failed to create async pool!");
39
40    while async_pool.pending() < NUM_TRANSFERS {
41        async_pool
42            .submit_bulk(endpoint, Vec::with_capacity(BUF_SIZE))
43            .expect("Failed to submit transfer");
44    }
45
46    let timeout = Duration::from_secs(10);
47    loop {
48        let data = async_pool.poll(timeout).expect("Transfer failed");
49        println!("Got data: {} {:?}", data.len(), data);
50        async_pool
51            .submit_bulk(endpoint, data)
52            .expect("Failed to resubmit transfer");
53    }
54}
More examples
Hide additional examples
examples/read_write_async.rs (line 48)
8fn main() {
9    let args: Vec<String> = std::env::args().collect();
10
11    if args.len() < 5 {
12        eprintln!("Usage: read_write_async <vendor-id> <product-id> <out-endpoint> <in-endpoint> (all numbers hex)");
13        return;
14    }
15
16    let vid = u16::from_str_radix(args[1].as_ref(), 16).unwrap();
17    let pid = u16::from_str_radix(args[2].as_ref(), 16).unwrap();
18    let out_endpoint = u8::from_str_radix(args[3].as_ref(), 16).unwrap();
19    let in_endpoint = u8::from_str_radix(args[4].as_ref(), 16).unwrap();
20
21    let ctx = Context::new().expect("Could not initialize libusb");
22    let device = Arc::new(
23        ctx.open_device_with_vid_pid(vid, pid)
24            .expect("Could not find device"),
25    );
26
27    thread::spawn({
28        let device = device.clone();
29        move || {
30            let mut write_pool = TransferPool::new(device).expect("Failed to create async pool!");
31
32            let mut i = 0u8;
33
34            loop {
35                let mut buf = if write_pool.pending() < 8 {
36                    Vec::with_capacity(64)
37                } else {
38                    write_pool
39                        .poll(Duration::from_secs(5))
40                        .expect("Failed to poll OUT transfer")
41                };
42
43                buf.clear();
44                buf.push(i);
45                buf.resize(64, 0x2);
46
47                write_pool
48                    .submit_bulk(out_endpoint, buf)
49                    .expect("Failed to submit OUT transfer");
50                println!("Wrote {}", i);
51                i = i.wrapping_add(1);
52            }
53        }
54    });
55
56    let mut read_pool = TransferPool::new(device).expect("Failed to create async pool!");
57
58    while read_pool.pending() < 8 {
59        read_pool
60            .submit_bulk(in_endpoint, Vec::with_capacity(1024))
61            .expect("Failed to submit IN transfer");
62    }
63
64    loop {
65        let data = read_pool
66            .poll(Duration::from_secs(10))
67            .expect("Failed to poll IN transfer");
68        println!("Got data: {} {:?}", data.len(), data[0]);
69        read_pool
70            .submit_bulk(in_endpoint, data)
71            .expect("Failed to resubmit IN transfer");
72    }
73}
Source

pub fn submit_control( &mut self, request_type: u8, request: u8, value: u16, index: u16, data: &[u8], ) -> Result<(), Error>

Source

pub unsafe fn submit_control_raw( &mut self, buffer: Vec<u8>, ) -> Result<(), Error>

Source

pub fn submit_interrupt( &mut self, endpoint: u8, buf: Vec<u8>, ) -> Result<(), Error>

Source

pub fn submit_iso( &mut self, endpoint: u8, buf: Vec<u8>, iso_packets: i32, ) -> Result<(), Error>

Source

pub fn poll(&mut self, timeout: Duration) -> Result<Vec<u8>, Error>

Examples found in repository?
examples/read_async.rs (line 48)
17fn main() {
18    let args: Vec<String> = std::env::args().collect();
19
20    if args.len() < 4 {
21        eprintln!("Usage: read_async <base-10/0xbase-16> <base-10/0xbase-16> <endpoint>");
22        return;
23    }
24
25    let vid = convert_argument(args[1].as_ref());
26    let pid = convert_argument(args[2].as_ref());
27    let endpoint: u8 = FromStr::from_str(args[3].as_ref()).unwrap();
28
29    let ctx = Context::new().expect("Could not initialize libusb");
30    let device = Arc::new(
31        ctx.open_device_with_vid_pid(vid, pid)
32            .expect("Could not find device"),
33    );
34
35    const NUM_TRANSFERS: usize = 32;
36    const BUF_SIZE: usize = 64;
37
38    let mut async_pool = TransferPool::new(device).expect("Failed to create async pool!");
39
40    while async_pool.pending() < NUM_TRANSFERS {
41        async_pool
42            .submit_bulk(endpoint, Vec::with_capacity(BUF_SIZE))
43            .expect("Failed to submit transfer");
44    }
45
46    let timeout = Duration::from_secs(10);
47    loop {
48        let data = async_pool.poll(timeout).expect("Transfer failed");
49        println!("Got data: {} {:?}", data.len(), data);
50        async_pool
51            .submit_bulk(endpoint, data)
52            .expect("Failed to resubmit transfer");
53    }
54}
More examples
Hide additional examples
examples/read_write_async.rs (line 39)
8fn main() {
9    let args: Vec<String> = std::env::args().collect();
10
11    if args.len() < 5 {
12        eprintln!("Usage: read_write_async <vendor-id> <product-id> <out-endpoint> <in-endpoint> (all numbers hex)");
13        return;
14    }
15
16    let vid = u16::from_str_radix(args[1].as_ref(), 16).unwrap();
17    let pid = u16::from_str_radix(args[2].as_ref(), 16).unwrap();
18    let out_endpoint = u8::from_str_radix(args[3].as_ref(), 16).unwrap();
19    let in_endpoint = u8::from_str_radix(args[4].as_ref(), 16).unwrap();
20
21    let ctx = Context::new().expect("Could not initialize libusb");
22    let device = Arc::new(
23        ctx.open_device_with_vid_pid(vid, pid)
24            .expect("Could not find device"),
25    );
26
27    thread::spawn({
28        let device = device.clone();
29        move || {
30            let mut write_pool = TransferPool::new(device).expect("Failed to create async pool!");
31
32            let mut i = 0u8;
33
34            loop {
35                let mut buf = if write_pool.pending() < 8 {
36                    Vec::with_capacity(64)
37                } else {
38                    write_pool
39                        .poll(Duration::from_secs(5))
40                        .expect("Failed to poll OUT transfer")
41                };
42
43                buf.clear();
44                buf.push(i);
45                buf.resize(64, 0x2);
46
47                write_pool
48                    .submit_bulk(out_endpoint, buf)
49                    .expect("Failed to submit OUT transfer");
50                println!("Wrote {}", i);
51                i = i.wrapping_add(1);
52            }
53        }
54    });
55
56    let mut read_pool = TransferPool::new(device).expect("Failed to create async pool!");
57
58    while read_pool.pending() < 8 {
59        read_pool
60            .submit_bulk(in_endpoint, Vec::with_capacity(1024))
61            .expect("Failed to submit IN transfer");
62    }
63
64    loop {
65        let data = read_pool
66            .poll(Duration::from_secs(10))
67            .expect("Failed to poll IN transfer");
68        println!("Got data: {} {:?}", data.len(), data[0]);
69        read_pool
70            .submit_bulk(in_endpoint, data)
71            .expect("Failed to resubmit IN transfer");
72    }
73}
Source

pub fn cancel_all(&mut self)

Source

pub fn pending(&self) -> usize

Returns the number of async transfers pending

Examples found in repository?
examples/read_async.rs (line 40)
17fn main() {
18    let args: Vec<String> = std::env::args().collect();
19
20    if args.len() < 4 {
21        eprintln!("Usage: read_async <base-10/0xbase-16> <base-10/0xbase-16> <endpoint>");
22        return;
23    }
24
25    let vid = convert_argument(args[1].as_ref());
26    let pid = convert_argument(args[2].as_ref());
27    let endpoint: u8 = FromStr::from_str(args[3].as_ref()).unwrap();
28
29    let ctx = Context::new().expect("Could not initialize libusb");
30    let device = Arc::new(
31        ctx.open_device_with_vid_pid(vid, pid)
32            .expect("Could not find device"),
33    );
34
35    const NUM_TRANSFERS: usize = 32;
36    const BUF_SIZE: usize = 64;
37
38    let mut async_pool = TransferPool::new(device).expect("Failed to create async pool!");
39
40    while async_pool.pending() < NUM_TRANSFERS {
41        async_pool
42            .submit_bulk(endpoint, Vec::with_capacity(BUF_SIZE))
43            .expect("Failed to submit transfer");
44    }
45
46    let timeout = Duration::from_secs(10);
47    loop {
48        let data = async_pool.poll(timeout).expect("Transfer failed");
49        println!("Got data: {} {:?}", data.len(), data);
50        async_pool
51            .submit_bulk(endpoint, data)
52            .expect("Failed to resubmit transfer");
53    }
54}
More examples
Hide additional examples
examples/read_write_async.rs (line 35)
8fn main() {
9    let args: Vec<String> = std::env::args().collect();
10
11    if args.len() < 5 {
12        eprintln!("Usage: read_write_async <vendor-id> <product-id> <out-endpoint> <in-endpoint> (all numbers hex)");
13        return;
14    }
15
16    let vid = u16::from_str_radix(args[1].as_ref(), 16).unwrap();
17    let pid = u16::from_str_radix(args[2].as_ref(), 16).unwrap();
18    let out_endpoint = u8::from_str_radix(args[3].as_ref(), 16).unwrap();
19    let in_endpoint = u8::from_str_radix(args[4].as_ref(), 16).unwrap();
20
21    let ctx = Context::new().expect("Could not initialize libusb");
22    let device = Arc::new(
23        ctx.open_device_with_vid_pid(vid, pid)
24            .expect("Could not find device"),
25    );
26
27    thread::spawn({
28        let device = device.clone();
29        move || {
30            let mut write_pool = TransferPool::new(device).expect("Failed to create async pool!");
31
32            let mut i = 0u8;
33
34            loop {
35                let mut buf = if write_pool.pending() < 8 {
36                    Vec::with_capacity(64)
37                } else {
38                    write_pool
39                        .poll(Duration::from_secs(5))
40                        .expect("Failed to poll OUT transfer")
41                };
42
43                buf.clear();
44                buf.push(i);
45                buf.resize(64, 0x2);
46
47                write_pool
48                    .submit_bulk(out_endpoint, buf)
49                    .expect("Failed to submit OUT transfer");
50                println!("Wrote {}", i);
51                i = i.wrapping_add(1);
52            }
53        }
54    });
55
56    let mut read_pool = TransferPool::new(device).expect("Failed to create async pool!");
57
58    while read_pool.pending() < 8 {
59        read_pool
60            .submit_bulk(in_endpoint, Vec::with_capacity(1024))
61            .expect("Failed to submit IN transfer");
62    }
63
64    loop {
65        let data = read_pool
66            .poll(Duration::from_secs(10))
67            .expect("Failed to poll IN transfer");
68        println!("Got data: {} {:?}", data.len(), data[0]);
69        read_pool
70            .submit_bulk(in_endpoint, data)
71            .expect("Failed to resubmit IN transfer");
72    }
73}

Trait Implementations§

Source§

impl<C: UsbContext> Drop for TransferPool<C>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<C: UsbContext> Send for TransferPool<C>

Source§

impl<C: UsbContext> Sync for TransferPool<C>

Auto Trait Implementations§

§

impl<C> Freeze for TransferPool<C>

§

impl<C> RefUnwindSafe for TransferPool<C>
where C: RefUnwindSafe,

§

impl<C> Unpin for TransferPool<C>

§

impl<C> UnwindSafe for TransferPool<C>
where C: RefUnwindSafe,

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.

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.