Skip to content

Commit

Permalink
Mark _Tp as non-Copyable to workaround bindgen bug (boncheolgu#69)
Browse files Browse the repository at this point in the history
* fix: workaround bindgen bug (rust-lang/rust-bindgen#2157)

* lint.

* fix: `slice::from_raw_parts(null(), 0)` is UB.

---------

Co-authored-by: Sanguk Park <[email protected]>
  • Loading branch information
2 people authored and CrazyChaoz committed Nov 20, 2024
1 parent 140577f commit cd84d68
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 39 deletions.
3 changes: 2 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ fn import_tflite_types() {
.clang_arg("c++")
.clang_arg("-std=c++11")
// required to get cross compilation for aarch64 to work because of an issue in flatbuffers
.clang_arg("-fms-extensions");
.clang_arg("-fms-extensions")
.no_copy("_Tp");

let bindings = bindings.generate().expect("Unable to generate bindings");

Expand Down
8 changes: 4 additions & 4 deletions src/interpreter/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ where
{
fn drop(&mut self) {
let handle = Box::into_raw(std::mem::take(&mut self.handle));
#[allow(clippy::forget_copy, clippy::useless_transmute, deprecated)]
#[allow(clippy::forgetting_copy_types, clippy::useless_transmute, deprecated)]
unsafe {
cpp!([handle as "InterpreterBuilder*"] {
delete handle;
Expand All @@ -49,7 +49,7 @@ where
let model_handle = model.as_ref().handle.deref();
let resolver_handle = resolver.get_resolver_handle();

#[allow(clippy::forget_copy, deprecated)]
#[allow(clippy::forgetting_copy_types, deprecated)]
unsafe {
cpp!([model_handle as "const FlatBufferModel*",
resolver_handle as "const OpResolver*"
Expand All @@ -66,7 +66,7 @@ where
}

pub fn build(mut self) -> Result<Interpreter<'a, Op>> {
#[allow(clippy::forget_copy, deprecated)]
#[allow(clippy::forgetting_copy_types, deprecated)]
let handle = {
let builder = &mut *self.handle;
unsafe {
Expand All @@ -87,7 +87,7 @@ where
mut self,
threads: std::os::raw::c_int,
) -> Result<Interpreter<'a, Op>> {
#[allow(clippy::forget_copy, deprecated)]
#[allow(clippy::forgetting_copy_types, deprecated)]
let handle = {
let builder = &mut *self.handle;
#[allow(clippy::transmute_num_to_bytes)]
Expand Down
4 changes: 2 additions & 2 deletions src/interpreter/fbmodel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Drop for FlatBufferModel {
fn drop(&mut self) {
let handle = Box::into_raw(mem::take(&mut self.handle));

#[allow(clippy::forget_copy, clippy::useless_transmute, deprecated)]
#[allow(clippy::forgetting_copy_types, clippy::useless_transmute, deprecated)]
unsafe {
cpp!([handle as "FlatBufferModel*"] {
delete handle;
Expand All @@ -40,7 +40,7 @@ impl FlatBufferModel {
let ptr = model_buffer.as_ptr();
let size = model_buffer.len();

#[allow(clippy::forget_copy, deprecated, clippy::transmute_num_to_bytes)]
#[allow(clippy::forgetting_copy_types, deprecated, clippy::transmute_num_to_bytes)]
let handle = unsafe {
cpp!([ptr as "const char*", size as "size_t"]
-> *mut bindings::FlatBufferModel as "FlatBufferModel*" {
Expand Down
30 changes: 15 additions & 15 deletions src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ where
{
fn drop(&mut self) {
let handle = Box::into_raw(mem::take(&mut self.handle));
#[allow(clippy::forget_copy, clippy::useless_transmute, deprecated)]
#[allow(clippy::forgetting_copy_types, clippy::useless_transmute, deprecated)]
unsafe {
cpp!([handle as "Interpreter*"] {
delete handle;
Expand Down Expand Up @@ -82,7 +82,7 @@ where
pub fn allocate_tensors(&mut self) -> Result<()> {
let interpreter = self.handle_mut();

#[allow(clippy::forget_copy, deprecated)]
#[allow(clippy::forgetting_copy_types, deprecated)]
let r = unsafe {
cpp!([interpreter as "Interpreter*"] -> bool as "bool" {
return interpreter->AllocateTensors() == kTfLiteOk;
Expand Down Expand Up @@ -119,7 +119,7 @@ where
pub fn print_state(&self) {
let interpreter = self.handle();

#[allow(clippy::forget_copy, clippy::useless_transmute, deprecated)]
#[allow(clippy::forgetting_copy_types, clippy::useless_transmute, deprecated)]
unsafe {
cpp!([interpreter as "Interpreter*"] {
PrintInterpreterState(interpreter);
Expand Down Expand Up @@ -153,7 +153,7 @@ where
pub fn set_num_threads(&mut self, threads: c_int) {
let interpreter = self.handle_mut();

#[allow(clippy::forget_copy, deprecated, clippy::transmute_num_to_bytes)]
#[allow(clippy::forgetting_copy_types, deprecated, clippy::transmute_num_to_bytes)]
unsafe {
cpp!([interpreter as "Interpreter*", threads as "int"] {
interpreter->SetNumThreads(threads);
Expand All @@ -167,7 +167,7 @@ where
let interpreter = self.handle();
let mut count: size_t = 0;

#[allow(clippy::forget_copy, deprecated, clippy::transmute_num_to_bytes)]
#[allow(clippy::forgetting_copy_types, deprecated, clippy::transmute_num_to_bytes)]
let ptr = unsafe {
cpp!([
interpreter as "const Interpreter*",
Expand All @@ -186,7 +186,7 @@ where
let interpreter = self.handle();
let mut count: size_t = 0;

#[allow(clippy::forget_copy, deprecated, clippy::transmute_num_to_bytes)]
#[allow(clippy::forgetting_copy_types, deprecated, clippy::transmute_num_to_bytes)]
let ptr = unsafe {
cpp!([
interpreter as "const Interpreter*",
Expand All @@ -205,7 +205,7 @@ where
let interpreter = self.handle();
let mut count: size_t = 0;

#[allow(clippy::forget_copy, deprecated, clippy::transmute_num_to_bytes)]
#[allow(clippy::forgetting_copy_types, deprecated, clippy::transmute_num_to_bytes)]
let ptr = unsafe {
cpp!([
interpreter as "const Interpreter*",
Expand All @@ -223,7 +223,7 @@ where
pub fn tensors_size(&self) -> size_t {
let interpreter = self.handle();

#[allow(clippy::forget_copy, deprecated)]
#[allow(clippy::forgetting_copy_types, deprecated)]
unsafe {
cpp!([interpreter as "const Interpreter*"] -> size_t as "size_t" {
return interpreter->tensors_size();
Expand All @@ -235,7 +235,7 @@ where
pub fn nodes_size(&self) -> size_t {
let interpreter = self.handle();

#[allow(clippy::forget_copy, deprecated)]
#[allow(clippy::forgetting_copy_types, deprecated)]
unsafe {
cpp!([interpreter as "const Interpreter*"] -> size_t as "size_t" {
return interpreter->nodes_size();
Expand All @@ -249,7 +249,7 @@ where
let interpreter = self.handle();
let mut index: TensorIndex = 0;

#[allow(clippy::forget_copy, deprecated, clippy::transmute_num_to_bytes)]
#[allow(clippy::forgetting_copy_types, deprecated, clippy::transmute_num_to_bytes)]
let result = unsafe {
cpp!([
interpreter as "Interpreter*",
Expand All @@ -274,7 +274,7 @@ where
let ptr = inputs.as_ptr();
let len = inputs.len() as size_t;

#[allow(clippy::forget_copy, deprecated, clippy::transmute_num_to_bytes)]
#[allow(clippy::forgetting_copy_types, deprecated, clippy::transmute_num_to_bytes)]
let result = unsafe {
cpp!([
interpreter as "Interpreter*",
Expand All @@ -300,7 +300,7 @@ where
let ptr = outputs.as_ptr();
let len = outputs.len() as size_t;

#[allow(clippy::forget_copy, deprecated, clippy::transmute_num_to_bytes)]
#[allow(clippy::forgetting_copy_types, deprecated, clippy::transmute_num_to_bytes)]
let result = unsafe {
cpp!([
interpreter as "Interpreter*",
Expand All @@ -326,7 +326,7 @@ where
let ptr = variables.as_ptr();
let len = variables.len() as size_t;

#[allow(clippy::forget_copy, deprecated, clippy::transmute_num_to_bytes)]
#[allow(clippy::forgetting_copy_types, deprecated, clippy::transmute_num_to_bytes)]
let result = unsafe {
cpp!([
interpreter as "Interpreter*",
Expand Down Expand Up @@ -363,7 +363,7 @@ where
let dims_ptr = dims.as_ptr();
let dims_len = dims.len() as size_t;

#[allow(clippy::forget_copy, deprecated, clippy::transmute_num_to_bytes)]
#[allow(clippy::forgetting_copy_types, deprecated, clippy::transmute_num_to_bytes)]
let result = unsafe {
cpp!([
interpreter as "Interpreter*",
Expand Down Expand Up @@ -391,7 +391,7 @@ where
fn tensor_inner(&self, tensor_index: TensorIndex) -> Option<&bindings::TfLiteTensor> {
let interpreter = self.handle();

#[allow(clippy::forget_copy, deprecated, clippy::transmute_num_to_bytes)]
#[allow(clippy::forgetting_copy_types, deprecated, clippy::transmute_num_to_bytes)]
let ptr = unsafe {
cpp!([
interpreter as "const Interpreter*",
Expand Down
1 change: 0 additions & 1 deletion src/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use crate::{Error, Result};
pub use builtin_options::{
BuiltinOptionsUnion, ConcatEmbeddingsOptionsT, ReshapeOptionsT, SqueezeOptionsT,
};
pub use builtin_options_impl::*;

#[repr(C)]
#[derive(Debug)]
Expand Down
32 changes: 16 additions & 16 deletions src/model/stl/memory_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl Deref for UniquePtr<crate::model::OperatorCodeT> {
unsafe {
let ptr = cpp!([self as "const std::unique_ptr<OperatorCodeT>*"] -> *const crate::model::OperatorCodeT as "const OperatorCodeT*" {
return self->get();
}) as *const Self::Target;
});

ptr.as_ref().unwrap()
}
Expand All @@ -39,7 +39,7 @@ impl DerefMut for UniquePtr<crate::model::OperatorCodeT> {
unsafe {
let ptr = cpp!([self as "std::unique_ptr<OperatorCodeT>*"] -> *mut crate::model::OperatorCodeT as "OperatorCodeT*" {
return self->get();
}) as *mut Self::Target;
});

ptr.as_mut().unwrap()
}
Expand Down Expand Up @@ -77,7 +77,7 @@ impl Deref for UniquePtr<crate::model::TensorT> {
unsafe {
let ptr = cpp!([self as "const std::unique_ptr<TensorT>*"] -> *const crate::model::TensorT as "const TensorT*" {
return self->get();
}) as *const Self::Target;
});

ptr.as_ref().unwrap()
}
Expand All @@ -90,7 +90,7 @@ impl DerefMut for UniquePtr<crate::model::TensorT> {
unsafe {
let ptr = cpp!([self as "std::unique_ptr<TensorT>*"] -> *mut crate::model::TensorT as "TensorT*" {
return self->get();
}) as *mut Self::Target;
});

ptr.as_mut().unwrap()
}
Expand Down Expand Up @@ -128,7 +128,7 @@ impl Deref for UniquePtr<crate::model::OperatorT> {
unsafe {
let ptr = cpp!([self as "const std::unique_ptr<OperatorT>*"] -> *const crate::model::OperatorT as "const OperatorT*" {
return self->get();
}) as *const Self::Target;
});

ptr.as_ref().unwrap()
}
Expand All @@ -141,7 +141,7 @@ impl DerefMut for UniquePtr<crate::model::OperatorT> {
unsafe {
let ptr = cpp!([self as "std::unique_ptr<OperatorT>*"] -> *mut crate::model::OperatorT as "OperatorT*" {
return self->get();
}) as *mut Self::Target;
});

ptr.as_mut().unwrap()
}
Expand Down Expand Up @@ -179,7 +179,7 @@ impl Deref for UniquePtr<crate::model::SubGraphT> {
unsafe {
let ptr = cpp!([self as "const std::unique_ptr<SubGraphT>*"] -> *const crate::model::SubGraphT as "const SubGraphT*" {
return self->get();
}) as *const Self::Target;
});

ptr.as_ref().unwrap()
}
Expand All @@ -192,7 +192,7 @@ impl DerefMut for UniquePtr<crate::model::SubGraphT> {
unsafe {
let ptr = cpp!([self as "std::unique_ptr<SubGraphT>*"] -> *mut crate::model::SubGraphT as "SubGraphT*" {
return self->get();
}) as *mut Self::Target;
});

ptr.as_mut().unwrap()
}
Expand Down Expand Up @@ -230,7 +230,7 @@ impl Deref for UniquePtr<crate::model::BufferT> {
unsafe {
let ptr = cpp!([self as "const std::unique_ptr<BufferT>*"] -> *const crate::model::BufferT as "const BufferT*" {
return self->get();
}) as *const Self::Target;
});

ptr.as_ref().unwrap()
}
Expand All @@ -243,7 +243,7 @@ impl DerefMut for UniquePtr<crate::model::BufferT> {
unsafe {
let ptr = cpp!([self as "std::unique_ptr<BufferT>*"] -> *mut crate::model::BufferT as "BufferT*" {
return self->get();
}) as *mut Self::Target;
});

ptr.as_mut().unwrap()
}
Expand Down Expand Up @@ -281,7 +281,7 @@ impl Deref for UniquePtr<crate::model::QuantizationParametersT> {
unsafe {
let ptr = cpp!([self as "const std::unique_ptr<QuantizationParametersT>*"] -> *const crate::model::QuantizationParametersT as "const QuantizationParametersT*" {
return self->get();
}) as *const Self::Target;
});

ptr.as_ref().unwrap()
}
Expand All @@ -294,7 +294,7 @@ impl DerefMut for UniquePtr<crate::model::QuantizationParametersT> {
unsafe {
let ptr = cpp!([self as "std::unique_ptr<QuantizationParametersT>*"] -> *mut crate::model::QuantizationParametersT as "QuantizationParametersT*" {
return self->get();
}) as *mut Self::Target;
});

ptr.as_mut().unwrap()
}
Expand Down Expand Up @@ -332,7 +332,7 @@ impl Deref for UniquePtr<crate::model::ModelT> {
unsafe {
let ptr = cpp!([self as "const std::unique_ptr<ModelT>*"] -> *const crate::model::ModelT as "const ModelT*" {
return self->get();
}) as *const Self::Target;
});

ptr.as_ref().unwrap()
}
Expand All @@ -345,7 +345,7 @@ impl DerefMut for UniquePtr<crate::model::ModelT> {
unsafe {
let ptr = cpp!([self as "std::unique_ptr<ModelT>*"] -> *mut crate::model::ModelT as "ModelT*" {
return self->get();
}) as *mut Self::Target;
});

ptr.as_mut().unwrap()
}
Expand Down Expand Up @@ -383,7 +383,7 @@ impl Deref for UniquePtr<crate::model::MetadataT> {
unsafe {
let ptr = cpp!([self as "const std::unique_ptr<MetadataT>*"] -> *const crate::model::MetadataT as "const MetadataT*" {
return self->get();
}) as *const Self::Target;
});

ptr.as_ref().unwrap()
}
Expand All @@ -396,7 +396,7 @@ impl DerefMut for UniquePtr<crate::model::MetadataT> {
unsafe {
let ptr = cpp!([self as "std::unique_ptr<MetadataT>*"] -> *mut crate::model::MetadataT as "MetadataT*" {
return self->get();
}) as *mut Self::Target;
});

ptr.as_mut().unwrap()
}
Expand Down

0 comments on commit cd84d68

Please sign in to comment.