Skip to content

Commit 454da03

Browse files
authored
Merge pull request #418 from CohenAriel/seal_tensortype
Seal TensorType trait
2 parents 62a3019 + a6e6e3e commit 454da03

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

src/lib.rs

+42-3
Original file line numberDiff line numberDiff line change
@@ -657,17 +657,56 @@ pub type Result<T> = std::result::Result<T, Status>;
657657

658658
////////////////////////
659659

660+
/// A common implementation of the sealed supertrait
661+
///
662+
/// See https://rust-lang.github.io/api-guidelines/future-proofing.html#sealed-traits-protect-against-downstream-implementations-c-sealed
663+
mod private {
664+
use crate::{BFloat16, QInt16, QInt32, QInt8, QUInt16, QUInt8};
665+
666+
pub trait Sealed {}
667+
668+
macro_rules! impl_Sealed {
669+
($($t:ty),+) => {
670+
$(impl Sealed for $t {})*
671+
}
672+
}
673+
674+
impl_Sealed!(
675+
half::f16,
676+
f32,
677+
f64,
678+
i32,
679+
u8,
680+
u16,
681+
u32,
682+
u64,
683+
i16,
684+
i8,
685+
num_complex::Complex<f32>,
686+
num_complex::Complex<f64>,
687+
i64,
688+
bool,
689+
QInt8,
690+
QUInt8,
691+
QInt16,
692+
QUInt16,
693+
QInt32,
694+
BFloat16,
695+
String
696+
);
697+
}
698+
660699
/// A Rust type that maps to a `DataType`.
661700
///
662701
/// Currently, all implementors must *not* implement Drop (or transitively contain
663702
/// anything that does) and must be bit-for-bit compatible with the corresponding C
664-
/// type. Clients must not implement this trait.
703+
/// type. Clients cannot implement this trait.
665704
///
666705
/// This trait doesn't require `num::Zero` or `num::One` because some tensor
667706
/// types (such as `bool` and `String`) don't implement them and we need to
668707
/// supply custom implementations.
669-
pub trait TensorType: Default + Clone + Display + Debug + 'static {
670-
/// Internal only; do not use outside of the tensorflow crate.
708+
pub trait TensorType: private::Sealed + Default + Clone + Display + Debug + 'static {
709+
/// Internal only; can't be used outside of the tensorflow crate.
671710
///
672711
/// Tensor representation for this type. Normally `TensorDataCRepr` for types
673712
/// that have the same representation in Rust; or `TensorDataNoCRepr` for

0 commit comments

Comments
 (0)