zig/lib/std/io/seekable_stream.zig
Andrew Kelley ba0e3be5cf
(breaking) rework stream abstractions
The main goal here is to make the function pointers comptime, so that we
don't have to do the crazy stuff with async function frames.

Since InStream, OutStream, and SeekableStream are already generic
across error sets, it's not really worse to make them generic across the
vtable as well.

See #764 for the open issue acknowledging that using generics for these
abstractions is a design flaw.

See #130 for the efforts to make these abstractions non-generic.

This commit also changes the OutStream API so that `write` returns
number of bytes written, and `writeAll` is the one that loops until the
whole buffer is written.
2020-03-10 15:32:32 -04:00

37 lines
1.1 KiB
Zig

const std = @import("../std.zig");
const InStream = std.io.InStream;
pub fn SeekableStream(
comptime Context: type,
comptime SeekErrorType: type,
comptime GetSeekPosErrorType: type,
comptime seekToFn: fn (context: Context, pos: u64) SeekErrorType!void,
comptime seekByFn: fn (context: Context, pos: i64) SeekErrorType!void,
comptime getPosFn: fn (context: Context) GetSeekPosErrorType!u64,
comptime getEndPosFn: fn (context: Context) GetSeekPosErrorType!u64,
) type {
return struct {
context: Context,
const Self = @This();
pub const SeekError = SeekErrorType;
pub const GetSeekPosError = GetSeekPosErrorType;
pub fn seekTo(self: Self, pos: u64) SeekError!void {
return seekToFn(self.context, pos);
}
pub fn seekBy(self: Self, amt: i64) SeekError!void {
return seekByFn(self.context, amt);
}
pub fn getEndPos(self: Self) GetSeekPosError!u64 {
return getEndPosFn(self.context);
}
pub fn getPos(self: Self) GetSeekPosError!u64 {
return getPosFn(self.context);
}
};
}