Skip to content

Commit 7ef481c

Browse files
authored
doc: add an in-memory tensor data example (#137)
* doc: add an in-memory tensor data example Closes #136. * doc: fix `cargo doc` warning * clippy: avoid `macro_metavars_in_unsafe` * Ensure OpenVINO libraries are loaded
1 parent 190261d commit 7ef481c

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

crates/openvino/src/core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ impl Core {
112112

113113
/// Gets properties related to device behavior.
114114
///
115-
/// The method extracts information that can be set via the [`set_property`] method.
115+
/// The method extracts information that can be set via the [`Core::set_property`] method.
116116
///
117117
/// # Panics
118118
///

crates/openvino/src/tensor.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,26 @@ use openvino_sys::{
1111
};
1212

1313
/// See [`Tensor`](https://docs.openvino.ai/2023.3/api/c_cpp_api/group__ov__tensor__c__api.html).
14+
///
15+
/// To create a tensor from in-memory data, construct it and then fill it:
16+
///
17+
/// ```rust
18+
/// # use openvino::{Shape, Tensor, ElementType};
19+
/// # fn main() -> anyhow::Result<()> {
20+
/// # openvino_sys::library::load().unwrap();
21+
/// let data = [1u8; 1000];
22+
/// let shape = Shape::new(&[10, 10, 10])?;
23+
/// let mut tensor = Tensor::new(ElementType::U8, &shape)?;
24+
/// tensor.get_raw_data_mut()?.copy_from_slice(&data);
25+
/// # Ok(())
26+
/// # }
27+
/// ```
28+
///
29+
/// This approach currently results in a copy, which is sub-optimal. It is safe, however; passing a
30+
/// slice to OpenVINO is unsafe unless additional lifetime constraints are added (to improve this in
31+
/// the future, see the context in [#125]).
32+
///
33+
/// [#125]: https://github.com/intel/openvino-rs/pull/125
1434
pub struct Tensor {
1535
ptr: *mut ov_tensor_t,
1636
}

crates/openvino/src/util.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ macro_rules! drop_using_function {
2727
($ty: ty, $free_fn: expr) => {
2828
impl Drop for $ty {
2929
fn drop(&mut self) {
30-
unsafe { $free_fn(self.ptr.cast()) }
30+
let free = $free_fn;
31+
unsafe { free(self.ptr.cast()) }
3132
}
3233
}
3334
};

0 commit comments

Comments
 (0)