Skip to content

Commit 30fa16c

Browse files
Add as_mut_ptr and inline hints (#116)
* use as_mut_ptr and as_ptr * add inline hint * rename as_ptr and misc * update node::new to node::from_ptr
1 parent 0d93626 commit 30fa16c

File tree

14 files changed

+48
-29
lines changed

14 files changed

+48
-29
lines changed

crates/openvino/src/core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ impl Core {
218218
self.ptr,
219219
model_str.as_ptr().cast::<c_char>(),
220220
model_str.len(),
221-
weights_buffer.map_or(std::ptr::null(), |tensor| tensor.as_ptr().cast_const()),
221+
weights_buffer.map_or(std::ptr::null(), |tensor| tensor.as_ptr()),
222222
std::ptr::addr_of_mut!(ptr)
223223
))?;
224224
Ok(Model::from_ptr(ptr))

crates/openvino/src/dimension.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,21 @@ impl Eq for Dimension {}
1717

1818
impl Dimension {
1919
/// Creates a new Dimension with minimum and maximum values.
20+
#[inline]
2021
pub fn new(min: i64, max: i64) -> Self {
2122
Self {
2223
c_struct: ov_dimension_t { min, max },
2324
}
2425
}
2526

2627
/// Returns the minimum value.
28+
#[inline]
2729
pub fn get_min(&self) -> i64 {
2830
self.c_struct.min
2931
}
3032

3133
/// Returns the maximum value.
34+
#[inline]
3235
pub fn get_max(&self) -> i64 {
3336
self.c_struct.max
3437
}

crates/openvino/src/layout.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ drop_using_function!(Layout, ov_layout_free);
99

1010
impl Layout {
1111
/// Get a pointer to the [`ov_layout_t`].
12-
pub(crate) fn as_ptr(&self) -> *mut ov_layout_t {
12+
#[inline]
13+
pub(crate) fn as_mut_ptr(&mut self) -> *mut ov_layout_t {
1314
self.ptr
1415
}
1516

crates/openvino/src/model.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ unsafe impl Sync for Model {}
2828

2929
impl Model {
3030
/// Create a new [`Model`] from an internal pointer.
31+
#[inline]
3132
pub(crate) fn from_ptr(ptr: *mut ov_model_t) -> Self {
3233
Self { ptr }
3334
}
3435

3536
/// Get the pointer to the underlying [`ov_model_t`].
36-
pub(crate) fn as_ptr(&self) -> *mut ov_model_t {
37+
#[inline]
38+
pub(crate) fn as_ptr(&self) -> *const ov_model_t {
3739
self.ptr
3840
}
3941

@@ -59,7 +61,7 @@ impl Model {
5961
index,
6062
std::ptr::addr_of_mut!(node)
6163
))?;
62-
Ok(Node::new(node))
64+
Ok(Node::from_ptr(node))
6365
}
6466

6567
/// Retrieve the output node by index.
@@ -70,7 +72,7 @@ impl Model {
7072
index,
7173
std::ptr::addr_of_mut!(node)
7274
))?;
73-
Ok(Node::new(node))
75+
Ok(Node::from_ptr(node))
7476
}
7577

7678
/// Retrieve the constant output node by index.
@@ -81,7 +83,7 @@ impl Model {
8183
index,
8284
std::ptr::addr_of_mut!(node)
8385
))?;
84-
Ok(Node::new(node))
86+
Ok(Node::from_ptr(node))
8587
}
8688

8789
/// Returns `true` if the model contains dynamic shapes.
@@ -128,7 +130,7 @@ impl CompiledModel {
128130
self.ptr,
129131
std::ptr::addr_of_mut!(port)
130132
))?;
131-
Ok(Node::new(port))
133+
Ok(Node::from_ptr(port))
132134
}
133135

134136
/// Get an input port of the compiled model by port index.
@@ -139,7 +141,7 @@ impl CompiledModel {
139141
index,
140142
std::ptr::addr_of_mut!(port)
141143
))?;
142-
Ok(Node::new(port))
144+
Ok(Node::from_ptr(port))
143145
}
144146

145147
/// Get an input port of the compiled model by name.
@@ -151,7 +153,7 @@ impl CompiledModel {
151153
name,
152154
std::ptr::addr_of_mut!(port)
153155
))?;
154-
Ok(Node::new(port))
156+
Ok(Node::from_ptr(port))
155157
}
156158

157159
/// Get the number of outputs of the compiled model.
@@ -168,7 +170,7 @@ impl CompiledModel {
168170
self.ptr,
169171
std::ptr::addr_of_mut!(port)
170172
))?;
171-
Ok(Node::new(port))
173+
Ok(Node::from_ptr(port))
172174
}
173175

174176
/// Get an output port of the compiled model by port index.
@@ -179,7 +181,7 @@ impl CompiledModel {
179181
index,
180182
std::ptr::addr_of_mut!(port)
181183
))?;
182-
Ok(Node::new(port))
184+
Ok(Node::from_ptr(port))
183185
}
184186

185187
/// Get an output port of the compiled model by name.
@@ -191,7 +193,7 @@ impl CompiledModel {
191193
name,
192194
std::ptr::addr_of_mut!(port)
193195
))?;
194-
Ok(Node::new(port))
196+
Ok(Node::from_ptr(port))
195197
}
196198

197199
/// Gets runtime model information from a device.

