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>
impl<C: UsbContext> TransferPool<C>
Sourcepub fn new(device: Arc<DeviceHandle<C>>) -> Result<Self, Error>
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
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}
Sourcepub fn submit_bulk(&mut self, endpoint: u8, buf: Vec<u8>) -> Result<(), Error>
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
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}
pub fn submit_control( &mut self, request_type: u8, request: u8, value: u16, index: u16, data: &[u8], ) -> Result<(), Error>
pub unsafe fn submit_control_raw( &mut self, buffer: Vec<u8>, ) -> Result<(), Error>
pub fn submit_interrupt( &mut self, endpoint: u8, buf: Vec<u8>, ) -> Result<(), Error>
pub fn submit_iso( &mut self, endpoint: u8, buf: Vec<u8>, iso_packets: i32, ) -> Result<(), Error>
Sourcepub fn poll(&mut self, timeout: Duration) -> Result<Vec<u8>, Error>
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
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}
pub fn cancel_all(&mut self)
Sourcepub fn pending(&self) -> usize
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
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>
impl<C: UsbContext> Drop for TransferPool<C>
impl<C: UsbContext> Send for TransferPool<C>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more