Repository navigation
Add DeepCloneFrom for Stack, Queue, ConcurrentStack and ConcurrentQueue [minor] - #115
Merged
Merged
Conversation
…currentQueue Stack<T>, Queue<T>, ConcurrentStack<T> and ConcurrentQueue<T> don't implement ICollection<T>, so the documented clone.Items.DeepCloneFrom(Items) pattern didn't compile for get-only properties of those types, and the usual manual copy reversed stacks. Each new overload snapshots the source first (so self-clone is safe), clears the destination and refills it in source order. Fixes #114 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017vfJW4ekcBueRdQ8TsyWTk
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017vfJW4ekcBueRdQ8TsyWTk
|
This was referenced Nov 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.



The README teaches
clone.Items.DeepCloneFrom(Items)for get-only collection properties. That pattern needsICollection<T>orIDictionary<TKey, TValue>.Stack<T>,Queue<T>,ConcurrentStack<T>andConcurrentQueue<T>implement neither, so it failed with CS1929. The workaround most people reach for, pushing each item ofUndo.DeepClone(), compiles but reverses the stack.This adds four in-place
DeepCloneFromoverloads. Each one:sourcefirst, sox.DeepCloneFrom(x)leavesxunchanged (theDeepCloneFromempties the destination whendestandsourceare the same collection instance #64 self-clone safety)destand refills it with deep clones, keeping the source order: a stack still pops in the same order, and a queue still dequeues in the same orderPer-type details:
Stack<T>: pushes the snapshot in reverse.ConcurrentStack<T>: builds the reversed clones first, then callsClearandPushRange.ConcurrentQueue<T>: callsClearon netstandard2.1 and .NET. netstandard2.0 has noClear, so it drains withTryDequeueinstead.The remarks note that on the concurrent types, clear-and-refill isn't atomic.
ConcurrentBag<T>is left out on purpose. It has no defined order, and the issue lists it as optional.The README's DeepCloneFrom table now lists the new receivers.
Tests
New
InPlaceStackQueueTests:destare replaced.DeepCloneablewith get-onlyStack<T>andConcurrentQueue<T>properties uses the README pattern, and its clone has the right order.Validation:
Fixes #114
Generated by Claude Code