crates/openvino/src/node.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ pub struct Node {
1313

1414
impl Node {
1515
/// Create a new [`Port`] from [`ov_output_const_port_t`].
16-
pub(crate) fn new(ptr: *mut ov_output_const_port_t) -> Self {
16+
#[inline]
17+
pub(crate) fn from_ptr(ptr: *mut ov_output_const_port_t) -> Self {
1718
Self { ptr }
1819
}
1920

crates/openvino/src/partial_shape.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ impl Drop for PartialShape {
2222

2323
impl PartialShape {
2424
/// Create a new partial shape object from `ov_partial_shape_t`.
25+
#[inline]
2526
pub(crate) fn from_c_struct(c_struct: ov_partial_shape_t) -> Self {
2627
Self { c_struct }
2728
}

crates/openvino/src/prepostprocess.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
//! let input_info = pipeline.get_input_info_by_name("input").expect("to get input info by name");
2222
//! let mut input_tensor_info = input_info.get_tensor_info().expect("to get tensor info");
2323
//! input_tensor_info.set_from(&tensor).expect("to set tensor from");
24-
//! input_tensor_info.set_layout(&Layout::new("NHWC").expect("to create a new layout")).expect("to set layout");
24+
//! input_tensor_info.set_layout(Layout::new("NHWC").expect("to create a new layout")).expect("to set layout");
2525
//! let mut preprocess_steps = input_info.get_steps().expect("to get preprocess steps");
2626
//! preprocess_steps.resize(ResizeAlgorithm::Linear).expect("to resize");
2727
//! let mut model_info = input_info.get_model_info().expect("to get model info");
28-
//! model_info.set_layout(&Layout::new("NCHW").expect("to create a new layout")).expect("to set layout");
28+
//! model_info.set_layout(Layout::new("NCHW").expect("to create a new layout")).expect("to set layout");
2929
//! let new_model = pipeline.build_new_model().expect("to build new model with above prepostprocess steps");
3030
//! ```
3131
use crate::{
@@ -205,10 +205,10 @@ pub struct InputModelInfo {
205205
drop_using_function!(InputModelInfo, ov_preprocess_input_model_info_free);
206206
impl InputModelInfo {
207207
/// Sets the layout for the model information obj.
208-
pub fn set_layout(&mut self, layout: &Layout) -> Result<()> {
208+
pub fn set_layout(&mut self, mut layout: Layout) -> Result<()> {
209209
try_unsafe!(ov_preprocess_input_model_info_set_layout(
210210
self.ptr,
211-
layout.as_ptr()
211+
layout.as_mut_ptr()
212212
))
213213
}
214214
}
@@ -220,10 +220,10 @@ pub struct InputTensorInfo {
220220
drop_using_function!(InputTensorInfo, ov_preprocess_input_tensor_info_free);
221221
impl InputTensorInfo {
222222
/// Sets the [`Layout`] for the input tensor.
223-
pub fn set_layout(&mut self, layout: &Layout) -> Result<()> {
223+
pub fn set_layout(&mut self, mut layout: Layout) -> Result<()> {
224224
try_unsafe!(ov_preprocess_input_tensor_info_set_layout(
225225
self.ptr,
226-
layout.as_ptr()
226+
layout.as_mut_ptr()
227227
))
228228
}
229229

@@ -266,10 +266,10 @@ impl Steps {
266266
}
267267

268268
/// Converts the [`Layout`] of the data in a [`Tensor`].
269-
pub fn convert_layout(&mut self, new_layout: &Layout) -> Result<()> {
269+
pub fn convert_layout(&mut self, mut new_layout: Layout) -> Result<()> {
270270
try_unsafe!(ov_preprocess_preprocess_steps_convert_layout(
271271
self.ptr,
272-
new_layout.as_ptr(),
272+
new_layout.as_mut_ptr(),
273273
))
274274
}
275275

crates/openvino/src/rank.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,33 @@ impl Eq for Rank {}
1717

1818
impl Rank {
1919
/// Get the pointer to the underlying OpenVINO rank.
20+
#[inline]
2021
pub(crate) fn as_c_struct(&self) -> ov_rank_t {
2122
self.c_struct
2223
}
2324

2425
/// Create a new rank object from `ov_rank_t`.
26+
#[inline]
2527
pub(crate) fn from_c_struct(ptr: ov_rank_t) -> Self {
2628
Self { c_struct: ptr }
2729
}
2830

2931
/// Creates a new Rank with minimum and maximum values.
32+
#[inline]
3033
pub fn new(min: i64, max: i64) -> Self {
3134
Self {
3235
c_struct: ov_rank_t { min, max },
3336
}
3437
}
3538

3639
/// Returns the minimum value.
40+
#[inline]
3741
pub fn get_min(&self) -> i64 {
3842
self.c_struct.min
3943
}
4044

4145
/// Returns the maximum value.
46+
#[inline]
4247
pub fn get_max(&self) -> i64 {
4348
self.c_struct.max
4449
}

crates/openvino/src/request.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ unsafe impl Sync for InferRequest {}
2020

2121
impl InferRequest {
2222
/// Create a new [`InferRequest`] from [`ov_infer_request_t`].
23+
#[inline]
2324
pub(crate) fn from_ptr(ptr: *mut ov_infer_request_t) -> Self {
2425
Self { ptr }
2526
}

crates/openvino/src/shape.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,19 @@ impl Shape {
3939
}
4040

4141
/// Create a new shape object from `ov_shape_t`.
42+
#[inline]
4243
pub(crate) fn from_c_struct(ptr: ov_shape_t) -> Self {
4344
Self { c_struct: ptr }
4445
}
4546

4647
/// Get the pointer to the underlying OpenVINO shape.
48+
#[inline]
4749
pub(crate) fn as_c_struct(&self) -> ov_shape_t {
4850
self.c_struct
4951
}
5052

5153
/// Returns the rank of the shape.
54+
#[inline]
5255
pub fn get_rank(&self) -> i64 {
5356
self.c_struct.rank
5457
}

0 commit comments

Comments
 (0)