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 CopyTo operator #964

Closed
wants to merge 4 commits into from
Closed

Add CopyTo operator #964

wants to merge 4 commits into from

Conversation

viceroypenguin
Copy link
Contributor

This PR adds a CopyTo operator, which operates similar to Array.CopyTo or List.CopyTo. This is useful for copying data from a stream to a pre-existing array or IList<>, such as an array rented from an ArrayPool<>.

To match semantics with existing methods, this method will throw an exception if there is not enough room in the specified destination for the input data to be stored. If the consumer does not care about excess data, they can apply the Take operator before calling CopyTo to avoid the exception.

One difference between this operator and the collection methods is that this version returns an int. With the collection methods, it is easy to know pre or post how much data should be copied, because the Count/Length property is available. For IEnumerable<> this information is not as readily available, so CopyTo returns how many elements were stored in the destination.

Fixes #963

@codecov
Copy link

codecov bot commented Feb 13, 2023

Codecov Report

Merging #964 (efed50c) into master (4a9041d) will decrease coverage by 0.02%.
The diff coverage is 91.37%.

@@            Coverage Diff             @@
##           master     #964      +/-   ##
==========================================
- Coverage   92.52%   92.51%   -0.02%     
==========================================
  Files         113      114       +1     
  Lines        3440     3498      +58     
  Branches     1024     1040      +16     
==========================================
+ Hits         3183     3236      +53     
- Misses        192      197       +5     
  Partials       65       65              
Impacted Files Coverage Δ
MoreLinq/CopyTo.cs 91.37% <91.37%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

{
return CopyTo(source, array, index);
}
else

Choose a reason for hiding this comment

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

💡You don't need the else statement here, because there is no more instruction after it.

var i = index;
foreach (var el in source)
{
if (i < list.Count)

Choose a reason for hiding this comment

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

💡 Sometimes you use { } and sometimes you don't use it in if statement. Adopt a single code style.

@viceroypenguin viceroypenguin closed this by deleting the head repository Jan 19, 2024
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.

Add CopyTo Operator
2 participants