Skip to content

Commit 4dbd919

Browse files
committed
Refactor: encapsulate PartialShape::c_struct per review
Signed-off-by: khengari77 <khengari77@gmail.com>
1 parent 6248f37 commit 4dbd919

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

crates/openvino/src/model.rs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl Model {
9797
pub fn reshape_single_input(&mut self, partial_shape: &PartialShape) -> Result<()> {
9898
try_unsafe!(openvino_sys::ov_model_reshape_single_input(
9999
self.ptr,
100-
partial_shape.c_struct
100+
partial_shape.as_c_struct()
101101
))
102102
}
103103

@@ -111,67 +111,70 @@ impl Model {
111111
try_unsafe!(openvino_sys::ov_model_reshape_input_by_name(
112112
self.ptr,
113113
name.as_ptr(),
114-
partial_shape.c_struct
114+
partial_shape.as_c_struct()
115115
))
116116
}
117117

118118
/// Reshape the model with a list of (name, `partial_shape`) pairs.
119-
pub fn reshape(&mut self, shapes: &[(&str, &PartialShape)]) -> Result<()> {
120-
let mut c_names = Vec::with_capacity(shapes.len());
121-
let mut c_shapes = Vec::with_capacity(shapes.len());
119+
pub fn reshape(&mut self, partial_shapes: &[(&str, &PartialShape)]) -> Result<()> {
120+
let mut c_names = Vec::with_capacity(partial_shapes.len());
121+
let mut c_shapes = Vec::with_capacity(partial_shapes.len());
122122
// We must keep the CStrings alive until the FFI call is finished
123-
let mut names_keep_alive = Vec::with_capacity(shapes.len());
123+
let mut names_keep_alive = Vec::with_capacity(partial_shapes.len());
124124

125-
for (name, shape) in shapes {
125+
for (name, partial_shape) in partial_shapes {
126126
let c_name = cstr!(*name);
127127
c_names.push(c_name.as_ptr());
128128
names_keep_alive.push(c_name);
129-
c_shapes.push(shape.c_struct);
129+
c_shapes.push(partial_shape.as_c_struct());
130130
}
131131

132132
try_unsafe!(openvino_sys::ov_model_reshape(
133133
self.ptr,
134134
c_names.as_mut_ptr(),
135135
c_shapes.as_ptr(),
136-
shapes.len()
136+
partial_shapes.len()
137137
))
138138
}
139139

140140
/// Reshape the model using a list of (`port_index`, `partial_shape`) pairs.
141-
pub fn reshape_by_port_indexes(&mut self, shapes: &[(usize, &PartialShape)]) -> Result<()> {
142-
let mut c_indexes = Vec::with_capacity(shapes.len());
143-
let mut c_shapes = Vec::with_capacity(shapes.len());
141+
pub fn reshape_by_port_indexes(
142+
&mut self,
143+
partial_shapes: &[(usize, &PartialShape)],
144+
) -> Result<()> {
145+
let mut c_indexes = Vec::with_capacity(partial_shapes.len());
146+
let mut c_shapes = Vec::with_capacity(partial_shapes.len());
144147

145-
for (index, shape) in shapes {
148+
for (index, partial_shape) in partial_shapes {
146149
c_indexes.push(*index);
147-
c_shapes.push(shape.c_struct);
150+
c_shapes.push(partial_shape.as_c_struct());
148151
}
149152

150153
try_unsafe!(openvino_sys::ov_model_reshape_by_port_indexes(
151154
self.ptr,
152155
c_indexes.as_ptr(),
153156
c_shapes.as_ptr(),
154-
shapes.len()
157+
partial_shapes.len()
155158
))
156159
}
157160

158161
/// Reshape the model using a list of (Node/Port, `partial_shape`) pairs.
159-
pub fn reshape_by_ports(&mut self, shapes: &[(&Node, &PartialShape)]) -> Result<()> {
160-
let mut c_ports = Vec::with_capacity(shapes.len());
161-
let mut c_shapes = Vec::with_capacity(shapes.len());
162+
pub fn reshape_by_ports(&mut self, partial_shapes: &[(&Node, &PartialShape)]) -> Result<()> {
163+
let mut c_ports = Vec::with_capacity(partial_shapes.len());
164+
let mut c_shapes = Vec::with_capacity(partial_shapes.len());
162165

163-
for (node, shape) in shapes {
166+
for (node, partial_shape) in partial_shapes {
164167
// ov_model_reshape_by_ports expects *const *const ov_output_port_t.
165168
// Node::as_ptr returns *mut ov_output_const_port_t.
166169
c_ports.push(node.as_ptr() as *const openvino_sys::ov_output_port_t);
167-
c_shapes.push(shape.c_struct);
170+
c_shapes.push(partial_shape.as_c_struct());
168171
}
169172

170173
try_unsafe!(openvino_sys::ov_model_reshape_by_ports(
171174
self.ptr,
172175
c_ports.as_mut_ptr(),
173176
c_shapes.as_ptr(),
174-
shapes.len()
177+
partial_shapes.len()
175178
))
176179
}
177180
}

crates/openvino/src/partial_shape.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::convert::TryInto;
1010
/// See
1111
/// [`ov_partial_shape_t`](https://docs.openvino.ai/2024/api/c_cpp_api/group__ov__partial__shape__c__api.html).
1212
pub struct PartialShape {
13-
pub(crate) c_struct: ov_partial_shape_t,
13+
c_struct: ov_partial_shape_t,
1414
}
1515

1616
impl Drop for PartialShape {
@@ -28,6 +28,12 @@ impl PartialShape {
2828
Self { c_struct }
2929
}
3030

31+
/// Get the pointer to the underlying OpenVINO partial shape.
32+
#[inline]
33+
pub(crate) fn as_c_struct(&self) -> ov_partial_shape_t {
34+
self.c_struct
35+
}
36+
3137
/// Create a new [`PartialShape`] with a static rank and dynamic dimensions.
3238
pub fn new(rank: i64, dimensions: &[Dimension]) -> Result<Self> {
3339
let mut partial_shape = ov_partial_shape_t {

0 commit comments

Comments
 (0)