Refactor storage types for views and mutable views #148
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Previously tensor views used slices as their storage type (the
TensorBase::data
field).When tensor operations create logically separate mutable views whose underlying storage ranges overlap (for example, a mutable iterator over the column axis of a matrix), this lead to violations of Rust's ownership rules. Miri's stacked borrows check complained about this.
This PR refactors tensor storage to address this:
TensorView
andTensorViewMut
now use custom types (ViewData
,ViewMutData
) instead of slices for their storage. These types have the same representation, but don't promise there is no overlap between a mutable storage and other storage instances. Instead the caller has to ensure that.TensorBase
has changed fromTensorBase<T, S: AsRef<[T]>, L: MutLayout>
toTensorBase<S: Storage, L: MutLayout>
whereStorage
is a trait that is implemented byVec<T>
,Cow<[T]>
,ViewData
andViewMutData
. TheStorage
trait provides access to the underlying raw data. It has methods for getting a reference to the element at a given offset, but unlikeAsRef
these are unsafe, since the caller has to ensure that no mutable references can exist. The type of element in aTensorBase
is now accessed viaS::Elem
TensorBase::{non_contiguous_data, data_mut_ptr}
have been replaced withTensorBase::{storage, storage_mut}
for low-level code that needs to get at the underlying data pointer.non_contiguous_data
was unsound due to the same issues as storage for mutable views.make miri
has been updated to re-enable the stacked borrows check.