-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
de-genericify linked lists #23459
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
de-genericify linked lists #23459
Conversation
by making it always intrusive, we make it a more broadly useful API, and avoid binary bloat.
by making it always intrusive, we make it more broadly useful API, and avoid binary bloat.
this is trivial to tack on, and in my experience it is rarely wanted.
These changes make sense, though I feel like need for pub fn SinglyLinkedList(comptime T: type) type {
comptime {
if (@FieldType(T, "next") != ?*T) {
@compileError("type T must satisfy next field with type ?*T");
}
}
return struct { ... };
} This is a typical pattern we see in large Zig projects too (libxev, TigerBeetle). That way, new implementation can attract projects to switch to stdlib implementation. |
This assumes that using |
next_node.prev = node.prev; | ||
} else { | ||
// Last element of the list. | ||
list.last = node.prev; |
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.
What if the node is a member of a different list? I dont see how that is asserted here.
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.
It isn't asserted here, and the old code didn't either (it is basically the same code). So I think it's off topic for this PR.
With these changes, there's no longer any incentive to hand-roll next/prev pointers. A little bit less bloat too.
Migration Instructions
⬇️
Then use
@fieldParentPtr
to get fromnode
todata
.In many cases there's a better pattern instead which is to put the node intrusively into the data structure. If you're not already doing that, there's a good chance linked list is the wrong data structure.