Skip to content

Commit 73bb08b

Browse files
authored
Apply some clippy fixes (#128)
This fixes some warnings that have been creeping into the build.
1 parent 7a562f0 commit 73bb08b

File tree

13 files changed

+89
-73
lines changed

13 files changed

+89
-73
lines changed
File renamed without changes.

crates/openvino/src/core.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,13 @@ impl Core {
4545
}
4646

4747
/// Gets device plugins version information.
48-
/// Device name can be complex and identify multiple devices at once like `HETERO:CPU,GPU`;
49-
/// in this case, the returned map contains multiple entries, each per device.
48+
///
49+
/// A device name can be complex and identify multiple devices at once, like `HETERO:CPU,GPU`.
50+
/// In this case, the returned map contains multiple entries, each per device.
51+
///
52+
/// # Panics
53+
///
54+
/// This function panics if OpenVINO returns a device name these bindings do not yet recognize.
5055
pub fn versions(&self, device_name: &str) -> Result<Vec<(DeviceType, Version)>> {
5156
let device_name = cstr!(device_name);
5257
let mut ov_version_list = openvino_sys::ov_core_version_list_t {
@@ -75,6 +80,10 @@ impl Core {
7580
}
7681

7782
/// Gets devices available for inference.
83+
///
84+
/// # Panics
85+
///
86+
/// This function panics if OpenVINO returns a device name these bindings do not yet recognize.
7887
pub fn available_devices(&self) -> Result<Vec<DeviceType>> {
7988
let mut ov_available_devices = openvino_sys::ov_available_devices_t {
8089
devices: std::ptr::null_mut(),
@@ -101,8 +110,13 @@ impl Core {
101110
Ok(devices)
102111
}
103112

104-
/// Gets properties related to device behaviour for this core.
113+
/// Gets properties related to device behavior.
114+
///
105115
/// The method extracts information that can be set via the [`set_property`] method.
116+
///
117+
/// # Panics
118+
///
119+
/// This function panics in the unlikely case OpenVINO returns a non-UTF8 string.
106120
pub fn get_property(&self, device_name: &DeviceType, key: &PropertyKey) -> Result<String> {
107121
let ov_device_name = cstr!(device_name.as_ref());
108122
let ov_prop_key = cstr!(key.as_ref());
@@ -142,11 +156,11 @@ impl Core {
142156
/// Sets properties for a device.
143157
pub fn set_properties<'a>(
144158
&mut self,
145-
device_name: DeviceType,
159+
device_name: &DeviceType,
146160
properties: impl IntoIterator<Item = (RwPropertyKey, &'a str)>,
147161
) -> Result<()> {
148162
for (prop_key, prop_value) in properties {
149-
self.set_property(&device_name, &prop_key, prop_value)?;
163+
self.set_property(device_name, &prop_key, prop_value)?;
150164
}
151165
Ok(())
152166
}
@@ -177,7 +191,7 @@ impl Core {
177191
self.ptr,
178192
model_str.as_ptr().cast::<c_char>(),
179193
model_str.len(),
180-
weights_buffer.map_or(std::ptr::null(), |tensor| tensor.as_ptr()),
194+
weights_buffer.map_or(std::ptr::null(), Tensor::as_ptr),
181195
std::ptr::addr_of_mut!(ptr)
182196
))?;
183197
Ok(Model::from_ptr(ptr))

crates/openvino/src/element_type.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
use openvino_sys::*;
1+
use openvino_sys::{
2+
ov_element_type_e_BF16, ov_element_type_e_DYNAMIC, ov_element_type_e_F16,
3+
ov_element_type_e_F32, ov_element_type_e_F64, ov_element_type_e_F8E4M3,
4+
ov_element_type_e_F8E5M3, ov_element_type_e_I16, ov_element_type_e_I32, ov_element_type_e_I4,
5+
ov_element_type_e_I64, ov_element_type_e_I8, ov_element_type_e_NF4,
6+
ov_element_type_e_OV_BOOLEAN, ov_element_type_e_U1, ov_element_type_e_U16,
7+
ov_element_type_e_U32, ov_element_type_e_U4, ov_element_type_e_U64, ov_element_type_e_U8,
8+
ov_element_type_e_UNDEFINED,
9+
};
210

311
use std::convert::TryFrom;
412
use std::error::Error;

crates/openvino/src/layout.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ mod tests {
3939
.unwrap();
4040
let layout_desc = "NCHW";
4141
let layout = Layout::new(layout_desc).unwrap();
42-
assert_eq!(layout.ptr.is_null(), false);
42+
assert!(!layout.ptr.is_null());
4343
}
4444
}

crates/openvino/src/model.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ impl CompiledModel {
207207
}
208208

209209
/// Gets a property for the compiled model.
210-
pub fn get_property(&self, key: PropertyKey) -> Result<Cow<str>> {
210+
pub fn get_property(&self, key: &PropertyKey) -> Result<Cow<str>> {
211211
let ov_prop_key = cstr!(key.as_ref());
212212
let mut ov_prop_value = std::ptr::null_mut();
213213
try_unsafe!(ov_compiled_model_get_property(
@@ -220,7 +220,7 @@ impl CompiledModel {
220220
}
221221

222222
/// Sets a property for the compiled model.
223-
pub fn set_property(&mut self, key: RwPropertyKey, value: &str) -> Result<()> {
223+
pub fn set_property(&mut self, key: &RwPropertyKey, value: &str) -> Result<()> {
224224
let ov_prop_key = cstr!(key.as_ref());
225225
let ov_prop_value = cstr!(value);
226226
try_unsafe!(ov_compiled_model_set_property(

crates/openvino/src/node.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ impl Node {
3232
}
3333

3434
/// Get the data type of elements of the port.
35+
///
36+
/// # Panics
37+
///
38+
/// This function panics in the unlikely case OpenVINO returns an unknown element type.
3539
pub fn get_element_type(&self) -> Result<ElementType> {
3640
let mut element_type = ElementType::Undefined as u32;
3741
try_unsafe!(ov_port_get_element_type(

crates/openvino/src/property.rs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::borrow::Cow;
22

33
/// See [`Property`](https://docs.openvino.ai/2024/api/c_cpp_api/group__ov__property__c__api.html).
4-
/// `PropertyKey` represents valid configuration properties for a [crate::Core] instance.
4+
/// `PropertyKey` represents valid configuration properties for a [`crate::Core`] instance.
55
#[derive(Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
66
pub enum PropertyKey {
77
/// A string list of supported read-only properties.
@@ -10,8 +10,8 @@ pub enum PropertyKey {
1010
AvailableDevices,
1111
/// An unsigned integer value of optimal number of compiled model infer requests.
1212
OptimalNumberOfInferRequests,
13-
/// A hint for a range for number of async infer requests.
14-
/// If device supports streams, the metric provides range for number of IRs per stream.
13+
/// A hint for a range for number of async infer requests. If a device supports streams, the
14+
/// metric provides the range for number of IRs per stream.
1515
RangeForAsyncInferRequests,
1616
/// Information about a range for streams on platforms where streams are supported.
1717
RangeForStreams,
@@ -27,7 +27,7 @@ pub enum PropertyKey {
2727
MaxBatchSize,
2828
/// Read-write property key.
2929
Rw(RwPropertyKey),
30-
/// Arbitrary string property key.
30+
/// An arbitrary key.
3131
Other(Cow<'static, str>),
3232
}
3333

@@ -36,9 +36,9 @@ pub enum PropertyKey {
3636
pub enum RwPropertyKey {
3737
/// The directory which will be used to store any data cached by plugins.
3838
CacheDir,
39-
/// The cache mode between optimize_size and optimize_speed.
40-
/// If optimize_size is selected, smaller cache files will be created.
41-
/// If optimize_speed is selected, loading time will decrease but the cache file size will increase.
39+
/// The cache mode between `optimize_size` and `optimize_speed`. If `optimize_size` is selected,
40+
/// smaller cache files will be created. If `optimize_speed` is selected, loading time will
41+
/// decrease but the cache file size will increase.
4242
CacheMode,
4343
/// The number of executor logical partitions.
4444
NumStreams,
@@ -56,36 +56,35 @@ pub enum RwPropertyKey {
5656
HintSchedulingCoreType,
5757
/// Hint for device to use specified precision for inference.
5858
HintInferencePrecision,
59-
/// Backs the Performance Hints by giving
60-
/// additional information on how many inference requests the application will be
61-
/// keeping in flight usually this value comes from the actual use-case (e.g.
62-
/// number of video-cameras, or other sources of inputs)
59+
/// Backs the performance hints by giving additional information on how many inference requests
60+
/// the application will be keeping in flight usually this value comes from the actual use-case
61+
/// (e.g. number of video-cameras, or other sources of inputs)
6362
HintNumRequests,
6463
/// Desirable log level.
6564
LogLevel,
6665
/// High-level OpenVINO model priority hint.
6766
HintModelPriority,
6867
/// Performance counters.
6968
EnableProfiling,
70-
/// Device Priorities config option,
71-
/// with comma-separated devices listed in the desired priority.
69+
/// Device priorities configuration, with comma-separated devices listed in the desired
70+
/// priority.
7271
DevicePriorities,
73-
/// High-level OpenVINO Execution hint
74-
/// unlike low-level properties that are individual (per-device), the hints are something that every device accepts
75-
/// and turns into device-specific settings
76-
/// Execution mode hint controls preferred optimization targets (performance or accuracy) for given model
72+
/// A high-level OpenVINO execution hint. Unlike low-level properties that are individual
73+
/// (per-device), the hints are something that every device accepts and turns into
74+
/// device-specific settings, the execution mode hint controls preferred optimization targets
75+
/// (performance or accuracy) for a given model.
7776
///
7877
/// It can be set to be below value:
79-
/// - `"PERFORMANCE"`: Optimize for max performance
80-
/// - `"ACCURACY"`: Optimize for max accuracy
78+
/// - `"PERFORMANCE"`: optimize for max performance
79+
/// - `"ACCURACY"`: optimize for max accuracy
8180
HintExecutionMode,
8281
/// Whether to force terminate TBB when OV Core is destroyed.
8382
ForceTbbTerminate,
8483
/// Configure `mmap()` use for model read.
8584
EnableMmap,
8685
/// ?
8786
AutoBatchTimeout,
88-
/// Arbitrary string property key.
87+
/// An arbitrary key.
8988
Other(Cow<'static, str>),
9089
}
9190

crates/openvino/src/tensor.rs

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl Tensor {
4141
}
4242

4343
/// (Re)Set the shape of the tensor to a new shape.
44-
pub fn set_shape(&self, shape: Shape) -> Result<Self> {
44+
pub fn set_shape(&self, shape: &Shape) -> Result<Self> {
4545
try_unsafe!(ov_tensor_set_shape(self.ptr, shape.as_c_struct()))?;
4646
Ok(Self { ptr: self.ptr })
4747
}
@@ -57,6 +57,10 @@ impl Tensor {
5757
}
5858

5959
/// Get the data type of elements of the tensor.
60+
///
61+
/// # Panics
62+
///
63+
/// This function panics in the unlikely case OpenVINO returns an unknown element type.
6064
pub fn get_element_type(&self) -> Result<ElementType> {
6165
let mut element_type = ElementType::Undefined as u32;
6266
try_unsafe!(ov_tensor_get_element_type(
@@ -135,12 +139,14 @@ impl Tensor {
135139
/// Convenience function for checking that we can cast `data` to a slice of `T`, returning the
136140
/// length of that slice.
137141
fn get_safe_len<T>(data: &[u8]) -> usize {
138-
if data.len() % std::mem::size_of::<T>() != 0 {
139-
panic!("data size is not a multiple of the size of `T`");
140-
}
141-
if data.as_ptr() as usize % std::mem::align_of::<T>() != 0 {
142-
panic!("raw data is not aligned to `T`'s alignment");
143-
}
142+
assert!(
143+
data.len() % std::mem::size_of::<T>() == 0,
144+
"data size is not a multiple of the size of `T`"
145+
);
146+
assert!(
147+
data.as_ptr() as usize % std::mem::align_of::<T>() == 0,
148+
"raw data is not aligned to `T`'s alignment"
149+
);
144150
data.len() / std::mem::size_of::<T>()
145151
}
146152

@@ -151,66 +157,51 @@ mod tests {
151157
#[test]
152158
fn test_create_tensor() {
153159
openvino_sys::library::load().unwrap();
154-
let shape = Shape::new(&vec![1, 3, 227, 227]).unwrap();
160+
let shape = Shape::new(&[1, 3, 227, 227]).unwrap();
155161
let tensor = Tensor::new(ElementType::F32, &shape).unwrap();
156162
assert!(!tensor.ptr.is_null());
157163
}
158164

159165
#[test]
160166
fn test_get_shape() {
161167
openvino_sys::library::load().unwrap();
162-
let tensor = Tensor::new(
163-
ElementType::F32,
164-
&Shape::new(&vec![1, 3, 227, 227]).unwrap(),
165-
)
166-
.unwrap();
168+
let tensor =
169+
Tensor::new(ElementType::F32, &Shape::new(&[1, 3, 227, 227]).unwrap()).unwrap();
167170
let shape = tensor.get_shape().unwrap();
168171
assert_eq!(shape.get_rank(), 4);
169172
}
170173

171174
#[test]
172175
fn test_get_element_type() {
173176
openvino_sys::library::load().unwrap();
174-
let tensor = Tensor::new(
175-
ElementType::F32,
176-
&Shape::new(&vec![1, 3, 227, 227]).unwrap(),
177-
)
178-
.unwrap();
177+
let tensor =
178+
Tensor::new(ElementType::F32, &Shape::new(&[1, 3, 227, 227]).unwrap()).unwrap();
179179
let element_type = tensor.get_element_type().unwrap();
180180
assert_eq!(element_type, ElementType::F32);
181181
}
182182

183183
#[test]
184184
fn test_get_size() {
185185
openvino_sys::library::load().unwrap();
186-
let tensor = Tensor::new(
187-
ElementType::F32,
188-
&Shape::new(&vec![1, 3, 227, 227]).unwrap(),
189-
)
190-
.unwrap();
186+
let tensor =
187+
Tensor::new(ElementType::F32, &Shape::new(&[1, 3, 227, 227]).unwrap()).unwrap();
191188
let size = tensor.get_size().unwrap();
192-
assert_eq!(size, 1 * 3 * 227 * 227);
189+
assert_eq!(size, 3 * 227 * 227);
193190
}
194191

195192
#[test]
196193
fn test_get_byte_size() {
197194
openvino_sys::library::load().unwrap();
198-
let tensor = Tensor::new(
199-
ElementType::F32,
200-
&Shape::new(&vec![1, 3, 227, 227]).unwrap(),
201-
)
202-
.unwrap();
195+
let tensor =
196+
Tensor::new(ElementType::F32, &Shape::new(&[1, 3, 227, 227]).unwrap()).unwrap();
203197
let byte_size = tensor.get_byte_size().unwrap();
204-
assert_eq!(
205-
byte_size,
206-
1 * 3 * 227 * 227 * std::mem::size_of::<f32>() as usize
207-
);
198+
assert_eq!(byte_size, 3 * 227 * 227 * std::mem::size_of::<f32>());
208199
}
209200

210201
#[test]
211202
fn casting() {
212203
openvino_sys::library::load().unwrap();
213-
let shape = Shape::new(&vec![10, 10, 10]).unwrap();
204+
let shape = Shape::new(&[10, 10, 10]).unwrap();
214205
let tensor = Tensor::new(ElementType::F32, &shape).unwrap();
215206
let data = tensor.get_data::<f32>().unwrap();
216207
assert_eq!(data.len(), 10 * 10 * 10);
@@ -220,7 +211,7 @@ mod tests {
220211
#[should_panic(expected = "data size is not a multiple of the size of `T`")]
221212
fn casting_check() {
222213
openvino_sys::library::load().unwrap();
223-
let shape = Shape::new(&vec![10, 10, 10]).unwrap();
214+
let shape = Shape::new(&[10, 10, 10]).unwrap();
224215
let tensor = Tensor::new(ElementType::F32, &shape).unwrap();
225216
#[allow(dead_code)]
226217
struct LargeOddType([u8; 1061]);

crates/openvino/tests/classify-alexnet.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ fn classify_alexnet() -> anyhow::Result<()> {
2525

2626
// Retrieve the tensor from the test fixtures.
2727
let data = fs::read(Fixture::tensor())?;
28-
let input_shape = Shape::new(&vec![1, 227, 227, 3])?;
28+
let input_shape = Shape::new(&[1, 227, 227, 3])?;
2929
let element_type = ElementType::F32;
3030
let mut tensor = Tensor::new(element_type, &input_shape)?;
3131
let buffer = tensor.get_raw_data_mut()?;
@@ -53,7 +53,7 @@ fn classify_alexnet() -> anyhow::Result<()> {
5353
let mut infer_request = executable_model.create_infer_request()?;
5454
infer_request.set_tensor("data", &tensor)?;
5555
infer_request.infer()?;
56-
let mut results = infer_request.get_tensor(&output_port.get_name()?)?;
56+
let results = infer_request.get_tensor(&output_port.get_name()?)?;
5757

5858
// Sort results.
5959
let buffer = results.get_data::<f32>()?.to_vec();

crates/openvino/tests/classify-inception.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ fn classify_inception() -> anyhow::Result<()> {
2525

2626
// Retrieve the tensor from the test fixtures.
2727
let data = fs::read(Fixture::tensor())?;
28-
let input_shape = Shape::new(&vec![1, 299, 299, 3])?;
28+
let input_shape = Shape::new(&[1, 299, 299, 3])?;
2929
let element_type = ElementType::F32;
3030
let mut tensor = Tensor::new(element_type, &input_shape)?;
3131
let buffer = tensor.get_raw_data_mut()?;
@@ -50,7 +50,7 @@ fn classify_inception() -> anyhow::Result<()> {
5050
let mut infer_request = executable_model.create_infer_request()?;
5151
infer_request.set_tensor("input", &tensor)?;
5252
infer_request.infer()?;
53-
let mut results = infer_request.get_tensor(&output_port.get_name()?)?;
53+
let results = infer_request.get_tensor(&output_port.get_name()?)?;
5454

5555
// Sort results.
5656
let buffer = results.get_data::<f32>()?.to_vec();

0 commit comments

Comments
 (0)