-
Notifications
You must be signed in to change notification settings - Fork 310
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
Add for_each_mut method #910
Conversation
It's unfortunate that the `.is_contiguous()` check is called twice (once explicitly, and once in `.as_slice_memory_order_mut()). The compiler rejects the following: if let Some(slc) = self.as_slice_memory_order_mut() { slc.iter_mut().for_each(f); } else { let mut v = self.view_mut(); ... } So, the chosen implementation is a safe compromise.
src/impl_methods.rs
Outdated
A: 'a, | ||
S: DataMut, | ||
{ | ||
if self.is_contiguous() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To avoid the double check, another version of the try_into_slice
internal method would be needed (try_into_memory_order_slice
or similar).
This whole method looks like it could be self.iter_mut().for_each(f)
except that the iterator is order-preserving 🙁
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To avoid the double check, another version of the
try_into_slice
internal method would be needed (try_into_memory_order_slice
or similar).
Okay, I added a try_as_slice_memory_order_mut
method to do this.
This whole method looks like it could be
self.iter_mut().for_each(f)
except that the iterator is order-preserving
Yeah, I've been thinking about the API of the iterators, .into_shape()
, and other methods where there's a choice of order. One approach would be to introduce a wrapper type for specifying the order of things, similar to your builder suggestion, but applied before calling .iter()
/.into_shape()
/etc., rather than after. So, it would be used like this: arr.order(Order::Any).iter()
for a "fast-order" iterator, or like this: arr.order(Order::Fortran).into_shape(new_shape)
for reshaping with Fortran order.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
try_as_slice_memory_order_mut is a nice solution
I just realized this method overlaps with the |
Oh, I missed that. Thanks for catching it. I've created #911 to replace this PR. |
This PR does the following:
unordered_foreach_mut
method tofor_each_mut
.for_each_mut
public.fold
andfor_each_mut
.for_each_mut
element borrows to matchfor_each
. Unfortunately, the compiler rejects the previous implementation due to the longer borrows, so a compromise involving two checks for contiguity is used to avoid the compilation error.