feat: support timeouts and deadlines - #1018
Conversation
This commit adds support for specifying deadlines for read and write
operations over UARTs in one of two ways:
1. By providing a deadline as an absolute time in the future as was
done before. These deadlines can be 'refreshed' through the
`set_deadline` methods.
2. By providing a timeout with which a new deadline will be computed
upon performing a read or write operation.
This allows for a precise handling of deadlines when leveraging the
std.Io.{Reader,Writer} interface semantics.
|
Hi @Grazfather, @mattnite! Feel free to make any comments and/or changes: I'm by no means a Zig expert! For instance, I believe the explicit dereference in the Thanks a ton for your time! |
tact1m4n3
left a comment
There was a problem hiding this comment.
Solid work! I like the direction of this. I only have a few style suggestions.
| } | ||
| }; | ||
|
|
||
| pub const TimeFrontier = union(enum) { timeout_us: u64, deadline: mdf.time.Deadline }; |
There was a problem hiding this comment.
nit: maybe format this on multiple lines?
There was a problem hiding this comment.
You can add here a
pub const no_deadline: TimeFrontier = .{ .deadline = .no_deadline };
so you can do .no_deadline directly when you don't want any deadline.
| pub const Writer = struct { | ||
| uart: UART, | ||
| deadline: mdf.time.Deadline, | ||
| timeFrontier: TimeFrontier, |
There was a problem hiding this comment.
nit: fields should be snake case
| interface: std.Io.Writer, | ||
|
|
||
| pub fn set_deadline(self: *Writer, deadline: mdf.time.Deadline) void { | ||
| self.*.timeFrontier = TimeFrontier{.deadline = deadline}; |
There was a problem hiding this comment.
Doing self.* here is not necessary. Field access dereferences the pointer automatically
| var deadline: mdf.time.Deadline = undefined; | ||
| switch (uart_writer.timeFrontier) { | ||
| .deadline => |d| deadline = d, | ||
| .timeout_us => |t| deadline = time.deadline_in_us(t) | ||
| } |
There was a problem hiding this comment.
nit: switch can be used as an expression so you could do
const deadline: mdf.time.Deadline = switch (uart_writer.time_frontier) {
.deadline => |d| d,
.timeout_us => |t| time.deadline_in_us(t),
};| /// }; | ||
| pub fn init_logger(uart: UART) void { | ||
| uart_logger = uart.writer(.no_deadline, &.{}); | ||
| uart_logger = uart.writer(.{.deadline = .no_deadline}, &.{}); |
There was a problem hiding this comment.
Can become uart.writer(.no_deadline). See the suggestion above
|
Also CI formatting seems to fail. You should run zig fmt on the file. Or you can just setup your editor to do that on save so you don't have to think about it. |
This commit adds support for specifying deadlines for read and write operations over UARTs in one of two ways:
By providing a deadline as an absolute time in the future as was done before. These deadlines can be 'refreshed' through the
set_deadlinemethods.By providing a timeout with which a new deadline will be computed upon performing a read or write operation.
This allows for a precise handling of deadlines when leveraging the std.Io.{Reader,Writer} interface semantics.
Closes #1017