Skip to content

Implement trait on Owned and View arrays #528

Closed
@davenza

Description

@davenza

Hi,

this could be more of a Rust question than ndarray's question, but I submit this issue because maybe some of you have solved this yet.

I have an array inside of a struct Wrapper. The array could be owned or view. Now, I am trying to implement a given operation that requires using into_subview() for both owned and view Wrapper structs. My take on this was:

use ndarray::{Array, ArrayBase, Axis, IxDyn, OwnedRepr, ViewRepr, Data};
use std::mem;

pub struct Wrapper<S>
    where S: Data
{
    a: ArrayBase<S, IxDyn>
}

pub trait Operation {
    fn op(&mut self, axis: Axis, index: usize);
}

impl<V> Operation for Wrapper<OwnedRepr<V>>
    where OwnedRepr<V> : Data<Elem=V>,
{
    fn op(&mut self, axis: Axis, index: usize) {
        let mut owned = mem::replace(&mut self.a,
                                     Array::from_vec(Vec::new()).into_dyn());
        owned = owned.into_subview(axis, index);
        mem::replace(&mut self.a, owned);
    }
}

impl<'a, V> Operation for Wrapper<ViewRepr<&'a V>>
    where ViewRepr<&'a V> : Data<Elem=V>,
{
    fn op(&mut self, axis: Axis, index: usize) {
        let empty_array = Array::from_vec(Vec::new()).into_dyn();
        let empty_view = empty_array.view();
        
        let mut owned = mem::replace(&mut self.a,
                                     empty_view);
        owned = owned.into_subview(axis, index);
        mem::replace(&mut self.a, owned);
    }
}

This code does not compile, it errors in the view implementation:

error[E0597]: empty_array does not live long enough                                                                                                                                                              
  --> src/mwe.rs:30:26
                                                                                                                                                                         
   |                                                                                                                                                                                                               
30 |         let empty_view = empty_array.view();                                                                                                                                                                  
   |                          ^^^^^^^^^^^ borrowed value does not live long enough                                                                                                                                 
...                                                                                                                                                                                                                
36 |     }                                                                                                                                                                                                         
   |     - borrowed value only lives until here                                                                                                                                                                    
   |                                                                                                                                                                                                               
note: borrowed value must be valid for the lifetime 'a as defined on the impl at 25:6...                                                                                                                           
  --> src/mwe.rs:25:6                                                                                                                                                                                  
   |                                                                                                                                                                                                               
25 | impl<'a, V> Operation for Wrapper<ViewRepr<&'a V>>                                                                                                                                                            
   |      ^^                                                                                                                                                                                                       
                                                                                                                                                                                                                   
error: aborting due to previous error                                                                                                                                                                              
                                                                                                                                                                                                                   
For more information about this error, try `rustc --explain E0597`. 

At first, I thought that the mem::replace() in the view implementation was bounding the lifetime of the empty_view. However, it fails the same with the mem::replace() code commented:

impl<'a, V> Operation for Wrapper<ViewRepr<&'a V>>
    where ViewRepr<&'a V> : Data<Elem=V>,
{
    fn op(&mut self, axis: Axis, index: usize) {
        let empty_array = Array::from_vec(Vec::new()).into_dyn();
        let empty_view = empty_array.view();

//        let mut owned = mem::replace(&mut self.a,
//                                     empty_view);
//        owned = owned.into_subview(axis, index);
//        mem::replace(&mut self.a, owned);
    }
}

How would you implement this (if possible)? I don't get why it is failing.

Thanks.

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