Skip to content

Commit e154956

Browse files
authored
Restrict type of Arrays that can be used for indexing (#259)
* Restrict type of Arrays that can be used for indexing * Fix formatting
1 parent 2a08cdd commit e154956

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

src/core/index.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::array::Array;
22
use super::defines::AfError;
33
use super::error::HANDLE_ERROR;
44
use super::seq::Seq;
5-
use super::util::{af_array, af_index_t, dim_t, HasAfEnum};
5+
use super::util::{af_array, af_index_t, dim_t, HasAfEnum, IndexableType};
66

77
use libc::{c_double, c_int, c_uint};
88
use std::default::Default;
@@ -142,7 +142,10 @@ pub trait Indexable {
142142
///
143143
/// This is used in functions [index_gen](./fn.index_gen.html) and
144144
/// [assign_gen](./fn.assign_gen.html)
145-
impl<T: HasAfEnum> Indexable for Array<T> {
145+
impl<T> Indexable for Array<T>
146+
where
147+
T: HasAfEnum + IndexableType,
148+
{
146149
fn set(&self, idxr: &mut Indexer, dim: u32, _is_batch: Option<bool>) {
147150
unsafe {
148151
let err_val = af_set_array_indexer(idxr.get(), self.get(), dim as dim_t);
@@ -155,9 +158,10 @@ impl<T: HasAfEnum> Indexable for Array<T> {
155158
///
156159
/// This is used in functions [index_gen](./fn.index_gen.html) and
157160
/// [assign_gen](./fn.assign_gen.html)
158-
impl<T: Copy> Indexable for Seq<T>
161+
impl<T> Indexable for Seq<T>
159162
where
160163
c_double: From<T>,
164+
T: Copy + IndexableType,
161165
{
162166
fn set(&self, idxr: &mut Indexer, dim: u32, is_batch: Option<bool>) {
163167
unsafe {
@@ -256,10 +260,11 @@ impl<'object> Drop for Indexer<'object> {
256260
/// println!("a(seq(1, 3, 1), span)");
257261
/// print(&sub);
258262
/// ```
259-
pub fn index<IO, T: Copy>(input: &Array<IO>, seqs: &[Seq<T>]) -> Array<IO>
263+
pub fn index<IO, T>(input: &Array<IO>, seqs: &[Seq<T>]) -> Array<IO>
260264
where
261265
c_double: From<T>,
262266
IO: HasAfEnum,
267+
T: Copy + HasAfEnum + IndexableType,
263268
{
264269
let seqs: Vec<SeqInternal> = seqs.iter().map(|s| SeqInternal::from_seq(s)).collect();
265270
unsafe {
@@ -462,7 +467,7 @@ where
462467
pub fn lookup<T, I>(input: &Array<T>, indices: &Array<I>, seq_dim: i32) -> Array<T>
463468
where
464469
T: HasAfEnum,
465-
I: HasAfEnum,
470+
I: HasAfEnum + IndexableType,
466471
{
467472
unsafe {
468473
let mut temp: af_array = std::ptr::null_mut();
@@ -504,10 +509,11 @@ where
504509
/// // 1.0 1.0 1.0
505510
/// // 2.0 2.0 2.0
506511
/// ```
507-
pub fn assign_seq<T: Copy, I>(lhs: &mut Array<I>, seqs: &[Seq<T>], rhs: &Array<I>)
512+
pub fn assign_seq<T, I>(lhs: &mut Array<I>, seqs: &[Seq<T>], rhs: &Array<I>)
508513
where
509514
c_double: From<T>,
510515
I: HasAfEnum,
516+
T: Copy + IndexableType,
511517
{
512518
let seqs: Vec<SeqInternal> = seqs.iter().map(|s| SeqInternal::from_seq(s)).collect();
513519
unsafe {
@@ -632,9 +638,10 @@ struct SeqInternal {
632638
}
633639

634640
impl SeqInternal {
635-
fn from_seq<T: Copy>(s: &Seq<T>) -> Self
641+
fn from_seq<T>(s: &Seq<T>) -> Self
636642
where
637643
c_double: From<T>,
644+
T: Copy + IndexableType,
638645
{
639646
Self {
640647
begin: From::from(s.begin()),

src/core/seq.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@ use serde::{Deserialize, Serialize};
55
use std::default::Default;
66
use std::fmt;
77

8+
use super::util::IndexableType;
9+
810
/// Sequences are used for indexing Arrays
911
#[derive(Copy, Clone, Debug, PartialEq)]
1012
#[cfg_attr(feature = "afserde", derive(Serialize, Deserialize))]
1113
#[repr(C)]
12-
pub struct Seq<T> {
14+
pub struct Seq<T: IndexableType> {
1315
begin: T,
1416
end: T,
1517
step: T,
1618
}
1719

1820
/// Default `Seq` spans all the elements along a dimension
19-
impl<T: One + Zero> Default for Seq<T> {
21+
impl<T> Default for Seq<T>
22+
where
23+
T: One + Zero + IndexableType,
24+
{
2025
fn default() -> Self {
2126
Self {
2227
begin: One::one(),
@@ -27,7 +32,10 @@ impl<T: One + Zero> Default for Seq<T> {
2732
}
2833

2934
/// Enables use of `Seq` with `{}` format in print statements
30-
impl<T: fmt::Display> fmt::Display for Seq<T> {
35+
impl<T> fmt::Display for Seq<T>
36+
where
37+
T: fmt::Display + IndexableType,
38+
{
3139
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3240
write!(
3341
f,
@@ -37,7 +45,10 @@ impl<T: fmt::Display> fmt::Display for Seq<T> {
3745
}
3846
}
3947

40-
impl<T: Copy> Seq<T> {
48+
impl<T> Seq<T>
49+
where
50+
T: Copy + IndexableType,
51+
{
4152
/// Create a `Seq` that goes from `begin` to `end` at a step size of `step`
4253
pub fn new(begin: T, end: T, step: T) -> Self {
4354
Self { begin, end, step }

src/core/util.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,3 +827,16 @@ impl Fromf64 for i16 { fn fromf64(value: f64) -> Self { value as Self }}
827827
impl Fromf64 for u8 { fn fromf64(value: f64) -> Self { value as Self }}
828828
#[rustfmt::skip]
829829
impl Fromf64 for bool { fn fromf64(value: f64) -> Self { value > 0.0 }}
830+
831+
///Trait qualifier for the type of Arrays accepted by scan operations
832+
pub trait IndexableType {}
833+
834+
impl IndexableType for f64 {}
835+
impl IndexableType for i64 {}
836+
impl IndexableType for u64 {}
837+
impl IndexableType for f32 {}
838+
impl IndexableType for i32 {}
839+
impl IndexableType for u32 {}
840+
impl IndexableType for i16 {}
841+
impl IndexableType for u16 {}
842+
impl IndexableType for u8 {}

0 commit comments

Comments
 (0)