Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/openvino-sys/src/linking/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ macro_rules! link {
///
/// May fail if the `openvino-finder` cannot discover the library on the current system.
pub fn load() -> Result<(), String> {
match crate::library::find() {
match $crate::library::find() {
None => Err("Unable to find the `openvino_c` library to load".into()),
Some(path) => load_from(path),
}
Expand Down
35 changes: 25 additions & 10 deletions crates/openvino/src/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ impl Blob {
pub fn allocate(description: &TensorDesc) -> Result<Self> {
let mut instance = std::ptr::null_mut();
try_unsafe!(ie_blob_make_memory(
&description.instance as *const _,
&mut instance as *mut *mut _
std::ptr::addr_of!(description.instance),
std::ptr::addr_of_mut!(instance)
))?;
Ok(Self { instance })
}
Expand All @@ -54,16 +54,19 @@ impl Blob {
let blob = self.instance as *const ie_blob_t;

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

let mut dimensions = dimensions_t {
ranks: 0,
dims: [0; 8usize],
};
try_unsafe!(ie_blob_get_dims(blob, &mut dimensions as *mut _))?;
try_unsafe!(ie_blob_get_dims(blob, std::ptr::addr_of_mut!(dimensions)))?;

let mut precision = Precision::UNSPECIFIED;
try_unsafe!(ie_blob_get_precision(blob, &mut precision as *mut _))?;
try_unsafe!(ie_blob_get_precision(
blob,
std::ptr::addr_of_mut!(precision)
))?;

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

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

/// Retrieve the [`Blob`]'s data as an immutable slice of bytes.
pub fn buffer(&mut self) -> Result<&[u8]> {
let mut buffer = Blob::empty_buffer();
try_unsafe!(ie_blob_get_buffer(self.instance, &mut buffer as *mut _))?;
try_unsafe!(ie_blob_get_buffer(
self.instance,
std::ptr::addr_of_mut!(buffer)
))?;
let size = self.byte_len()?;
let slice = unsafe {
std::slice::from_raw_parts(buffer.__bindgen_anon_1.buffer as *const u8, size)
Expand All @@ -104,7 +113,10 @@ impl Blob {
/// Retrieve the [`Blob`]'s data as a mutable slice of bytes.
pub fn buffer_mut(&mut self) -> Result<&mut [u8]> {
let mut buffer = Blob::empty_buffer();
try_unsafe!(ie_blob_get_buffer(self.instance, &mut buffer as *mut _))?;
try_unsafe!(ie_blob_get_buffer(
self.instance,
std::ptr::addr_of_mut!(buffer)
))?;
let size = self.byte_len()?;
let slice = unsafe {
std::slice::from_raw_parts_mut(buffer.__bindgen_anon_1.buffer.cast::<u8>(), size)
Expand All @@ -122,7 +134,10 @@ impl Blob {
/// `results.buffer_mut_as_type::<f32>()`.
pub unsafe fn buffer_mut_as_type<T>(&mut self) -> Result<&mut [T]> {
let mut buffer = Blob::empty_buffer();
InferenceError::from(ie_blob_get_buffer(self.instance, &mut buffer as *mut _))?;
InferenceError::from(ie_blob_get_buffer(
self.instance,
std::ptr::addr_of_mut!(buffer),
))?;
// This is very unsafe, but very convenient: by allowing users to specify T, they can
// retrieve the buffer in whatever shape they prefer. But we must ensure that they cannot
// read too many bytes, so we manually calculate the resulting slice `size`.
Expand Down
13 changes: 8 additions & 5 deletions crates/openvino/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ impl Core {
Some(f) => f.to_string(),
};
let mut instance = std::ptr::null_mut();
try_unsafe!(ie_core_create(cstr!(file), &mut instance as *mut *mut _))?;
try_unsafe!(ie_core_create(
cstr!(file),
std::ptr::addr_of_mut!(instance)
))?;
Ok(Core { instance })
}

Expand All @@ -55,7 +58,7 @@ impl Core {
self.instance,
cstr!(model_path),
cstr!(weights_path),
&mut instance as *mut *mut _,
std::ptr::addr_of_mut!(instance)
))?;
Ok(CNNNetwork { instance })
}
Expand All @@ -75,7 +78,7 @@ impl Core {
model_content.as_ptr().cast::<u8>(),
model_content.len(),
weights_blob.instance,
&mut instance as *mut *mut _,
std::ptr::addr_of_mut!(instance)
))?;
Ok(CNNNetwork { instance })
}
Expand All @@ -99,8 +102,8 @@ impl Core {
self.instance,
network.instance,
cstr!(device),
&empty_config as *const _,
&mut instance as *mut *mut _
std::ptr::addr_of!(empty_config),
std::ptr::addr_of_mut!(instance)
))?;
Ok(ExecutableNetwork { instance })
}
Expand Down
2 changes: 1 addition & 1 deletion crates/openvino/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use thiserror::Error;
/// [`IEStatusCode`](https://docs.openvinotoolkit.org/latest/ie_c_api/ie__c__api_8h.html#a391683b1e8e26df8b58d7033edd9ee83).
// TODO This could be auto-generated (https://github.com/intel/openvino-rs/issues/20).
#[allow(missing_docs)]
#[derive(Debug, Error, PartialEq)]
#[derive(Debug, Error, PartialEq, Eq)]
pub enum InferenceError {
#[error("general error")]
GeneralError,
Expand Down
2 changes: 1 addition & 1 deletion crates/openvino/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ pub fn version() -> String {
let str_version = unsafe { CStr::from_ptr(ie_version.api_version) }
.to_string_lossy()
.into_owned();
unsafe { openvino_sys::ie_version_free(&mut ie_version as *mut openvino_sys::ie_version_t) };
unsafe { openvino_sys::ie_version_free(std::ptr::addr_of_mut!(ie_version)) };
str_version
}
10 changes: 5 additions & 5 deletions crates/openvino/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ impl CNNNetwork {
try_unsafe!(ie_network_get_input_name(
self.instance,
index,
&mut c_name as *mut *mut _
std::ptr::addr_of_mut!(c_name)
))?;
let rust_name = unsafe { CStr::from_ptr(c_name) }
.to_string_lossy()
.into_owned();
unsafe { ie_network_name_free(&mut c_name as *mut *mut _) };
unsafe { ie_network_name_free(std::ptr::addr_of_mut!(c_name)) };
debug_assert!(c_name.is_null());
Ok(rust_name)
}
Expand All @@ -58,12 +58,12 @@ impl CNNNetwork {
try_unsafe!(ie_network_get_output_name(
self.instance,
index,
&mut c_name as *mut *mut _
std::ptr::addr_of_mut!(c_name)
))?;
let rust_name = unsafe { CStr::from_ptr(c_name) }
.to_string_lossy()
.into_owned();
unsafe { ie_network_name_free(&mut c_name as *mut *mut _) };
unsafe { ie_network_name_free(std::ptr::addr_of_mut!(c_name)) };
debug_assert!(c_name.is_null());
Ok(rust_name)
}
Expand Down Expand Up @@ -122,7 +122,7 @@ impl ExecutableNetwork {
let mut instance = std::ptr::null_mut();
try_unsafe!(ie_exec_network_create_infer_request(
self.instance,
&mut instance as *mut *mut _
std::ptr::addr_of_mut!(instance)
))?;
Ok(InferRequest { instance })
}
Expand Down
2 changes: 1 addition & 1 deletion crates/openvino/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl InferRequest {
try_unsafe!(ie_infer_request_get_blob(
self.instance,
cstr!(name),
&mut instance as *mut *mut _
std::ptr::addr_of_mut!(instance)
))?;
Ok(unsafe { Blob::from_raw_pointer(instance) })
}
Expand Down
2 changes: 1 addition & 1 deletion crates/openvino/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ macro_rules! cstr {
#[macro_export]
macro_rules! try_unsafe {
($e: expr) => {
crate::InferenceError::from(unsafe { $e })
$crate::InferenceError::from(unsafe { $e })
};
}

Expand Down