std.Io make Clock.resolution fallible

This commit is contained in:
Andrew Kelley 2026-02-03 01:02:48 -08:00
parent 11476d83c9
commit fe5da36aa3
2 changed files with 9 additions and 4 deletions

View file

@ -232,7 +232,7 @@ pub const VTable = struct {
progressParentFile: *const fn (?*anyopaque) std.Progress.ParentFileError!File,
now: *const fn (?*anyopaque, Clock) Timestamp,
clockResolution: *const fn (?*anyopaque, Clock) Duration,
clockResolution: *const fn (?*anyopaque, Clock) Clock.ResolutionError!Duration,
sleep: *const fn (?*anyopaque, Timeout) Cancelable!void,
random: *const fn (?*anyopaque, buffer: []u8) void,
@ -713,9 +713,14 @@ pub const Clock = enum {
return io.vtable.now(io.userdata, clock);
}
pub const ResolutionError = error{
ClockUnavailable,
Unexpected,
};
/// Reveals the granularity of `clock`. May be zero, indicating
/// unsupported clock.
pub fn resolution(clock: Clock, io: Io) Io.Duration {
pub fn resolution(clock: Clock, io: Io) ResolutionError!Io.Duration {
return io.vtable.clockResolution(io.userdata, clock);
}

View file

@ -10833,7 +10833,7 @@ fn nowInner(clock: Io.Clock) Io.Timestamp {
};
}
fn clockResolution(userdata: ?*anyopaque, clock: Io.Clock) Io.Duration {
fn clockResolution(userdata: ?*anyopaque, clock: Io.Clock) Io.Clock.ResolutionError!Io.Duration {
const t: *Threaded = @ptrCast(@alignCast(userdata));
_ = t;
return switch (native_os) {
@ -10872,7 +10872,7 @@ fn clockResolution(userdata: ?*anyopaque, clock: Io.Clock) Io.Duration {
};
}
fn clockResolutionPosix(clock: Io.Clock) Io.Duration {
fn clockResolutionPosix(clock: Io.Clock) Io.Clock.ResolutionError!Io.Duration {
const clock_id: posix.clockid_t = clockToPosix(clock);
var timespec: posix.timespec = undefined;
return switch (posix.errno(posix.system.clock_getres(clock_id, &timespec))) {