Skip to content

Add ListUnmanaged.print#2995

Merged
IntegratedQuantum merged 8 commits intoPixelGuys:masterfrom
Argmaster:ListUnmanaged.print
May 3, 2026
Merged

Add ListUnmanaged.print#2995
IntegratedQuantum merged 8 commits intoPixelGuys:masterfrom
Argmaster:ListUnmanaged.print

Conversation

@Argmaster
Copy link
Copy Markdown
Collaborator

@Argmaster Argmaster commented Apr 30, 2026

This pull request adds print method to ListUnmanaged struct.
To avoid copying a leftover TODO from print function in List, it also makes an attempt on addressing it:

https://github.com/Argmaster/Cubyz/blob/ListUnmanaged.print/src/utils/list.zig#L213

// TODO: Is there no easier way to make this without an extra copy?

We might as well just leave it for different time tho

Resolves: #1425 (comment)

@Argmaster Argmaster marked this pull request as ready for review May 1, 2026 13:18
@Argmaster Argmaster moved this to Easy to Review in PRs to review May 1, 2026
Comment thread src/utils/list.zig Outdated
@IntegratedQuantum IntegratedQuantum moved this from Easy to Review to In review in PRs to review May 1, 2026
Comment thread src/utils/list.zig Outdated
// we use the list as print destination, we want to make sure it retains normal list behavior
// by inserting a single element.
list.append(main.stackAllocator, '\n');
list.print(main.stackAllocator, "BarFooSpam {}", .{main.vec.Vec2d{0.3, 0.4}});
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way how more complex objects are printed is possibly fragile, and I don't think we should rely on it in tests.
Plus the dependence on Vec2d (which is going to change once Zig adds glsl-style vectors) is unnecessary.

Copy link
Copy Markdown
Collaborator Author

@Argmaster Argmaster May 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, would a array be fine? I would want something more than just a single value tbh

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why though, what do you intend to test with a composite type?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, yeah I guess it doesn't make that much sense. I was somewhat aiming to test if realloc works and if weirder things can still be printed properly.

What about I replace it with a test that checks if .initCapactiy preserves the items pointer?
I would also test if items pointer changes when not using .initCapacity.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -421,12 +421,28 @@ pub fn ListUnmanaged(comptime T: type) type {
        };
 }

+test "ListUnmanaged.print buffer preserved" {
+       var list: ListUnmanaged(u8) = .initCapacity(main.stackAllocator, 20);
+       const oldAddress = list.items.ptr;
+       defer list.deinit(main.stackAllocator);
+
+       list.print(main.stackAllocator, "foo {}", .{34});
+       const newAddress = list.items.ptr;
+
+       try std.testing.expect(oldAddress == newAddress);
+       try std.testing.expectEqualStrings("foo 34", list.items);
+       try std.testing.expect(list.items.len <= list.capacity);
+}
+
 test "ListUnmanaged.print single call" {
        var list: ListUnmanaged(u8) = .{};
+       const oldAddress = list.items.ptr;
        defer list.deinit(main.stackAllocator);

-       list.print(main.stackAllocator, "foo {}", .{34});
+       list.print(main.stackAllocator, "foo {d:.1}", .{34});
+       const newAddress = list.items.ptr;

+       try std.testing.expect(oldAddress != newAddress);
        try std.testing.expectEqualStrings("foo 34", list.items);
        try std.testing.expect(list.items.len <= list.capacity);
 }

@Argmaster Argmaster force-pushed the ListUnmanaged.print branch from 75f0ff5 to 8aa164b Compare May 2, 2026 11:06
@Argmaster Argmaster force-pushed the ListUnmanaged.print branch from 8aa164b to 53ff61e Compare May 2, 2026 11:06
@Argmaster
Copy link
Copy Markdown
Collaborator Author

/home/runner/work/Cubyz/Cubyz/build.zig.zon:41:11: error: bad HTTP response code: '502 Bad Gateway'
.url = "https://github.com/PixelGuys/Cubyz-Libs/releases/download/11/cubyz_deps_x86_64-windows-gnu.tar.gz",

<3

@IntegratedQuantum IntegratedQuantum merged commit 727575f into PixelGuys:master May 3, 2026
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants