At the moment, std.os.uefi is a very barebones wrapper around UEFI. I'm proposing to rework it in the following ways:
- Make functions that can fail return error unions
- Pass slices to functions instead of separate lengths and pointers
- Make reading and writing functions return the number of bytes read or written respectively
- Use packed struct bitfields instead of ORed integers
As an example, here is the current implementation of std.os.uefi.protocols.FileProtocol.read:
|
pub fn read(self: *const FileProtocol, buffer_size: *usize, buffer: [*]u8) Status { |
|
return self._read(self, buffer_size, buffer); |
|
} |
There are two main issues with this as I see it:
- The function returns an enum instead of an error union. This allows the error to be fairly easily discarded using
_ = . On top of that, without looking at the UEFI specification itself, it's very unclear what errors the functions could return.
- When passing the output buffer to the function, it takes a pointer to the buffer length as a parameter. The actual UEFI function then reads the file into the buffer and writes the number of bytes read to said pointer. On top of passing the pointer and length separately being completely unnecessary while slices exist, having the length of the buffer passed and then changed can lead to all kinds of confusion.
Under this proposal, that function would look like this:
pub fn read(self: *const FileProtocol, buffer: []u8) !usize {
var buffer_size = buffer.len;
return switch(self._read(self, buffer_size, buffer.ptr)) {
.Success => buffer_size,
.NoMedia => UEFIError.NoMedia,
.DeviceError => UEFIError.DeviceError,
.VolumeCorrupted => UEFIError.VolumeCorrupted,
.BufferTooSmall => buffer_size, //Didn't read to the end of the file, which Reader.read doesn't consider an error
else => unreachable,
};
}
This implementation, while a bit more complex, is much nicer to work with as an end user, as well as being more idiomatic.
At the moment,
std.os.uefiis a very barebones wrapper around UEFI. I'm proposing to rework it in the following ways:As an example, here is the current implementation of
std.os.uefi.protocols.FileProtocol.read:zig/lib/std/os/uefi/protocols/file_protocol.zig
Lines 36 to 38 in 132b18e
There are two main issues with this as I see it:
_ =. On top of that, without looking at the UEFI specification itself, it's very unclear what errors the functions could return.Under this proposal, that function would look like this:
This implementation, while a bit more complex, is much nicer to work with as an end user, as well as being more idiomatic.