-

@ Animegirl supremacist
2025-02-23 06:12:04
nostr:nprofile1qy2hwumn8ghj7un9d3shjtnddaehgu3wwp6kyqpqs77zadrsjwsmrmtf566vz374l6v8wgnmhz3npcetjaugjdscvzsqz9cjsz
>it’s only one line, easier to read
I don't know
>i did what i did because
I have a really /comfy/ way of handling it:
const AlsaError = error{
InvalidState,
InvalidArgument,
DeviceBusy,
FileNotFound,
WouldBlock,
Xrun,
};
fn TODO() noreturn {
@panic("not implemented");
}
fn AlsaValueReturn(comptime T: type) type {
return @Type(.{ .int = .{
.signedness = .unsigned,
.bits = @typeInfo(T).int.bits - @intFromBool(@typeInfo(T).int.signedness == .signed),
} });
}
fn alsaValue(code: anytype) AlsaError!AlsaValueReturn(@TypeOf(code)) {
if (code < 0) return switch (-code) {
// errno errors
@intFromEnum(std.os.linux.E.PERM) => AlsaError.DeviceBusy,
@intFromEnum(std.os.linux.E.NOENT) => AlsaError.FileNotFound,
@intFromEnum(std.os.linux.E.BADFD) => AlsaError.InvalidState,
@intFromEnum(std.os.linux.E.IO) => TODO(),
@intFromEnum(std.os.linux.E.INVAL) => AlsaError.InvalidArgument,
@intFromEnum(std.os.linux.E.BUSY) => AlsaError.DeviceBusy,
@intFromEnum(std.os.linux.E.AGAIN) => AlsaError.WouldBlock,
@intFromEnum(std.os.linux.E.PIPE) => AlsaError.Xrun,
// asoundlib-specific errors
c.SND_ERROR_INCOMPATIBLE_VERSION => TODO(),
c.SND_ERROR_ALISP_NIL => TODO(),
else => @panic(@tagName(@as(std.os.linux.E, @enumFromInt(-code)))),
};
return @intCast(code);
}
fn alsaError(value: anytype) AlsaError!void {
std.debug.assert(try alsaValue(value) == 0);
return;
}
Use alsaValue for the ones that can return a value other than an error code (e.g. snd_pcm_avail). Use alsaError for the ones that only return zero or an error code. I'll post an example of how to use it in a later reply—it wouldn't clear the character limit if I included it here (even if I have more than 11 hundred characters remaining at this point (I fell for the html meme))