Last active
February 6, 2024 08:52
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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