-
Notifications
You must be signed in to change notification settings - Fork 415
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 readonly dictionary wrapper that removes enumerator alloc #6163
Conversation
/// <typeparam name="TKey">The type of keys in the dictionary.</typeparam> | ||
/// <typeparam name="TValue">The type of values in the dictionary.</typeparam> | ||
/// <returns>The read-only view.</returns> | ||
public static SlimReadOnlyDictionaryWrapper<TKey, TValue> AsSlimReadOnly<TKey, TValue>(this Dictionary<TKey, TValue> dictionary) |
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.
the namespace of this is a little unfortunate but probably not worth incurring a source breaking change for
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.
I could split it out into a DictionaryExtensions
, but it felt like too much. If you'd feel better with that then let me know and I'll do it.
IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator() | ||
=> dict.GetEnumerator(); |
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.
This (and the non-generic IEnumerator
one) still boxes, but I guess SlimReadOnlyListWrapper
does the same so maybe fine.
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.
This is fine, and is the way you're supposed to implement this. The only relevant case is foreach ((SlimReadOnlyDictionary<>)dict)
, if you go via the interface then you're SOL no matter what.
Add readonly dictionary wrapper that removes enumerator alloc
I find it pretty silly that this is still a thing even going into .NET 8. Hoping to use this for ppy/osu#26863.
In contrast to
SlimReadOnlyListWrapper<T>
, the only thing gained by implementing any of the other interfaces isCopyTo()
, so I've taken the liberty of not implementing every dictionary interface in existence here.