diff --git a/Cargo.toml b/Cargo.toml index 8235836..af3b420 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,7 +32,7 @@ name = "ort" description = "A safe Rust wrapper for ONNX Runtime 1.20 - Optimize and accelerate machine learning inference & training" version = "2.0.0-rc.9" edition = "2021" -rust-version = "1.70" +rust-version = "1.72" license = "MIT OR Apache-2.0" repository = "https://github.com/pykeio/ort" homepage = "https://ort.pyke.io/" diff --git a/ort-sys/src/internal/mod.rs b/ort-sys/src/internal/mod.rs index 6cb610a..0fc4fe6 100644 --- a/ort-sys/src/internal/mod.rs +++ b/ort-sys/src/internal/mod.rs @@ -1,4 +1,7 @@ -use std::hash::{BuildHasher, Hasher, RandomState}; +use std::{ + collections::hash_map::RandomState, + hash::{BuildHasher, Hasher} +}; pub mod dirs; diff --git a/src/lib.rs b/src/lib.rs index 75e6008..70ba61a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,6 +24,8 @@ pub(crate) mod logging; pub mod memory; pub mod metadata; pub mod operator; +#[macro_use] +pub(crate) mod private; pub mod session; pub mod tensor; #[cfg(feature = "training")] @@ -247,26 +249,6 @@ pub(crate) fn char_p_to_string(raw: *const c_char) -> Result { Ok(c_string.to_string_lossy().to_string()) } -pub(crate) struct PrivateTraitMarker; - -macro_rules! private_trait { - () => { - #[doc(hidden)] - #[allow(private_interfaces)] - fn _private() -> crate::PrivateTraitMarker; - }; -} -macro_rules! private_impl { - () => { - #[allow(private_interfaces)] - fn _private() -> crate::PrivateTraitMarker { - crate::PrivateTraitMarker - } - }; -} -pub(crate) use private_impl; -pub(crate) use private_trait; - #[cfg(test)] mod test { use std::ffi::CString; diff --git a/src/operator/kernel.rs b/src/operator/kernel.rs index aee1fc7..fc3ed13 100644 --- a/src/operator/kernel.rs +++ b/src/operator/kernel.rs @@ -33,7 +33,6 @@ impl KernelAttributes { Self(NonNull::from(unsafe { &*info })) } - #[allow(private_bounds)] pub fn get<'s, T: GetKernelAttribute<'s>>(&'s self, name: impl AsRef) -> Option { let name = CString::new(name.as_ref()).ok()?; T::get_from(self.0.as_ptr(), name.as_ptr()) @@ -106,7 +105,7 @@ impl AsPointer for KernelAttributes { } } -pub(crate) trait GetKernelAttribute<'s> { +pub trait GetKernelAttribute<'s> { fn get_from(info: *mut ort_sys::OrtKernelInfo, name: *const ort_sys::c_char) -> Option where Self: Sized; diff --git a/src/private.rs b/src/private.rs new file mode 100644 index 0000000..9dd6365 --- /dev/null +++ b/src/private.rs @@ -0,0 +1,15 @@ +pub struct PrivateTraitMarker; + +macro_rules! private_trait { + () => { + #[doc(hidden)] + fn _private() -> crate::private::PrivateTraitMarker; + }; +} +macro_rules! private_impl { + () => { + fn _private() -> crate::private::PrivateTraitMarker { + crate::private::PrivateTraitMarker + } + }; +} diff --git a/src/tensor/types.rs b/src/tensor/types.rs index 8b73ece..aa02a91 100644 --- a/src/tensor/types.rs +++ b/src/tensor/types.rs @@ -116,13 +116,13 @@ pub trait IntoTensorElementType { /// Returns the ONNX tensor element data type corresponding to the given Rust type. fn into_tensor_element_type() -> TensorElementType; - crate::private_trait!(); + private_trait!(); } /// A superset of [`IntoTensorElementType`] that represents traits whose underlying memory is identical between Rust and /// C++ (i.e., every type except `String`). pub trait PrimitiveTensorElementType: IntoTensorElementType { - crate::private_trait!(); + private_trait!(); } macro_rules! impl_type_trait { @@ -132,11 +132,11 @@ macro_rules! impl_type_trait { TensorElementType::$variant } - crate::private_impl!(); + private_impl!(); } impl PrimitiveTensorElementType for $type_ { - crate::private_impl!(); + private_impl!(); } }; } @@ -164,7 +164,7 @@ impl IntoTensorElementType for String { TensorElementType::String } - crate::private_impl!(); + private_impl!(); } /// Adapter for common Rust string types to ONNX strings. diff --git a/src/util.rs b/src/util.rs index bfa11d9..ccf8c27 100644 --- a/src/util.rs +++ b/src/util.rs @@ -17,7 +17,7 @@ pub fn path_to_os_char(path: impl AsRef) -> OsCharArray { let model_path: Vec = model_path.encode_wide().chain(std::iter::once(0)).collect(); #[cfg(not(target_family = "windows"))] let model_path: Vec = model_path - .as_encoded_bytes() + .as_bytes() .iter() .chain(std::iter::once(&b'\0')) .map(|b| *b as c_char) diff --git a/src/value/impl_map.rs b/src/value/impl_map.rs index 930f5e6..6f132c4 100644 --- a/src/value/impl_map.rs +++ b/src/value/impl_map.rs @@ -20,7 +20,7 @@ use crate::{ }; pub trait MapValueTypeMarker: ValueTypeMarker { - crate::private_trait!(); + private_trait!(); } #[derive(Debug)] @@ -30,10 +30,10 @@ impl ValueTypeMarker for DynMapValueType { "DynMap".to_string() } - crate::private_impl!(); + private_impl!(); } impl MapValueTypeMarker for DynMapValueType { - crate::private_impl!(); + private_impl!(); } impl DowncastableTarget for DynMapValueType { @@ -41,7 +41,7 @@ impl DowncastableTarget for DynMapValueType { matches!(dtype, ValueType::Map { .. }) } - crate::private_impl!(); + private_impl!(); } #[derive(Debug)] @@ -51,10 +51,10 @@ impl", K::into_tensor_element_type(), V::into_tensor_element_type()) } - crate::private_impl!(); + private_impl!(); } impl MapValueTypeMarker for MapValueType { - crate::private_impl!(); + private_impl!(); } impl DowncastableTarget for MapValueType { @@ -65,7 +65,7 @@ impl; diff --git a/src/value/impl_sequence.rs b/src/value/impl_sequence.rs index c3aed0d..16e886d 100644 --- a/src/value/impl_sequence.rs +++ b/src/value/impl_sequence.rs @@ -14,7 +14,7 @@ use crate::{ }; pub trait SequenceValueTypeMarker: ValueTypeMarker { - crate::private_trait!(); + private_trait!(); } #[derive(Debug)] @@ -24,10 +24,10 @@ impl ValueTypeMarker for DynSequenceValueType { "DynSequence".to_string() } - crate::private_impl!(); + private_impl!(); } impl SequenceValueTypeMarker for DynSequenceValueType { - crate::private_impl!(); + private_impl!(); } impl DowncastableTarget for DynSequenceValueType { @@ -35,7 +35,7 @@ impl DowncastableTarget for DynSequenceValueType { matches!(dtype, ValueType::Sequence { .. }) } - crate::private_impl!(); + private_impl!(); } #[derive(Debug)] @@ -45,10 +45,10 @@ impl ValueTypeMarker f format!("Sequence<{}>", T::format()) } - crate::private_impl!(); + private_impl!(); } impl SequenceValueTypeMarker for SequenceValueType { - crate::private_impl!(); + private_impl!(); } impl DowncastableTarget for SequenceValueType { @@ -59,7 +59,7 @@ impl DowncastableTarge } } - crate::private_impl!(); + private_impl!(); } pub type DynSequence = Value; diff --git a/src/value/impl_tensor/create.rs b/src/value/impl_tensor/create.rs index 0d09914..4656f44 100644 --- a/src/value/impl_tensor/create.rs +++ b/src/value/impl_tensor/create.rs @@ -3,6 +3,7 @@ use std::{ ffi, fmt::Debug, marker::PhantomData, + mem::size_of, ptr::{self, NonNull}, sync::Arc }; @@ -438,20 +439,20 @@ pub trait TensorArrayData { #[allow(clippy::type_complexity)] fn ref_parts(&self) -> Result<(Vec, &[I], Option>)>; - crate::private_trait!(); + private_trait!(); } pub trait TensorArrayDataMut: TensorArrayData { #[allow(clippy::type_complexity)] fn ref_parts_mut(&mut self) -> Result<(Vec, &mut [I], Option>)>; - crate::private_trait!(); + private_trait!(); } pub trait OwnedTensorArrayData { fn into_parts(self) -> Result>; - crate::private_trait!(); + private_trait!(); } pub struct TensorArrayDataParts { @@ -531,7 +532,7 @@ impl TensorArrayData for &CowArra Ok((shape, data, None)) } - crate::private_impl!(); + private_impl!(); } #[cfg(feature = "ndarray")] @@ -545,7 +546,7 @@ impl TensorArrayData for ArcArray Ok((shape, data, Some(Box::new(self.clone())))) } - crate::private_impl!(); + private_impl!(); } #[cfg(feature = "ndarray")] @@ -559,7 +560,7 @@ impl TensorArrayData for &Array TensorArrayData for &mut Arr Ok((shape, data, None)) } - crate::private_impl!(); + private_impl!(); } #[cfg(feature = "ndarray")] @@ -598,7 +599,7 @@ impl OwnedTensorArrayData for Arr } } - crate::private_impl!(); + private_impl!(); } #[cfg(feature = "ndarray")] @@ -612,7 +613,7 @@ impl TensorArrayData for ArrayVie Ok((shape, data, None)) } - crate::private_impl!(); + private_impl!(); } #[cfg(feature = "ndarray")] @@ -626,7 +627,7 @@ impl TensorArrayData for ArrayVie Ok((shape, data, None)) } - crate::private_impl!(); + private_impl!(); } #[cfg(feature = "ndarray")] @@ -640,7 +641,7 @@ impl TensorArrayDataMut for Array Ok((shape, data, None)) } - crate::private_impl!(); + private_impl!(); } #[cfg(feature = "ndarray")] @@ -654,7 +655,7 @@ impl TensorArrayDataMut for &mut Ok((shape, data, None)) } - crate::private_impl!(); + private_impl!(); } impl TensorArrayData for (D, &[T]) { @@ -663,7 +664,7 @@ impl TensorArrayData for (D, &[T]) { Ok((shape, self.1, None)) } - crate::private_impl!(); + private_impl!(); } impl TensorArrayData for (D, &mut [T]) { @@ -672,7 +673,7 @@ impl TensorArrayData for (D, &mut [T]) { Ok((shape, self.1, None)) } - crate::private_impl!(); + private_impl!(); } impl TensorArrayDataMut for (D, &mut [T]) { @@ -681,7 +682,7 @@ impl TensorArrayDataMut for (D, &mut [T] Ok((shape, self.1, None)) } - crate::private_impl!(); + private_impl!(); } impl OwnedTensorArrayData for (D, Vec) { @@ -697,7 +698,7 @@ impl OwnedTensorArrayData for (D, Vec }) } - crate::private_impl!(); + private_impl!(); } impl OwnedTensorArrayData for (D, Box<[T]>) { @@ -713,7 +714,7 @@ impl OwnedTensorArrayData for (D, Box<[T }) } - crate::private_impl!(); + private_impl!(); } impl TensorArrayData for (D, Arc<[T]>) { @@ -723,7 +724,7 @@ impl TensorArrayData for (D, Arc<[T]>) { Ok((shape, data, Some(Box::new(self.1.clone())))) } - crate::private_impl!(); + private_impl!(); } impl TensorArrayData for (D, Arc>) { @@ -733,5 +734,5 @@ impl TensorArrayData for (D, Arc Value { #[cfg(feature = "ndarray")] #[cfg_attr(docsrs, doc(cfg(feature = "ndarray")))] pub fn try_extract_tensor(&self) -> Result> { - use crate::AsPointer; - match self.dtype() { ValueType::Tensor { ty, dimensions, .. } => { let mem = self.memory_info(); diff --git a/src/value/impl_tensor/mod.rs b/src/value/impl_tensor/mod.rs index c6509c4..9a27707 100644 --- a/src/value/impl_tensor/mod.rs +++ b/src/value/impl_tensor/mod.rs @@ -13,7 +13,7 @@ use super::{DowncastableTarget, DynValue, Value, ValueRef, ValueRefMut, ValueTyp use crate::{AsPointer, error::Result, memory::MemoryInfo, ortsys, tensor::IntoTensorElementType}; pub trait TensorValueTypeMarker: ValueTypeMarker { - crate::private_trait!(); + private_trait!(); } #[derive(Debug)] @@ -23,10 +23,10 @@ impl ValueTypeMarker for DynTensorValueType { "DynTensor".to_string() } - crate::private_impl!(); + private_impl!(); } impl TensorValueTypeMarker for DynTensorValueType { - crate::private_impl!(); + private_impl!(); } #[derive(Debug)] @@ -36,10 +36,10 @@ impl ValueTypeMarker for TensorValueType { format!("Tensor<{}>", T::into_tensor_element_type()) } - crate::private_impl!(); + private_impl!(); } impl TensorValueTypeMarker for TensorValueType { - crate::private_impl!(); + private_impl!(); } /// A tensor [`Value`] whose data type is unknown. @@ -61,7 +61,7 @@ impl DowncastableTarget for DynTensorValueType { matches!(dtype, ValueType::Tensor { .. }) } - crate::private_impl!(); + private_impl!(); } impl Value { @@ -215,7 +215,7 @@ impl DowncastableTarget for TensorValueType } } - crate::private_impl!(); + private_impl!(); } impl From>> for DynValue { diff --git a/src/value/mod.rs b/src/value/mod.rs index a865875..b4e720c 100644 --- a/src/value/mod.rs +++ b/src/value/mod.rs @@ -248,14 +248,14 @@ pub trait ValueTypeMarker { #[doc(hidden)] fn format() -> String; - crate::private_trait!(); + private_trait!(); } /// Represents a type that a [`DynValue`] can be downcast to. pub trait DowncastableTarget: ValueTypeMarker { fn can_downcast(dtype: &ValueType) -> bool; - crate::private_trait!(); + private_trait!(); } // this implementation is used in case we want to extract `DynValue`s from a [`Sequence`]; see `try_extract_sequence` @@ -264,7 +264,7 @@ impl DowncastableTarget for DynValueTypeMarker { true } - crate::private_impl!(); + private_impl!(); } /// The dynamic type marker, used for values which can be of any type. @@ -275,16 +275,16 @@ impl ValueTypeMarker for DynValueTypeMarker { "DynValue".to_string() } - crate::private_impl!(); + private_impl!(); } impl MapValueTypeMarker for DynValueTypeMarker { - crate::private_impl!(); + private_impl!(); } impl SequenceValueTypeMarker for DynValueTypeMarker { - crate::private_impl!(); + private_impl!(); } impl TensorValueTypeMarker for DynValueTypeMarker { - crate::private_impl!(); + private_impl!(); } unsafe impl Send for Value {}