From fdd34d1ac10e37b3e0683a3c716665f50684596b Mon Sep 17 00:00:00 2001 From: Daniel Mueller Date: Mon, 27 Mar 2017 23:32:24 -0700 Subject: Update hid crate to version 0.4.0 The 'hid' crate got a couple of bug fixes, at least one of which we require in order to retrieve HID feature reports correctly. This patch imports the new state and bumps up the library version used. Import subrepo hid/:hid at 52b47d78c17b876194e4b4a1c0c8ae8adfb3d39c --- hid/src/handle.rs | 43 ++++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 23 deletions(-) (limited to 'hid/src') diff --git a/hid/src/handle.rs b/hid/src/handle.rs index 280bdcf..759c87e 100644 --- a/hid/src/handle.rs +++ b/hid/src/handle.rs @@ -29,11 +29,11 @@ impl Handle { impl Handle { /// Set the handle in blocking or non-blocking mode. - pub fn blocking(&mut self, value: bool) -> error::Result<()> { + pub fn blocking(&mut self, value: bool) -> error::Result<&mut Self> { unsafe { match hid_set_nonblocking(self.as_mut_ptr(), if value { 1 } else { 0 }) { 0 => - Ok(()), + Ok(self), _ => Err(Error::General) @@ -87,10 +87,10 @@ impl<'a> Data<'a> { /// Write data to the device with the given report ID. pub fn write_to>(&mut self, id: u8, data: T) -> error::Result { let data = data.as_ref(); - let mut buffer = Vec::with_capacity(data.len() + 1); + let mut buffer = vec![0u8; data.len() + 1]; - buffer.push(id); - buffer.extend(data); + buffer[0] = id; + (&mut buffer[1 ..]).copy_from_slice(data); self.write(&buffer) } @@ -134,16 +134,13 @@ impl<'a> Data<'a> { let mut data = data.as_mut(); let mut buffer = Vec::with_capacity(data.len() + 1); - match try!(self.read(&mut buffer, timeout)) { - None => { - Ok(None) - } - - Some(length) => { - data.clone_from_slice(&buffer[1..length]); + if let Some(length) = self.read(&mut buffer, timeout)? { + data[0..length - 1].copy_from_slice(&buffer[1..length]); - Ok(Some((data[0], length - 1))) - } + Ok(Some((buffer[0], length - 1))) + } + else { + Ok(None) } } } @@ -190,7 +187,7 @@ impl<'a> Feature<'a> { /// Get a feature request. /// /// The first byte must be the report ID. - pub fn get>(&mut self, mut data: T) -> error::Result> { + pub fn get>(&mut self, mut data: T) -> error::Result { let data = data.as_mut(); unsafe { @@ -198,22 +195,22 @@ impl<'a> Feature<'a> { -1 => Err(Error::Read), - 0 => - Ok(None), - - v => - Ok(Some(v as usize)) + length => + Ok(length as usize) } } } /// Get a feature request from the given report ID. - pub fn get_from>(&mut self, id: u8, mut data: T) -> error::Result> { + pub fn get_from>(&mut self, id: u8, mut data: T) -> error::Result { let data = data.as_mut(); let mut buffer = vec![0u8; data.len() + 1]; - buffer[0] = id; - self.get(&mut buffer) + + let length = self.get(&mut buffer)?; + data[0..length - 1].copy_from_slice(&buffer[1..length]); + + Ok(length - 1) } } -- cgit v1.2.3