Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slicing an ArrayView restricts lifetime #988

Closed
cassiersg opened this issue Apr 28, 2021 · 5 comments
Closed

Slicing an ArrayView restricts lifetime #988

cassiersg opened this issue Apr 28, 2021 · 5 comments

Comments

@cassiersg
Copy link
Contributor

The following doesn't work, due to the signature of ArrayBase::slice.

fn foo<'a>(x: ArrayView1<'a, i32>) -> ArrayView<'a, i32> {
    x.slice(s![1..])
}

It would be nice to have a method dedicated to this on ArrayView, similarly to ArrayView::to_slice, (which is a taylor-adapted version of ArrayBase::as_slice).

@jturner314
Copy link
Member

The slice_move method on ArrayBase can be used for this purpose. If you don't want to lose access to the original view, note that const-ndim ArrayViews implement Copy and Clone, and dynamic-ndim ArrayViews implement Clone. However, I'm not opposed to adding a special method to ArrayView if we can come up with a good name for it, since there is a potential efficiency gain from a specialized implementation instead of .clone().slice_move().

@cassiersg
Copy link
Contributor Author

Thank you, I missed slice_move. I'll go with that. I guess overhead of .clone().slice_move() will be negligible except in rather rare situations.

@jturner314
Copy link
Member

jturner314 commented Apr 28, 2021

Yeah, for ArrayView1 especially, you don't even need the .clone() since it implements Copy, and the overhead relative to a specialized method should be zero or negligible.

@cassiersg
Copy link
Contributor Author

I opened #989 which would have helped me not miss the .clone().slice_move() solution. Re-perf: indeed, and also for any dimensionality as long as you deal with large matrices (and actually do something with them).

Leaving up to you to close this or not.

@jturner314
Copy link
Member

Okay, I'll go ahead and close this.

To anyone coming across this issue: If .clone().slice_move() isn't good enough, and you need a method

impl<'a, A, D> ArrayView<'a, A, D> {
    fn foo<I>(&self, info: I) -> ArrayView<'a, A, I::OutDim> 
    where
        I: SliceArg<D>,
    { ... }
}

and have a good name for it, please feel free to reopen this issue or create a new one referencing it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants