Hi, I'm in trouble with copying memory in Rust
. Somehow it's very .....difficult to do that:
What I want to do is that I want to copy an opencv::core::Mat
(opencv
crate) memory into TensorFlowLite
buffer (tflite
crate).
-
Mat
struct, source code is herepub struct Mat { ptr: *mut c_void }
-
The
input tensor
I want to copy tolet input_tensor: *mut [u8] = self.interpreter.tensor_data_mut(input_index)?;
Here is the
tensor_data_mut
source code from the crate:pub fn tensor_data_mut<T>(&mut self, tensor_index: TensorIndex) -> Result<&mut [T]> where T: ElemKindOf, { let inner = self .tensor_inner(tensor_index) .ok_or_else(|| Error::internal_error("invalid tensor index"))?; let tensor_info: TensorInfo = inner.into(); if tensor_info.element_kind != T::elem_kind_of() { return Err(Error::InternalError(format!( "Invalid type reference of `{:?}` to the original type `{:?}`", T::elem_kind_of(), tensor_info.element_kind ))); } Ok(unsafe { slice::from_raw_parts_mut(inner.data.raw as *mut T, inner.bytes / mem::size_of::<T>()) }) }
I've already tried core::ptr::write
, std::ptr::copy_nonoverlapping
, std::ptr::copy
, all of them can't compile with the same error:
doesn't have a size known at compile-time
help: the trait `Sized` is not implemented for `[u8]`
Even tried to wrap Mat
into a box, still no luck.
For the minimal Mat
version, actually, I just copied the source code from opencv
:
pub struct FakeMat {
ptr: *mut std::ffi::c_void,
}
impl FakeMat {
pub fn as_raw(&self) -> *const std::ffi::c_void {
self.ptr
}
}
I know the compiler needs to know size during the compilation (and that's good), but for this case, I totally have no idea how to make that works in Rust
.....plz help