Skip to content

Is there a way to use index_axis on a reference to ArrayBase? #620

Closed
@LukeMathWalker

Description

@LukeMathWalker

I have been reviewing #606 and to better understand how the code works I was trying to rewrite it (it usually helps me).
The skeleton of what I was trying to do looks like this:

fn format_array<A, S, D, F>(
    view: &ArrayBase<S, D>,
    f: &mut fmt::Formatter,
    mut format: F,
    limit: Ix) -> fmt::Result
where
    F: FnMut(&A, &mut fmt::Formatter) -> fmt::Result,
    D: Dimension,
    S: Data<Elem=A>,
{
    match view.shape() {
        [] => format(view.iter().next().unwrap(), f)?,
        [_] => format_1d_array(&view, f, format, limit)?,
        shape => {
            let first_axis_length = shape[0];
            let indexes_to_be_printed: Vec<Option<usize>> = [...]
            write!(f, "[")?;
            for index in indexes_to_be_printed {
                match index {
                    Some(i) => format_array(
                        &view.index_axis(Axis(0), i), f, format, limit
                    )?,
                    None => {
                        writeln!(f, "...,")?
                    }
                }
            }
            write!(f, "]")?;
        }
    }
    Ok(())
}

If I try to compile it, I obviously get this error:

error[E0277]: the trait bound `D: dimension::remove_axis::RemoveAxis` is not satisfied
   --> src/arrayformat.rs:111:31
    |
111 |                         &view.index_axis(Axis(0), i), f, format, limit
    |                               ^^^^^^^^^^ the trait `dimension::remove_axis::RemoveAxis` is not implemented for `D`
    |
    = help: consider adding a `where D: dimension::remove_axis::RemoveAxis` bound

At this point I'd have liked to work with a dynamical dimension: get a "dynamically dimensioned" view of my ArrayBase reference and take the risk of a panic when using index_array (if I try to pass invalid parameters).
Is this even possible?
Looking at the signature of into_dyn, it seems I need to consume the array in the conversion, which is something I can't do.
Is there an alternative I can use that could work with this function skeleton?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions