Type Function FixedBufferStream [src]
Alias for std.io.fixed_buffer_stream.FixedBufferStream
This turns a byte buffer into an io.Writer, io.Reader, or io.SeekableStream.
If the supplied byte buffer is const, then io.Writer is not available.
Prototype
pub fn FixedBufferStream(comptime Buffer: type) type
Parameters
Buffer: type
Source
pub fn FixedBufferStream(comptime Buffer: type) type {
return struct {
/// `Buffer` is either a `[]u8` or `[]const u8`.
buffer: Buffer,
pos: usize,
pub const ReadError = error{};
pub const WriteError = error{NoSpaceLeft};
pub const SeekError = error{};
pub const GetSeekPosError = error{};
pub const Reader = io.Reader(*Self, ReadError, read);
pub const Writer = io.Writer(*Self, WriteError, write);
pub const SeekableStream = io.SeekableStream(
*Self,
SeekError,
GetSeekPosError,
seekTo,
seekBy,
getPos,
getEndPos,
);
const Self = @This();
pub fn reader(self: *Self) Reader {
return .{ .context = self };
}
pub fn writer(self: *Self) Writer {
return .{ .context = self };
}
pub fn seekableStream(self: *Self) SeekableStream {
return .{ .context = self };
}
pub fn read(self: *Self, dest: []u8) ReadError!usize {
const size = @min(dest.len, self.buffer.len - self.pos);
const end = self.pos + size;
@memcpy(dest[0..size], self.buffer[self.pos..end]);
self.pos = end;
return size;
}
/// If the returned number of bytes written is less than requested, the
/// buffer is full. Returns `error.NoSpaceLeft` when no bytes would be written.
/// Note: `error.NoSpaceLeft` matches the corresponding error from
/// `std.fs.File.WriteError`.
pub fn write(self: *Self, bytes: []const u8) WriteError!usize {
if (bytes.len == 0) return 0;
if (self.pos >= self.buffer.len) return error.NoSpaceLeft;
const n = @min(self.buffer.len - self.pos, bytes.len);
@memcpy(self.buffer[self.pos..][0..n], bytes[0..n]);
self.pos += n;
if (n == 0) return error.NoSpaceLeft;
return n;
}
pub fn seekTo(self: *Self, pos: u64) SeekError!void {
self.pos = @min(std.math.lossyCast(usize, pos), self.buffer.len);
}
pub fn seekBy(self: *Self, amt: i64) SeekError!void {
if (amt < 0) {
const abs_amt = @abs(amt);
const abs_amt_usize = std.math.cast(usize, abs_amt) orelse std.math.maxInt(usize);
if (abs_amt_usize > self.pos) {
self.pos = 0;
} else {
self.pos -= abs_amt_usize;
}
} else {
const amt_usize = std.math.cast(usize, amt) orelse std.math.maxInt(usize);
const new_pos = std.math.add(usize, self.pos, amt_usize) catch std.math.maxInt(usize);
self.pos = @min(self.buffer.len, new_pos);
}
}
pub fn getEndPos(self: *Self) GetSeekPosError!u64 {
return self.buffer.len;
}
pub fn getPos(self: *Self) GetSeekPosError!u64 {
return self.pos;
}
pub fn getWritten(self: Self) Buffer {
return self.buffer[0..self.pos];
}
pub fn reset(self: *Self) void {
self.pos = 0;
}
};
}