Skip to content

Calling slice on a dynamic size array #501

@kali

Description

@kali

Hey folks,

I'm struggling to find a way to call slice() on a dynamic ranked array, and can find a way to make it work.

extern crate ndarray;
use ndarray::*;

fn main() {
    let array: ArrayD<f32> = Array::zeros(vec![8, 8, 8]);
    foo(&array.view());
}

fn foo<'a>(a: &'a ArrayViewD<'a, f32>) -> ArrayViewD<'a, f32> {
    let slicer: Vec<SliceOrIndex> = (0..a.ndim())
        .map(|_| SliceOrIndex::Slice {
            start: 2,
            end: Some(6),
            step: 1,
        }).collect();
    let slice = SliceInfo::new(slicer).unwrap();
    a.slice(slice)
}

The gist of the issue is the type of the AsRef<[SliceOrIndex]> in (slicer in the example).

   |
13 |     a.slice(slice)
   |             ^^^^^ expected reference, found struct `ndarray::SliceInfo`
   |
   = note: expected type `&ndarray::SliceInfo<[ndarray::SliceOrIndex], ndarray::Dim<ndarray::IxDynImpl>>`
              found type `ndarray::SliceInfo<std::vec::Vec<ndarray::SliceOrIndex>, _>`

I can't see how to make the typechecker happy here, and I can't instantiate SliceInfo<[ndarray::SliceOrIndex]> because [ndarray::SliceOrIndex] is not sized. Am I missing something obvious ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions