Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

wasm4.zig: print wrappers with comptime formatting and type safety #673

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Ruulul
Copy link

@Ruulul Ruulul commented Nov 17, 2023

add functions print, that prints on console, printText, that wraps the text function, and printBuffer, that prints on an arbitrary buffer. allows leveraging custom formatters on custom types, type safety, and use the error handling for ergonomics.

add functions `print`, that prints on console, `printText`, that wraps the `text` function, and `printBuffer`, that prints on an arbitrary buffer.
allows leveraging custom formatters on custom types, type safety, and use the error handling for ergonomics.
@Ruulul Ruulul changed the title print wrappers with comptime formatting and type safety wasm4.zig: print wrappers with comptime formatting and type safety Nov 17, 2023
@xdBronch
Copy link

the stdlib already has functions to print to a buffer and with an arguably better API. im also a little confused about the other functions youve added, if i know the extra_len that i want, why not just create an array myself and use the buffer print function?

@Ruulul
Copy link
Author

Ruulul commented Nov 21, 2023

again didnt know about this one handy function

about the why... I just realized it was being too common to write out a block, create a scoped buffer, print to that buffer, then send the buffer to the proper function (w4.trace or w4.text)

and on a second note, I dont like to need to manually count characters to know the needed buffer size, and it is not viable to do the std.debug trick and make a [1024]u8 buffer on wasm4, so I settled to an offset based on the fmt string len

mostly I wanted a way to be able to do this kind of pattern in an ergonomic way:

w4.textPrint(1, "Gen:\n{}", 135, 10, .{generation}) catch {
  w4.text("Gen:\nXe?", 135, 20);
};

I am curious tho of what is the expected way to handle printing in general... without a wrapper I would need to do something like...

{
  const fmt = "Gen:\n{}";
  var buffer: [@as(comptime_int, fmt.len) + 1]u8 = undefined;
  w4.text(std.fmt.bufPrint(&buffer, fmt, .{generation}) catch "Gen:\nXe?", 135, 20);
}

I understand tho if you see this as not general enough for the wasm4.zig file. I just thought it is a pretty common need to want to use a formatter, and it is unpleasant to need to manually count characters to know the proper offsets

@Ruulul
Copy link
Author

Ruulul commented Nov 21, 2023

I just realized I can write a simple comptime logic to make all calls use the same buffer, but I am not sure if this is worthywhile

@JerwuQu
Copy link
Contributor

JerwuQu commented Apr 18, 2024

I think this is better handled by users since you both need to specify how big you want the buffer to be and it adds a dependency on std.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants