-
Notifications
You must be signed in to change notification settings - Fork 459
fix!: added plainly-callable Add() method to NetworkSet [MTT-1005] #1022
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -331,29 +331,6 @@ IEnumerator IEnumerable.GetEnumerator() | |
| return m_Set.GetEnumerator(); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| void ICollection<T>.Add(T item) | ||
| { | ||
| EnsureInitialized(); | ||
|
|
||
| if (m_NetworkBehaviour.NetworkManager.IsServer) | ||
| { | ||
| m_Set.Add(item); | ||
| } | ||
|
|
||
| var setEvent = new NetworkSetEvent<T>() | ||
| { | ||
| Type = NetworkSetEvent<T>.EventType.Add, | ||
| Value = item | ||
| }; | ||
| m_DirtyEvents.Add(setEvent); | ||
|
|
||
| if (m_NetworkBehaviour.NetworkManager.IsServer && OnSetChanged != null) | ||
| { | ||
| OnSetChanged(setEvent); | ||
| } | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public void ExceptWith(IEnumerable<T> other) | ||
| { | ||
|
|
@@ -478,8 +455,7 @@ public void UnionWith(IEnumerable<T> other) | |
| } | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| bool ISet<T>.Add(T item) | ||
| public void Add(T item) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: You don't need 3 methods for this. Just put the implementation in the ISet and call it from the ICollection.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the wackiness here is that we have But then if a user wants to call either one, they have to do a non-intuitive cast, e.g. I'm thinking about using a less grand interface than ISet / ICollection anyway vs. today where we promise to do everything those interfaces do, so that if we use native container underlying storage we don't have to make up for make of these calls that the native types don't support.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
public bool Add(T item) => AddIfNotPresent(item, out _);
void ICollection<T>.Add(T item) => AddIfNotPresent(item, out _);underlying private bool AddIfNotPresent(T value, out int location)
{
if (_buckets == null)
{
Initialize(0);
}
Debug.Assert(_buckets != null);
Entry[]? entries = _entries;
// ...I think we could follow the same interface pattern closely to make our stuff more .NET-like :)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I ended up sending Matt much the same thing in a DM & gist. The key here is you don't need explicit implementation on both of them, just enough to disambiguate. Explicit implementation of both is also what causes this required cast in the first place, as explicit implementations always need an explicit cast. |
||
| { | ||
| EnsureInitialized(); | ||
|
|
||
|
|
@@ -499,10 +475,21 @@ bool ISet<T>.Add(T item) | |
| { | ||
| OnSetChanged(setEvent); | ||
| } | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| bool ISet<T>.Add(T item) | ||
| { | ||
| Add(item); | ||
| return true; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| void ICollection<T>.Add(T item) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Maybe use an expression-bodied method to make it SUPER clear this is just a remap... e.g. (you also only need the explicit implementation on one of them)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, cool, I can do that on
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My code snippet should work. You need to change your implementation to return a bool, and then just ignore it on ICollection, that should meet both ISet and ICollection needs (but is a moot point if you do the suggestion in the other comment thread) |
||
| { | ||
| Add(item); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public void Clear() | ||
| { | ||
|
|
||
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.
Git has made a mess of this diff; I just hoisted the Add functionality out of the interface-qualified definitions into a method and then made those interfaces call that method.