diff --git a/src/core/index.rs b/src/core/index.rs index a67d86d7a..641e9735b 100644 --- a/src/core/index.rs +++ b/src/core/index.rs @@ -2,7 +2,7 @@ use super::array::Array; use super::defines::AfError; use super::error::HANDLE_ERROR; use super::seq::Seq; -use super::util::{af_array, af_index_t, dim_t, HasAfEnum}; +use super::util::{af_array, af_index_t, dim_t, HasAfEnum, IndexableType}; use libc::{c_double, c_int, c_uint}; use std::default::Default; @@ -142,7 +142,10 @@ pub trait Indexable { /// /// This is used in functions [index_gen](./fn.index_gen.html) and /// [assign_gen](./fn.assign_gen.html) -impl Indexable for Array { +impl Indexable for Array +where + T: HasAfEnum + IndexableType, +{ fn set(&self, idxr: &mut Indexer, dim: u32, _is_batch: Option) { unsafe { let err_val = af_set_array_indexer(idxr.get(), self.get(), dim as dim_t); @@ -155,9 +158,10 @@ impl Indexable for Array { /// /// This is used in functions [index_gen](./fn.index_gen.html) and /// [assign_gen](./fn.assign_gen.html) -impl Indexable for Seq +impl Indexable for Seq where c_double: From, + T: Copy + IndexableType, { fn set(&self, idxr: &mut Indexer, dim: u32, is_batch: Option) { unsafe { @@ -256,10 +260,11 @@ impl<'object> Drop for Indexer<'object> { /// println!("a(seq(1, 3, 1), span)"); /// print(&sub); /// ``` -pub fn index(input: &Array, seqs: &[Seq]) -> Array +pub fn index(input: &Array, seqs: &[Seq]) -> Array where c_double: From, IO: HasAfEnum, + T: Copy + HasAfEnum + IndexableType, { let seqs: Vec = seqs.iter().map(|s| SeqInternal::from_seq(s)).collect(); unsafe { @@ -462,7 +467,7 @@ where pub fn lookup(input: &Array, indices: &Array, seq_dim: i32) -> Array where T: HasAfEnum, - I: HasAfEnum, + I: HasAfEnum + IndexableType, { unsafe { let mut temp: af_array = std::ptr::null_mut(); @@ -504,10 +509,11 @@ where /// // 1.0 1.0 1.0 /// // 2.0 2.0 2.0 /// ``` -pub fn assign_seq(lhs: &mut Array, seqs: &[Seq], rhs: &Array) +pub fn assign_seq(lhs: &mut Array, seqs: &[Seq], rhs: &Array) where c_double: From, I: HasAfEnum, + T: Copy + IndexableType, { let seqs: Vec = seqs.iter().map(|s| SeqInternal::from_seq(s)).collect(); unsafe { @@ -632,9 +638,10 @@ struct SeqInternal { } impl SeqInternal { - fn from_seq(s: &Seq) -> Self + fn from_seq(s: &Seq) -> Self where c_double: From, + T: Copy + IndexableType, { Self { begin: From::from(s.begin()), diff --git a/src/core/seq.rs b/src/core/seq.rs index 06c6c2eb7..4d6ec98c8 100644 --- a/src/core/seq.rs +++ b/src/core/seq.rs @@ -5,18 +5,23 @@ use serde::{Deserialize, Serialize}; use std::default::Default; use std::fmt; +use super::util::IndexableType; + /// Sequences are used for indexing Arrays #[derive(Copy, Clone, Debug, PartialEq)] #[cfg_attr(feature = "afserde", derive(Serialize, Deserialize))] #[repr(C)] -pub struct Seq { +pub struct Seq { begin: T, end: T, step: T, } /// Default `Seq` spans all the elements along a dimension -impl Default for Seq { +impl Default for Seq +where + T: One + Zero + IndexableType, +{ fn default() -> Self { Self { begin: One::one(), @@ -27,7 +32,10 @@ impl Default for Seq { } /// Enables use of `Seq` with `{}` format in print statements -impl fmt::Display for Seq { +impl fmt::Display for Seq +where + T: fmt::Display + IndexableType, +{ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!( f, @@ -37,7 +45,10 @@ impl fmt::Display for Seq { } } -impl Seq { +impl Seq +where + T: Copy + IndexableType, +{ /// Create a `Seq` that goes from `begin` to `end` at a step size of `step` pub fn new(begin: T, end: T, step: T) -> Self { Self { begin, end, step } diff --git a/src/core/util.rs b/src/core/util.rs index 2eff73f64..118948c42 100644 --- a/src/core/util.rs +++ b/src/core/util.rs @@ -827,3 +827,16 @@ impl Fromf64 for i16 { fn fromf64(value: f64) -> Self { value as Self }} impl Fromf64 for u8 { fn fromf64(value: f64) -> Self { value as Self }} #[rustfmt::skip] impl Fromf64 for bool { fn fromf64(value: f64) -> Self { value > 0.0 }} + +///Trait qualifier for the type of Arrays accepted by scan operations +pub trait IndexableType {} + +impl IndexableType for f64 {} +impl IndexableType for i64 {} +impl IndexableType for u64 {} +impl IndexableType for f32 {} +impl IndexableType for i32 {} +impl IndexableType for u32 {} +impl IndexableType for i16 {} +impl IndexableType for u16 {} +impl IndexableType for u8 {}