diff options
-rw-r--r-- | hid/Cargo.toml | 2 | ||||
-rw-r--r-- | hid/src/handle.rs | 43 | ||||
-rw-r--r-- | nitrocli/Cargo.lock | 4 | ||||
-rw-r--r-- | nitrocli/Cargo.toml | 2 |
4 files changed, 24 insertions, 27 deletions
diff --git a/hid/Cargo.toml b/hid/Cargo.toml index 13a7d49..91111a4 100644 --- a/hid/Cargo.toml +++ b/hid/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "hid" -version = "0.3.0" +version = "0.4.0" authors = ["meh. <meh@schizofreni.co>"] license = "WTFPL" 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<T: AsRef<[u8]>>(&mut self, id: u8, data: T) -> error::Result<usize> { 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<T: AsMut<[u8]>>(&mut self, mut data: T) -> error::Result<Option<usize>> { + pub fn get<T: AsMut<[u8]>>(&mut self, mut data: T) -> error::Result<usize> { 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<T: AsMut<[u8]>>(&mut self, id: u8, mut data: T) -> error::Result<Option<usize>> { + pub fn get_from<T: AsMut<[u8]>>(&mut self, id: u8, mut data: T) -> error::Result<usize> { 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) } } diff --git a/nitrocli/Cargo.lock b/nitrocli/Cargo.lock index 6a6d315..ec390a5 100644 --- a/nitrocli/Cargo.lock +++ b/nitrocli/Cargo.lock @@ -3,7 +3,7 @@ name = "nitrocli" version = "0.1.0" dependencies = [ "gcc 0.3.45", - "hid 0.3.0", + "hid 0.4.0", "hidapi-sys 0.1.2", "libc 0.2.21", "pkg-config 0.3.9", @@ -21,7 +21,7 @@ replace = "gcc 0.3.45" [[package]] name = "hid" -version = "0.3.0" +version = "0.4.0" dependencies = [ "hidapi-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/nitrocli/Cargo.toml b/nitrocli/Cargo.toml index 1ee63fb..af0a09e 100644 --- a/nitrocli/Cargo.toml +++ b/nitrocli/Cargo.toml @@ -47,7 +47,7 @@ path = "../pkg-config" [replace] "gcc:0.3.45" = { path = "../gcc" } -"hid:0.3.0" = { path = "../hid" } +"hid:0.4.0" = { path = "../hid" } "hidapi-sys:0.1.2" = { path = "../hidapi-sys" } "libc:0.2.21" = { path = "../libc" } "pkg-config:0.3.9" = { path = "../pkg-config" } |