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

Add new method .assign_to() #947

Merged
merged 1 commit into from Mar 18, 2021
Merged

Add new method .assign_to() #947

merged 1 commit into from Mar 18, 2021

Conversation

bluss
Copy link
Member

@bluss bluss commented Mar 17, 2021

This method does the same job as Zip::from(a).map_assign_into(f, b) with
cloning as the function f.

Comparison with existing assign:

  • Reverse argument order: source, destination
  • The new method doesn't broadcast.
  • The new method is generic over AssignElem (assign into &Cell, &mut
    ManuallyUninit etc).

@bluss
Copy link
Member Author

bluss commented Mar 17, 2021

Unsure if these methods pull their weight (they add nothing new, but are expressive because they are easy to use). Their properties contrast with the existing .assign().

Best we can do for existing .assign is to generalize it, with some loss of type inference, so that it can assign to both "T" and "ManuallyUninit" arrays. The assign_into solution additionally handles "Cell" arrays.

@bluss bluss force-pushed the assign-into branch 2 times, most recently from 98970f5 to b22e2b1 Compare March 17, 2021 22:10
@bluss bluss changed the title Add new methods .assign_into() and .map_assign_into Add new methods .assign_into() and .map_assign_into() Mar 17, 2021
/// [`AssignElem`] determines how elements are assigned.
///
/// **Panics** if shapes disagree.
pub fn map_assign_into<'a, F, B, P>(&'a self, f: F, to: P)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe skip exposing this as a public method, it's not as used.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm not sure this method is necessary. The parameters are also a little awkward, because while I think the order f: F, to: P better fits the name of the method, it's more common for Rust methods to put the closure as the last parameter since closures are often formatted as multiple lines. (Consider e.g. Result::map_or, which IMO would better fit the name as map_or<F, U>(self, f: F, default: U) but is actually map_or<U, F>(self, default: U, f: F) in the standard library.)

Copy link
Member

@jturner314 jturner314 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do think it's worth having a simple way to assign to arrays of MaybeUninit, although as you pointed out, this could be handled with the existing assign. I'm not opposed to an assign_to method, though, because it's a simple addition, and the swapped parameter order could be useful in some cases. The fact that it works with arrays of Cell/MathCell is a bonus, although I'm unclear on the typical use cases for arrays of Cell/MathCell.

/// [`AssignElem`] determines how elements are assigned.
///
/// **Panics** if shapes disagree.
pub fn assign_into<P>(&self, to: P)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a better name would be assign_to, because this method takes &self rather than self.

Copy link
Member Author

@bluss bluss Mar 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I guess it's better to stick to that convention, even though it's not always clear cut. Consuming a producer seems to be a very "into" situation, but in most cases, it will just be a mutable reference to an array or view.

/// [`AssignElem`] determines how elements are assigned.
///
/// **Panics** if shapes disagree.
pub fn map_assign_into<'a, F, B, P>(&'a self, f: F, to: P)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm not sure this method is necessary. The parameters are also a little awkward, because while I think the order f: F, to: P better fits the name of the method, it's more common for Rust methods to put the closure as the last parameter since closures are often formatted as multiple lines. (Consider e.g. Result::map_or, which IMO would better fit the name as map_or<F, U>(self, f: F, default: U) but is actually map_or<U, F>(self, default: U, f: F) in the standard library.)

This method does the same job as Zip::from(a).map_assign_into(f, b) with
cloning as the function f.

Comparison with existing assign:

- Reverse argument order: source, destination
- The new method doesn't broadcast.
- The new method is generic over AssignElem (assign into &Cell, &mut
ManuallyUninit etc).
@bluss bluss changed the title Add new methods .assign_into() and .map_assign_into() Add new method .assign_to() Mar 18, 2021
@bluss bluss merged commit 0970fc5 into master Mar 18, 2021
@bluss bluss deleted the assign-into branch March 18, 2021 17:21
@bluss bluss added this to the 0.15.0 milestone Mar 18, 2021
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.

None yet

2 participants