Skip to content

Commit 127f20c

Browse files
committedMar 20, 2016
feat/tensor: implement IntoTensorDesc for [usize; N], N=1...6
Currently dimensions of a tensor can be specified with usize, tuples, Vecs and slices: SharedTensor::new(backend, &10) SharedTensor::new(backend, &(10, 2)) SharedTensor::new(backend, &vec![10, 2]) In cases like this, vec! causes an unneeded allocation and is a bit more verbose than possible. Usize/tuple syntax looks somewhat irregular. It would be nice to be able to express tensor creation like this: SharedTensor::new(backend, &[10, 2]) But Rust doesn't autocoerce &[usize; _] into &[usize]. This patch adds explicit implementations to make this use case work. Package version is also bumped to make it possible to depend on this feature.
1 parent b32ac19 commit 127f20c

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed
 

‎Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "collenchyma"
33
description = "high-performance computation on any hardware"
4-
version = "0.0.8"
4+
version = "0.0.9"
55
authors = ["Michael Hirn <mj@autumnai.com>",
66
"Maximilian Goisser <max@autumnai.com>"]
77

‎src/tensor.rs

+14
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,20 @@ impl IntoTensorDesc for (usize, usize, usize, usize, usize, usize) {
201201
}
202202
}
203203

204+
macro_rules! impl_array_into_tensor_desc {
205+
($($N:expr)+) => {
206+
$(
207+
impl IntoTensorDesc for [usize; $N] {
208+
fn into(&self) -> TensorDesc {
209+
let slice: &[_] = self;
210+
From::from(slice)
211+
}
212+
}
213+
)+
214+
}
215+
}
216+
impl_array_into_tensor_desc!(1 2 3 4 5 6);
217+
204218
impl ITensorDesc for TensorDesc {
205219
fn rank(&self) -> usize {
206220
self.len()

0 commit comments

Comments
 (0)