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

impl Borrow<&ArrayBase<ViewRepr<&T>, _>> for ArrayBase<OwnedRepr<T>, _> #878

Closed
garro95 opened this issue Jan 1, 2021 · 4 comments
Closed

Comments

@garro95
Copy link

garro95 commented Jan 1, 2021

If possible, the trait specified in the title would make it easier to store owned arrays in a (Hash)Map and retrieve them without the need to build a new owned array (performing allocations).

@garro95
Copy link
Author

garro95 commented Jan 1, 2021

Probably the implementation of borrow would be a simple self.slice(s![..]) with .. repeated for every axes

@jturner314
Copy link
Member

Unfortunately, this is not possible in current Rust. (Who would own the ArrayView that borrow would return a reference to?) For the same reason, we can't return an &ArrayView from our implementation of Index.

To do something like this, we need the language to support custom dynamically-sized types, i.e. custom "fat pointers." (The shape/strides/etc. would be stored in the fat pointer itself.) It's worth noting that current proposals, e.g. rust-lang/rfcs#2594, would make it possible to return &ArrayView for constant-ndim cases (e.g. Ix1, Ix2, etc.), but they still wouldn't allow returning an &ArrayView for the IxDyn case.

@bluss
Copy link
Member

bluss commented Jan 2, 2021

The crate indexmap implements a more mechanism for this (instead of Borrow) - its Equivalence trait should make it possible to implement this, if you also use indexmap's hash table implementation.

@jturner314
Copy link
Member

jturner314 commented Jan 3, 2021

I realized that my initial response was misleading. It's not possible to implement Borrow<ArrayView<'a, A, D>> for ArrayBase<S, D> where S: Data since it's not possible to correctly express the lifetime 'a. However, it is possible for us to introduce a new "storage" type RefRepr such that &'a ArrayBase<RefRepr<A>, D> is analogous to ArrayView<'a, A, D>, and we can implement Borrow<ArrayBase<RefRepr<A>, D>> and Deref<Target = ArrayBase<RefRepr<A>, D>> for ArrayBase<S, D> where S: Data. I'll write up my thoughts in a separate issue.

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

3 participants