Skip to content

Commit fbc5169

Browse files
authored
Fix new clippy warnings (#49)
A new version of Rust has introduced several new lints: this fixes the use of `$crate` in macros (an error) and uses `std::ptr::addr_of_mut!` in a variety of places (a warning).
1 parent 3e46a83 commit fbc5169

File tree

8 files changed

+43
-25
lines changed

8 files changed

+43
-25
lines changed

crates/openvino-sys/src/linking/runtime.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ macro_rules! link {
7474
///
7575
/// May fail if the `openvino-finder` cannot discover the library on the current system.
7676
pub fn load() -> Result<(), String> {
77-
match crate::library::find() {
77+
match $crate::library::find() {
7878
None => Err("Unable to find the `openvino_c` library to load".into()),
7979
Some(path) => load_from(path),
8080
}

crates/openvino/src/blob.rs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ impl Blob {
4343
pub fn allocate(description: &TensorDesc) -> Result<Self> {
4444
let mut instance = std::ptr::null_mut();
4545
try_unsafe!(ie_blob_make_memory(
46-
&description.instance as *const _,
47-
&mut instance as *mut *mut _
46+
std::ptr::addr_of!(description.instance),
47+
std::ptr::addr_of_mut!(instance)
4848
))?;
4949
Ok(Self { instance })
5050
}
@@ -54,16 +54,19 @@ impl Blob {
5454
let blob = self.instance as *const ie_blob_t;
5555

5656
let mut layout = Layout::ANY;
57-
try_unsafe!(ie_blob_get_layout(blob, &mut layout as *mut _))?;
57+
try_unsafe!(ie_blob_get_layout(blob, std::ptr::addr_of_mut!(layout)))?;
5858

5959
let mut dimensions = dimensions_t {
6060
ranks: 0,
6161
dims: [0; 8usize],
6262
};
63-
try_unsafe!(ie_blob_get_dims(blob, &mut dimensions as *mut _))?;
63+
try_unsafe!(ie_blob_get_dims(blob, std::ptr::addr_of_mut!(dimensions)))?;
6464

6565
let mut precision = Precision::UNSPECIFIED;
66-
try_unsafe!(ie_blob_get_precision(blob, &mut precision as *mut _))?;
66+
try_unsafe!(ie_blob_get_precision(
67+
blob,
68+
std::ptr::addr_of_mut!(precision)
69+
))?;
6770

6871
Ok(TensorDesc::new(layout, &dimensions.dims, precision))
6972
}
@@ -75,7 +78,7 @@ impl Blob {
7578
/// Panics if the returned OpenVINO size will not fit in `usize`.
7679
pub fn len(&mut self) -> Result<usize> {
7780
let mut size = 0;
78-
try_unsafe!(ie_blob_size(self.instance, &mut size as *mut _))?;
81+
try_unsafe!(ie_blob_size(self.instance, std::ptr::addr_of_mut!(size)))?;
7982
Ok(usize::try_from(size).unwrap())
8083
}
8184

@@ -86,14 +89,20 @@ impl Blob {
8689
/// Panics if the returned OpenVINO size will not fit in `usize`.
8790
pub fn byte_len(&mut self) -> Result<usize> {
8891
let mut size = 0;
89-
try_unsafe!(ie_blob_byte_size(self.instance, &mut size as *mut _))?;
92+
try_unsafe!(ie_blob_byte_size(
93+
self.instance,
94+
std::ptr::addr_of_mut!(size)
95+
))?;
9096
Ok(usize::try_from(size).unwrap())
9197
}
9298

9399
/// Retrieve the [`Blob`]'s data as an immutable slice of bytes.
94100
pub fn buffer(&mut self) -> Result<&[u8]> {
95101
let mut buffer = Blob::empty_buffer();
96-
try_unsafe!(ie_blob_get_buffer(self.instance, &mut buffer as *mut _))?;
102+
try_unsafe!(ie_blob_get_buffer(
103+
self.instance,
104+
std::ptr::addr_of_mut!(buffer)
105+
))?;
97106
let size = self.byte_len()?;
98107
let slice = unsafe {
99108
std::slice::from_raw_parts(buffer.__bindgen_anon_1.buffer as *const u8, size)
@@ -104,7 +113,10 @@ impl Blob {
104113
/// Retrieve the [`Blob`]'s data as a mutable slice of bytes.
105114
pub fn buffer_mut(&mut self) -> Result<&mut [u8]> {
106115
let mut buffer = Blob::empty_buffer();
107-
try_unsafe!(ie_blob_get_buffer(self.instance, &mut buffer as *mut _))?;
116+
try_unsafe!(ie_blob_get_buffer(
117+
self.instance,
118+
std::ptr::addr_of_mut!(buffer)
119+
))?;
108120
let size = self.byte_len()?;
109121
let slice = unsafe {
110122
std::slice::from_raw_parts_mut(buffer.__bindgen_anon_1.buffer.cast::<u8>(), size)
@@ -122,7 +134,10 @@ impl Blob {
122134
/// `results.buffer_mut_as_type::<f32>()`.
123135
pub unsafe fn buffer_mut_as_type<T>(&mut self) -> Result<&mut [T]> {
124136
let mut buffer = Blob::empty_buffer();
125-
InferenceError::from(ie_blob_get_buffer(self.instance, &mut buffer as *mut _))?;
137+
InferenceError::from(ie_blob_get_buffer(
138+
self.instance,
139+
std::ptr::addr_of_mut!(buffer),
140+
))?;
126141
// This is very unsafe, but very convenient: by allowing users to specify T, they can
127142
// retrieve the buffer in whatever shape they prefer. But we must ensure that they cannot
128143
// read too many bytes, so we manually calculate the resulting slice `size`.

crates/openvino/src/core.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ impl Core {
3939
Some(f) => f.to_string(),
4040
};
4141
let mut instance = std::ptr::null_mut();
42-
try_unsafe!(ie_core_create(cstr!(file), &mut instance as *mut *mut _))?;
42+
try_unsafe!(ie_core_create(
43+
cstr!(file),
44+
std::ptr::addr_of_mut!(instance)
45+
))?;
4346
Ok(Core { instance })
4447
}
4548

@@ -55,7 +58,7 @@ impl Core {
5558
self.instance,
5659
cstr!(model_path),
5760
cstr!(weights_path),
58-
&mut instance as *mut *mut _,
61+
std::ptr::addr_of_mut!(instance)
5962
))?;
6063
Ok(CNNNetwork { instance })
6164
}
@@ -75,7 +78,7 @@ impl Core {
7578
model_content.as_ptr().cast::<u8>(),
7679
model_content.len(),
7780
weights_blob.instance,
78-
&mut instance as *mut *mut _,
81+
std::ptr::addr_of_mut!(instance)
7982
))?;
8083
Ok(CNNNetwork { instance })
8184
}
@@ -99,8 +102,8 @@ impl Core {
99102
self.instance,
100103
network.instance,
101104
cstr!(device),
102-
&empty_config as *const _,
103-
&mut instance as *mut *mut _
105+
std::ptr::addr_of!(empty_config),
106+
std::ptr::addr_of_mut!(instance)
104107
))?;
105108
Ok(ExecutableNetwork { instance })
106109
}

crates/openvino/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use thiserror::Error;
44
/// [`IEStatusCode`](https://docs.openvinotoolkit.org/latest/ie_c_api/ie__c__api_8h.html#a391683b1e8e26df8b58d7033edd9ee83).
55
// TODO This could be auto-generated (https://github.com/intel/openvino-rs/issues/20).
66
#[allow(missing_docs)]
7-
#[derive(Debug, Error, PartialEq)]
7+
#[derive(Debug, Error, PartialEq, Eq)]
88
pub enum InferenceError {
99
#[error("general error")]
1010
GeneralError,

crates/openvino/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@ pub fn version() -> String {
5353
let str_version = unsafe { CStr::from_ptr(ie_version.api_version) }
5454
.to_string_lossy()
5555
.into_owned();
56-
unsafe { openvino_sys::ie_version_free(&mut ie_version as *mut openvino_sys::ie_version_t) };
56+
unsafe { openvino_sys::ie_version_free(std::ptr::addr_of_mut!(ie_version)) };
5757
str_version
5858
}

crates/openvino/src/network.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ impl CNNNetwork {
4242
try_unsafe!(ie_network_get_input_name(
4343
self.instance,
4444
index,
45-
&mut c_name as *mut *mut _
45+
std::ptr::addr_of_mut!(c_name)
4646
))?;
4747
let rust_name = unsafe { CStr::from_ptr(c_name) }
4848
.to_string_lossy()
4949
.into_owned();
50-
unsafe { ie_network_name_free(&mut c_name as *mut *mut _) };
50+
unsafe { ie_network_name_free(std::ptr::addr_of_mut!(c_name)) };
5151
debug_assert!(c_name.is_null());
5252
Ok(rust_name)
5353
}
@@ -58,12 +58,12 @@ impl CNNNetwork {
5858
try_unsafe!(ie_network_get_output_name(
5959
self.instance,
6060
index,
61-
&mut c_name as *mut *mut _
61+
std::ptr::addr_of_mut!(c_name)
6262
))?;
6363
let rust_name = unsafe { CStr::from_ptr(c_name) }
6464
.to_string_lossy()
6565
.into_owned();
66-
unsafe { ie_network_name_free(&mut c_name as *mut *mut _) };
66+
unsafe { ie_network_name_free(std::ptr::addr_of_mut!(c_name)) };
6767
debug_assert!(c_name.is_null());
6868
Ok(rust_name)
6969
}
@@ -122,7 +122,7 @@ impl ExecutableNetwork {
122122
let mut instance = std::ptr::null_mut();
123123
try_unsafe!(ie_exec_network_create_infer_request(
124124
self.instance,
125-
&mut instance as *mut *mut _
125+
std::ptr::addr_of_mut!(instance)
126126
))?;
127127
Ok(InferRequest { instance })
128128
}

crates/openvino/src/request.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl InferRequest {
3333
try_unsafe!(ie_infer_request_get_blob(
3434
self.instance,
3535
cstr!(name),
36-
&mut instance as *mut *mut _
36+
std::ptr::addr_of_mut!(instance)
3737
))?;
3838
Ok(unsafe { Blob::from_raw_pointer(instance) })
3939
}

crates/openvino/src/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ macro_rules! cstr {
1818
#[macro_export]
1919
macro_rules! try_unsafe {
2020
($e: expr) => {
21-
crate::InferenceError::from(unsafe { $e })
21+
$crate::InferenceError::from(unsafe { $e })
2222
};
2323
}
2424

0 commit comments

Comments
 (0)