Skip to content

Instantly share code, notes, and snippets.

@shimamura-sakura
Last active February 6, 2024 08:52
Show Gist options
  • Save shimamura-sakura/bdb1fe6ecac81cbc0db93c55c421d1e2 to your computer and use it in GitHub Desktop.
Save shimamura-sakura/bdb1fe6ecac81cbc0db93c55c421d1e2 to your computer and use it in GitHub Desktop.
A helper type for reading from memory files as a stream, referencing original memory.
fn Slice(comptime T: anytype) type {
return struct {
const Self = @This();
const Error = error{EOF};
left: T,
pub fn take(self: *Self, n: anytype) Error!@TypeOf(self.left[0..n]) {
if (self.left.len < n) return Error.EOF;
defer self.left = self.left[n..];
return self.left[0..n];
}
pub fn byte(self: *Self) Error!u8 {
if (self.left.len < 1) return Error.EOF;
defer self.left = self.left[1..];
return self.left[0];
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment