aboutsummaryrefslogtreecommitdiff
path: root/embassy-usb/src/descriptor.rs
diff options
context:
space:
mode:
authorRafael Bachmann <[email protected]>2023-10-15 23:45:44 +0200
committerRafael Bachmann <[email protected]>2023-10-15 23:52:44 +0200
commit31d4516516940720101300a40d0d6d2bb8d1728e (patch)
treece44bfebf56fea7726bccae7d2617efd360af319 /embassy-usb/src/descriptor.rs
parent66e62e999409fd6967ab959a061f7eae660102d0 (diff)
Apply Pedantic Clippy Lints
Diffstat (limited to 'embassy-usb/src/descriptor.rs')
-rw-r--r--embassy-usb/src/descriptor.rs32
1 files changed, 15 insertions, 17 deletions
diff --git a/embassy-usb/src/descriptor.rs b/embassy-usb/src/descriptor.rs
index 16c5f9d9b..fa83ef583 100644
--- a/embassy-usb/src/descriptor.rs
+++ b/embassy-usb/src/descriptor.rs
@@ -2,7 +2,7 @@
2 2
3use crate::builder::Config; 3use crate::builder::Config;
4use crate::driver::EndpointInfo; 4use crate::driver::EndpointInfo;
5use crate::types::*; 5use crate::types::{InterfaceNumber, StringIndex};
6use crate::CONFIGURATION_VALUE; 6use crate::CONFIGURATION_VALUE;
7 7
8/// Standard descriptor types 8/// Standard descriptor types
@@ -59,7 +59,7 @@ impl<'a> DescriptorWriter<'a> {
59 } 59 }
60 60
61 /// Gets the current position in the buffer, i.e. the number of bytes written so far. 61 /// Gets the current position in the buffer, i.e. the number of bytes written so far.
62 pub fn position(&self) -> usize { 62 pub const fn position(&self) -> usize {
63 self.position 63 self.position
64 } 64 }
65 65
@@ -67,9 +67,10 @@ impl<'a> DescriptorWriter<'a> {
67 pub fn write(&mut self, descriptor_type: u8, descriptor: &[u8]) { 67 pub fn write(&mut self, descriptor_type: u8, descriptor: &[u8]) {
68 let length = descriptor.len(); 68 let length = descriptor.len();
69 69
70 if (self.position + 2 + length) > self.buf.len() || (length + 2) > 255 { 70 assert!(
71 panic!("Descriptor buffer full"); 71 (self.position + 2 + length) <= self.buf.len() && (length + 2) <= 255,
72 } 72 "Descriptor buffer full"
73 );
73 74
74 self.buf[self.position] = (length + 2) as u8; 75 self.buf[self.position] = (length + 2) as u8;
75 self.buf[self.position + 1] = descriptor_type; 76 self.buf[self.position + 1] = descriptor_type;
@@ -102,7 +103,7 @@ impl<'a> DescriptorWriter<'a> {
102 config.serial_number.map_or(0, |_| 3), // iSerialNumber 103 config.serial_number.map_or(0, |_| 3), // iSerialNumber
103 1, // bNumConfigurations 104 1, // bNumConfigurations
104 ], 105 ],
105 ) 106 );
106 } 107 }
107 108
108 pub(crate) fn configuration(&mut self, config: &Config) { 109 pub(crate) fn configuration(&mut self, config: &Config) {
@@ -120,7 +121,7 @@ impl<'a> DescriptorWriter<'a> {
120 | if config.supports_remote_wakeup { 0x20 } else { 0x00 }, // bmAttributes 121 | if config.supports_remote_wakeup { 0x20 } else { 0x00 }, // bmAttributes
121 (config.max_power / 2) as u8, // bMaxPower 122 (config.max_power / 2) as u8, // bMaxPower
122 ], 123 ],
123 ) 124 );
124 } 125 }
125 126
126 #[allow(unused)] 127 #[allow(unused)]
@@ -248,9 +249,7 @@ impl<'a> DescriptorWriter<'a> {
248 pub(crate) fn string(&mut self, string: &str) { 249 pub(crate) fn string(&mut self, string: &str) {
249 let mut pos = self.position; 250 let mut pos = self.position;
250 251
251 if pos + 2 > self.buf.len() { 252 assert!(pos + 2 <= self.buf.len(), "Descriptor buffer full");
252 panic!("Descriptor buffer full");
253 }
254 253
255 self.buf[pos] = 0; // length placeholder 254 self.buf[pos] = 0; // length placeholder
256 self.buf[pos + 1] = descriptor_type::STRING; 255 self.buf[pos + 1] = descriptor_type::STRING;
@@ -258,9 +257,7 @@ impl<'a> DescriptorWriter<'a> {
258 pos += 2; 257 pos += 2;
259 258
260 for c in string.encode_utf16() { 259 for c in string.encode_utf16() {
261 if pos >= self.buf.len() { 260 assert!(pos < self.buf.len(), "Descriptor buffer full");
262 panic!("Descriptor buffer full");
263 }
264 261
265 self.buf[pos..pos + 2].copy_from_slice(&c.to_le_bytes()); 262 self.buf[pos..pos + 2].copy_from_slice(&c.to_le_bytes());
266 pos += 2; 263 pos += 2;
@@ -279,7 +276,7 @@ pub struct BosWriter<'a> {
279} 276}
280 277
281impl<'a> BosWriter<'a> { 278impl<'a> BosWriter<'a> {
282 pub(crate) fn new(writer: DescriptorWriter<'a>) -> Self { 279 pub(crate) const fn new(writer: DescriptorWriter<'a>) -> Self {
283 Self { 280 Self {
284 writer, 281 writer,
285 num_caps_mark: None, 282 num_caps_mark: None,
@@ -314,9 +311,10 @@ impl<'a> BosWriter<'a> {
314 let mut start = self.writer.position; 311 let mut start = self.writer.position;
315 let blen = data.len(); 312 let blen = data.len();
316 313
317 if (start + blen + 3) > self.writer.buf.len() || (blen + 3) > 255 { 314 assert!(
318 panic!("Descriptor buffer full"); 315 (start + blen + 3) <= self.writer.buf.len() && (blen + 3) <= 255,
319 } 316 "Descriptor buffer full"
317 );
320 318
321 self.writer.buf[start] = (blen + 3) as u8; 319 self.writer.buf[start] = (blen + 3) as u8;
322 self.writer.buf[start + 1] = descriptor_type::CAPABILITY; 320 self.writer.buf[start + 1] = descriptor_type::CAPABILITY;