Skip to content

Commit f15b646

Browse files
authored
Fix several clippy-related issues (#100)
* Fix clippy-related issues Running `cargo clippy` revealed several small issues that this change fixes. * Use the `cstr!` macro in more places * Improve documentation for non-macro-dropped structs * `Tensor::new_from_instance` does not need to return a `Result`
1 parent 656ae28 commit f15b646

File tree

9 files changed

+63
-61
lines changed

9 files changed

+63
-61
lines changed

crates/openvino/src/core.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use crate::error::LoadingError;
55
use crate::{cstr, drop_using_function, try_unsafe, util::Result};
66
use crate::{model::CompiledModel, Model};
77
use crate::{SetupError, Tensor};
8-
98
use openvino_sys::{
109
self, ov_core_compile_model, ov_core_create, ov_core_create_with_config, ov_core_free,
1110
ov_core_read_model, ov_core_read_model_from_memory_buffer, ov_core_t,
@@ -28,7 +27,7 @@ impl Core {
2827
Ok(Core { instance })
2928
}
3029

31-
///Construct a new OpenVINO [`Core`] with config specified in an xml file.
30+
/// Construct a new OpenVINO [`Core`] with config specified in an xml file.
3231
pub fn new_with_config(xml_config_file: &str) -> std::result::Result<Core, SetupError> {
3332
let mut instance = std::ptr::null_mut();
3433
try_unsafe!(ov_core_create_with_config(
@@ -51,7 +50,7 @@ impl Core {
5150
Ok(Model::new_from_instance(instance))
5251
}
5352

54-
///Read model with model and weights loaded in memory.
53+
/// Read model with model and weights loaded in memory.
5554
pub fn read_model_from_buffer(
5655
&mut self,
5756
model_str: &str,

crates/openvino/src/layout.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use crate::{drop_using_function, try_unsafe, util::Result};
1+
use crate::{cstr, drop_using_function, try_unsafe, util::Result};
22
use openvino_sys::{ov_layout_create, ov_layout_free, ov_layout_t};
3-
use std::ffi::CString;
43

54
/// See [`Layout`](https://docs.openvino.ai/2023.3/api/c_cpp_api/group__ov__layout__c__api.html).
65
pub struct Layout {
@@ -17,9 +16,8 @@ impl Layout {
1716
/// Creates a new layout with the given description.
1817
pub fn new(layout_desc: &str) -> Result<Self> {
1918
let mut layout = std::ptr::null_mut();
20-
let c_layout_desc = CString::new(layout_desc).unwrap();
2119
try_unsafe!(ov_layout_create(
22-
c_layout_desc.as_ptr(),
20+
cstr!(layout_desc),
2321
std::ptr::addr_of_mut!(layout)
2422
))?;
2523
Ok(Self { instance: layout })

crates/openvino/src/model.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ drop_using_function!(CompiledModel, ov_compiled_model_free);
9494
unsafe impl Send for CompiledModel {}
9595

9696
impl CompiledModel {
97-
/// Create a new instance of the CompiledModel struct from ov_compiled_model_t.
97+
/// Create a new instance of the [`CompiledModel`] from an internal `ov_compiled_model_t`.
9898
pub(crate) fn new(instance: *mut ov_compiled_model_t) -> Self {
9999
Self { instance }
100100
}

crates/openvino/src/partial_shape.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ pub struct PartialShape {
1313
}
1414

1515
impl Drop for PartialShape {
16-
/// Drops the `PartialShape` instance and frees the associated memory.
16+
// We don't use the `drop...!` macro here since:
17+
// - the `instance` field is not a pointer as with other types.
1718
fn drop(&mut self) {
1819
unsafe { ov_partial_shape_free(std::ptr::addr_of_mut!(self.instance)) }
1920
}
@@ -86,6 +87,10 @@ impl PartialShape {
8687
}
8788

8889
/// Returns the dimensions of the partial shape.
90+
///
91+
/// # Panics
92+
///
93+
/// Panics in the unlikely case the rank cannot be represented as a `usize`.
8994
pub fn get_dimensions(&self) -> &[Dimension] {
9095
if self.instance.dims.is_null() {
9196
&[]

crates/openvino/src/prepostprocess.rs

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
//! model_info.model_info_set_layout(&Layout::new("NCHW").expect("to create a new layout")).expect("to set layout");
3030
//! let new_model = pre_post_process.build_new_model().expect("to build new model with above prepostprocess steps");
3131
//! ```
32+
use crate::{
33+
cstr, drop_using_function, layout::Layout, try_unsafe, util::Result, ElementType, Model, Tensor,
34+
};
3235
use openvino_sys::{
3336
ov_preprocess_input_info_free, ov_preprocess_input_info_get_model_info,
3437
ov_preprocess_input_info_get_preprocess_steps, ov_preprocess_input_info_get_tensor_info,
@@ -49,11 +52,6 @@ use openvino_sys::{
4952
ov_preprocess_preprocess_steps_convert_layout, ov_preprocess_preprocess_steps_free,
5053
ov_preprocess_preprocess_steps_resize, ov_preprocess_preprocess_steps_t,
5154
};
52-
use std::ffi::CString;
53-
54-
use crate::{
55-
drop_using_function, layout::Layout, try_unsafe, util::Result, ElementType, Model, Tensor,
56-
};
5755

5856
/// See [`PrePostProcess`](https://docs.openvino.ai/2023.3/api/c_cpp_api/structov__preprocess__prepostprocessor__t.html).
5957
#[derive(Debug)]
@@ -142,7 +140,7 @@ impl PreProcessInputTensorInfo {
142140
}
143141

144142
impl PrePostProcess {
145-
/// Creates a new `PrePostprocess` instance for the given model.
143+
/// Creates a new `PrePostProcess` instance for the given model.
146144
pub fn new(model: &Model) -> Result<Self> {
147145
let mut preprocess = std::ptr::null_mut();
148146
try_unsafe!(ov_preprocess_prepostprocessor_create(
@@ -172,10 +170,9 @@ impl PrePostProcess {
172170
/// Retrieves the input information by name.
173171
pub fn get_input_info_by_name(&self, name: &str) -> Result<PreProcessInputInfo> {
174172
let mut input_info = std::ptr::null_mut();
175-
let c_layout_desc = CString::new(name).unwrap();
176173
try_unsafe!(ov_preprocess_prepostprocessor_get_input_info_by_name(
177174
self.instance,
178-
c_layout_desc.as_ptr(),
175+
cstr!(name),
179176
std::ptr::addr_of_mut!(input_info)
180177
))?;
181178

@@ -186,17 +183,13 @@ impl PrePostProcess {
186183

187184
/// Retrieves the output information by name.
188185
pub fn get_output_info_by_name(&self, name: &str) -> Result<PreProcessOutputInfo> {
189-
let mut output_info = std::ptr::null_mut();
190-
let c_layout_desc = CString::new(name).unwrap();
186+
let mut instance = std::ptr::null_mut();
191187
try_unsafe!(ov_preprocess_prepostprocessor_get_output_info_by_name(
192188
self.instance,
193-
c_layout_desc.as_ptr(),
194-
std::ptr::addr_of_mut!(output_info)
189+
cstr!(name),
190+
std::ptr::addr_of_mut!(instance)
195191
))?;
196-
197-
Ok(PreProcessOutputInfo {
198-
instance: output_info,
199-
})
192+
Ok(PreProcessOutputInfo { instance })
200193
}
201194

202195
/// Retrieves the output information by index.
@@ -214,17 +207,18 @@ impl PrePostProcess {
214207
}
215208

216209
/// Retrieves the input information.
210+
///
211+
/// # Panics
212+
///
213+
/// Panics if the returned input info is null.
217214
pub fn get_input_info(&self) -> Result<PreProcessInputInfo> {
218-
let mut input_info = std::ptr::null_mut();
215+
let mut instance = std::ptr::null_mut();
219216
try_unsafe!(ov_preprocess_prepostprocessor_get_input_info(
220217
self.instance,
221-
std::ptr::addr_of_mut!(input_info)
218+
std::ptr::addr_of_mut!(instance)
222219
))?;
223-
assert!(!input_info.is_null());
224-
225-
Ok(PreProcessInputInfo {
226-
instance: input_info,
227-
})
220+
assert!(!instance.is_null());
221+
Ok(PreProcessInputInfo { instance })
228222
}
229223

230224
/// Builds a new model with all steps from pre/postprocessing.

crates/openvino/src/request.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl InferRequest {
3838
cstr!(name),
3939
std::ptr::addr_of_mut!(tensor)
4040
))?;
41-
Ok(Tensor::new_from_instance(tensor).unwrap())
41+
Ok(Tensor::new_from_instance(tensor))
4242
}
4343

4444
/// Execute the inference request.

crates/openvino/src/shape.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ pub struct Shape {
88
}
99

1010
impl Drop for Shape {
11-
/// Drops the Shape instance and frees the associated memory.
12-
//Not using drop! macro since ov_shape_free returns an error code unlike other free methods.
11+
// We don't use the `drop...!` macro here since:
12+
// - `ov_shape_free` returns an error code unlike other free methods
13+
// - the `instance` field is not a pointer as with other types.
1314
fn drop(&mut self) {
1415
let code = unsafe { ov_shape_free(std::ptr::addr_of_mut!(self.instance)) };
1516
assert_eq!(code, 0);
@@ -19,12 +20,11 @@ impl Drop for Shape {
1920
}
2021

2122
impl Shape {
22-
/// Get the pointer to the underlying OpenVINO shape.
23-
pub(crate) fn instance(&self) -> ov_shape_t {
24-
self.instance
25-
}
26-
2723
/// Creates a new Shape instance with the given dimensions.
24+
///
25+
/// # Panics
26+
///
27+
/// Panics in the unlikely case the dimension length cannot be represented as an `i64`.
2828
pub fn new(dimensions: &[i64]) -> Result<Self> {
2929
let mut shape = ov_shape_t {
3030
rank: 8,
@@ -38,17 +38,26 @@ impl Shape {
3838
Ok(Self { instance: shape })
3939
}
4040

41-
/// Create a new shape object from ov_shape_t.
41+
/// Create a new shape object from `ov_shape_t`.
4242
pub(crate) fn new_from_instance(instance: ov_shape_t) -> Self {
4343
Self { instance }
4444
}
4545

46+
/// Get the pointer to the underlying OpenVINO shape.
47+
pub(crate) fn instance(&self) -> ov_shape_t {
48+
self.instance
49+
}
50+
4651
/// Returns the rank of the shape.
4752
pub fn get_rank(&self) -> i64 {
4853
self.instance.rank
4954
}
5055

5156
/// Returns the dimensions of the shape.
57+
///
58+
/// # Panics
59+
///
60+
/// Panics in the unlikely case the rank cannot be represented as a `usize`.
5261
pub fn get_dimensions(&self) -> &[i64] {
5362
if self.instance.dims.is_null() || self.instance.rank <= 0 {
5463
&[]
@@ -65,9 +74,8 @@ impl Shape {
6574

6675
#[cfg(test)]
6776
mod tests {
68-
use crate::LoadingError;
69-
7077
use super::*;
78+
use crate::LoadingError;
7179

7280
#[test]
7381
fn test_new_shape() {

crates/openvino/src/tensor.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,20 @@ pub struct Tensor {
1515
drop_using_function!(Tensor, ov_tensor_free);
1616

1717
impl Tensor {
18-
/// Get the pointer to the underlying OpenVINO tensor.
19-
pub fn instance(&self) -> *mut ov_tensor_t {
20-
self.instance
21-
}
22-
2318
/// Create a new [`Tensor`].
24-
pub fn new(data_type: ElementType, shape: &Shape) -> Result<Self> {
25-
let mut tensor = std::ptr::null_mut();
26-
let element_type = data_type as u32;
27-
let code = try_unsafe!(ov_tensor_create(
28-
element_type,
19+
pub fn new(element_type: ElementType, shape: &Shape) -> Result<Self> {
20+
let mut instance = std::ptr::null_mut();
21+
try_unsafe!(ov_tensor_create(
22+
element_type as u32,
2923
shape.instance(),
30-
std::ptr::addr_of_mut!(tensor),
31-
));
32-
assert_eq!(code, Ok(()));
33-
Ok(Self { instance: tensor })
24+
std::ptr::addr_of_mut!(instance),
25+
))?;
26+
Ok(Self { instance })
27+
}
28+
29+
/// Create a new [`Tensor`] from a instance pointer.
30+
pub(crate) fn new_from_instance(instance: *mut ov_tensor_t) -> Self {
31+
Self { instance }
3432
}
3533

3634
/// Create a new [`Tensor`] from a host pointer.
@@ -57,9 +55,9 @@ impl Tensor {
5755
Ok(Self { instance: tensor })
5856
}
5957

60-
///Create a new [`Tensor`] from a instance pointer.
61-
pub(crate) fn new_from_instance(instance: *mut ov_tensor_t) -> Result<Self> {
62-
Ok(Self { instance })
58+
/// Get the pointer to the underlying OpenVINO tensor.
59+
pub(crate) fn instance(&self) -> *mut ov_tensor_t {
60+
self.instance
6361
}
6462

6563
/// (Re)Set the shape of the tensor to a new shape.

crates/openvino/src/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ macro_rules! drop_using_function {
2929
($ty: ty, $free_fn: expr) => {
3030
impl Drop for $ty {
3131
fn drop(&mut self) {
32-
unsafe { $free_fn(self.instance as *mut _) }
32+
unsafe { $free_fn(self.instance.cast()) }
3333
}
3434
}
3535
};

0 commit comments

Comments
 (0)