authorgravatar for justus@klausecker.deJustus Klausecker <justus@klausecker.de> 2026-02-11 00:41:04+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-02-12 21:45:13-08:00
log35c5611f0707a46f6314a7b4e4597ff56bfead18
tree3e1f72329060c1ab5df8f0517b773228a6f95352
parentf36d0573cd0f80b271b9f07a7215fa237d14ae9a

std.Deque: add `*Ptr` variants of getter functions

This makes it practical to store large items or items that are meant to be mutable directly inside of the deque. It is the responsibility of the user to stop using the returned pointers after calling a function that could invalidate them.

1 files changed, 22 insertions(+), 0 deletions(-)

lib/std/deque.zig+22
...@@ -180,12 +180,24 @@ pub fn Deque(comptime T: type) type {...@@ -180,12 +180,24 @@ pub fn Deque(comptime T: type) type {
180 return deque.buffer[deque.head];180 return deque.buffer[deque.head];
181 }181 }
182182
183 /// Return pointer to the first item in the deque or null if empty.
184 pub fn frontPtr(deque: *const Self) ?*T {
185 if (deque.len == 0) return null;
186 return &deque.buffer[deque.head];
187 }
188
183 /// Return the last item in the deque or null if empty.189 /// Return the last item in the deque or null if empty.
184 pub fn back(deque: *const Self) ?T {190 pub fn back(deque: *const Self) ?T {
185 if (deque.len == 0) return null;191 if (deque.len == 0) return null;
186 return deque.buffer[deque.bufferIndex(deque.len - 1)];192 return deque.buffer[deque.bufferIndex(deque.len - 1)];
187 }193 }
188194
195 /// Return the last item in the deque or null if empty.
196 pub fn backPtr(deque: *const Self) ?*T {
197 if (deque.len == 0) return null;
198 return &deque.buffer[deque.bufferIndex(deque.len - 1)];
199 }
200
189 /// Return the item at the given index in the deque.201 /// Return the item at the given index in the deque.
190 ///202 ///
191 /// The first item in the queue is at index 0.203 /// The first item in the queue is at index 0.
...@@ -196,6 +208,16 @@ pub fn Deque(comptime T: type) type {...@@ -196,6 +208,16 @@ pub fn Deque(comptime T: type) type {
196 return deque.buffer[deque.bufferIndex(index)];208 return deque.buffer[deque.bufferIndex(index)];
197 }209 }
198210
211 /// Return pointer to the item at the given index in the deque.
212 ///
213 /// The first item in the queue is at index 0.
214 ///
215 /// Asserts that the index is in-bounds.
216 pub fn atPtr(deque: *const Self, index: usize) *T {
217 assert(index < deque.len);
218 return &deque.buffer[deque.bufferIndex(index)];
219 }
220
199 /// Remove and return the first item in the deque or null if empty.221 /// Remove and return the first item in the deque or null if empty.
200 pub fn popFront(deque: *Self) ?T {222 pub fn popFront(deque: *Self) ?T {
201 if (deque.len == 0) return null;223 if (deque.len == 0) return null;