This commit is contained in:
whatisaphone 2025-11-23 22:57:06 +00:00 committed by GitHub
commit 7008ac6ac9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -168,6 +168,10 @@ pub fn fixed(buffer: []const u8) Reader {
}
pub fn stream(r: *Reader, w: *Writer, limit: Limit) StreamError!usize {
if (limit == .nothing) {
@branchHint(.unlikely);
return 0;
}
const buffer = limit.slice(r.buffer[r.seek..r.end]);
if (buffer.len > 0) {
@branchHint(.likely);
@ -1336,6 +1340,14 @@ test fixed {
try testing.expectError(error.EndOfStream, r.takeByte());
}
test "fixed: when streaming 0 bytes, return 0" {
var r: Reader = .fixed("i'm dying");
var trash: Writer.Discarding = .init(&.{});
const n = try r.stream(&trash.writer, .nothing);
try testing.expectEqual(n, 0);
}
test peek {
var r: Reader = .fixed("abc");
try testing.expectEqualStrings("ab", try r.peek(2));