From 0f468bf4188d65488415412297edc5b9b67a2c28 Mon Sep 17 00:00:00 2001 From: pradeep Date: Mon, 18 Jan 2021 18:27:21 +0530 Subject: [PATCH] Seal HasAfEnum, Fromf64 and extended traits This prevents users from adding their own implementations for traits defined by ArrayFire Rust crate to help ease using arrayfire. --- src/core/util.rs | 25 ++++++++++++++++++++++--- src/lib.rs | 3 +++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/core/util.rs b/src/core/util.rs index cbc6c708f..253695f31 100644 --- a/src/core/util.rs +++ b/src/core/util.rs @@ -119,6 +119,25 @@ impl From for ColorMap { } } +mod private { + pub trait Sealed {} + + impl Sealed for num::complex::Complex {} + impl Sealed for num::complex::Complex {} + impl Sealed for f32 {} + impl Sealed for f64 {} + impl Sealed for bool {} + impl Sealed for u8 {} + impl Sealed for i16 {} + impl Sealed for u16 {} + impl Sealed for half::f16 {} + impl Sealed for i32 {} + impl Sealed for u32 {} + impl Sealed for i64 {} + impl Sealed for u64 {} + impl Sealed for usize {} +} + /// Types of the data that can be generated using ArrayFire data generation functions. /// /// The trait HasAfEnum has been defined internally for the following types. We strongly suggest @@ -139,7 +158,7 @@ impl From for ColorMap { /// - i16 /// - u16 /// -pub trait HasAfEnum { +pub trait HasAfEnum: private::Sealed { /// This type alias points to `Self` always. type InType: HasAfEnum; /// This type alias points to the data type used to hold real part of a @@ -795,7 +814,7 @@ impl BitOr for MatProp { /// Trait to convert reduction's scalar output to appropriate output type /// /// This is an internal trait and ideally of no use to user usecases. -pub trait Fromf64 { +pub trait Fromf64: private::Sealed { /// Convert to target type from a double precision value fn fromf64(value: f64) -> Self; } @@ -837,7 +856,7 @@ impl IndexableType for u16 {} impl IndexableType for u8 {} /// Trait qualifier for given type indicating computability of covariance -pub trait IntegralType {} +pub trait IntegralType: HasAfEnum {} impl IntegralType for i64 {} impl IntegralType for u64 {} diff --git a/src/lib.rs b/src/lib.rs index 2f89a6368..2215f175c 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,6 +19,9 @@ //! //! Please go through our [tutorials](http://arrayfire.org/arrayfire-rust/book/index.html) book for //! more explanations on how to use ArrayFire to speedup your code. +//! +//! Note that the public traits on arrayfire-rust crate aren't meant to be implemented for user +//! defined types. If attempted, rust compiler will throw an error. #![doc( html_logo_url = "http://www.arrayfire.com/logos/arrayfire_logo_symbol.png",