Skip to content

Commit c4b747d

Browse files
committed
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.
1 parent 2ee86b5 commit c4b747d

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/core/util.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,25 @@ impl From<u32> for ColorMap {
119119
}
120120
}
121121

122+
mod private {
123+
pub trait Sealed {}
124+
125+
impl Sealed for num::complex::Complex<f32> {}
126+
impl Sealed for num::complex::Complex<f64> {}
127+
impl Sealed for f32 {}
128+
impl Sealed for f64 {}
129+
impl Sealed for bool {}
130+
impl Sealed for u8 {}
131+
impl Sealed for i16 {}
132+
impl Sealed for u16 {}
133+
impl Sealed for half::f16 {}
134+
impl Sealed for i32 {}
135+
impl Sealed for u32 {}
136+
impl Sealed for i64 {}
137+
impl Sealed for u64 {}
138+
impl Sealed for usize {}
139+
}
140+
122141
/// Types of the data that can be generated using ArrayFire data generation functions.
123142
///
124143
/// The trait HasAfEnum has been defined internally for the following types. We strongly suggest
@@ -139,7 +158,7 @@ impl From<u32> for ColorMap {
139158
/// - i16
140159
/// - u16
141160
///
142-
pub trait HasAfEnum {
161+
pub trait HasAfEnum: private::Sealed {
143162
/// This type alias points to `Self` always.
144163
type InType: HasAfEnum;
145164
/// This type alias points to the data type used to hold real part of a
@@ -795,7 +814,7 @@ impl BitOr for MatProp {
795814
/// Trait to convert reduction's scalar output to appropriate output type
796815
///
797816
/// This is an internal trait and ideally of no use to user usecases.
798-
pub trait Fromf64 {
817+
pub trait Fromf64: private::Sealed {
799818
/// Convert to target type from a double precision value
800819
fn fromf64(value: f64) -> Self;
801820
}
@@ -837,7 +856,7 @@ impl IndexableType for u16 {}
837856
impl IndexableType for u8 {}
838857

839858
/// Trait qualifier for given type indicating computability of covariance
840-
pub trait IntegralType {}
859+
pub trait IntegralType: HasAfEnum {}
841860

842861
impl IntegralType for i64 {}
843862
impl IntegralType for u64 {}

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
//!
2020
//! Please go through our [tutorials](http://arrayfire.org/arrayfire-rust/book/index.html) book for
2121
//! more explanations on how to use ArrayFire to speedup your code.
22+
//!
23+
//! Note that the public traits on arrayfire-rust crate aren't meant to be implemented for user
24+
//! defined types. If attempted, rust compiler will throw an error.
2225
2326
#![doc(
2427
html_logo_url = "http://www.arrayfire.com/logos/arrayfire_logo_symbol.png",

0 commit comments

Comments
 (0)