Add .reborrow() methods to array views by bluss · Pull Request #412 · rust-ndarray/ndarray · GitHub
Skip to content

Add .reborrow() methods to array views#412

Merged
bluss merged 1 commit into
masterfrom
reborrow-view
Jan 26, 2018
Merged

Add .reborrow() methods to array views#412
bluss merged 1 commit into
masterfrom
reborrow-view

Conversation

@bluss

@bluss bluss commented Jan 25, 2018

Copy link
Copy Markdown
Member

ArrayView<'a, T, D> is invariant in parameters T and 'a, because
ArrayBase<S, D> is invariant in the parameter S because the array
base is using the projection <S as Data>::Elem in the struct
definition.

This means that the equivalent of the &'a T -> &'b T conversion (valid
whenever 'a: 'b is not naturally available for array views; we can
provide it manually, with a method.

`ArrayView<'a, T, D>` is invariant in parameters `T` and `'a`, because
`ArrayBase<S, D>` is invariant in the parameter `S` because the array
base is using the projection `<S as Data>::Elem` in the struct
definition.

This means that the equivalent of the `&'a T -> &'b T` conversion (valid
whenever `'a: 'b` is not naturally available for array views; we can
provide it manually, with a method.
@bluss

bluss commented Jan 25, 2018

Copy link
Copy Markdown
Member Author

@jturner314

Copy link
Copy Markdown
Member

"reborrow" seems like a good name to me. You are effectively borrowing the data again; the 'a: 'b constraint is just necessary to ensure that the lifetime of the new borrow is valid. You could call it "shrink_lifetime", "reduce_lifetime", or "shorten_life", but I like "reborrow" better.

For those not on IRC, this was the case that caused me to encounter the issue. I did find that calling .view() on state (Foo { mean: state.view(), covariance: view }) was a workaround for this particular case, but I'm not sure that workaround would work for all cases.

@Robbepop

Copy link
Copy Markdown

I was really in need of this API some time ago. Thanks for introducing it! :)

@bluss bluss merged commit 8218867 into master Jan 26, 2018
@bluss

bluss commented Jan 26, 2018

Copy link
Copy Markdown
Member Author

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

Successfully merging this pull request may close these issues.

3 participants