make errors take *const Self, which avoids a copy/stabilizes the pointer
All checks were successful
Generic zig build / build (push) Successful in 22s
All checks were successful
Generic zig build / build (push) Successful in 22s
This commit is contained in:
parent
d0599320f4
commit
7692d85745
1 changed files with 20 additions and 1 deletions
21
src/srf.zig
21
src/srf.zig
|
|
@ -84,7 +84,7 @@ pub fn BoundedDiagnostics(comptime max_errors: usize) type {
|
||||||
self.buffer[self.error_count].message = try self.allocator.dupe(u8, err.message);
|
self.buffer[self.error_count].message = try self.allocator.dupe(u8, err.message);
|
||||||
self.error_count += 1;
|
self.error_count += 1;
|
||||||
}
|
}
|
||||||
pub fn errors(self: Self) []const ParseLineError {
|
pub fn errors(self: *const Self) []const ParseLineError {
|
||||||
return self.buffer[0..self.error_count];
|
return self.buffer[0..self.error_count];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
@ -2698,3 +2698,22 @@ test "raw values returned iff include_raw is set to true" {
|
||||||
const raw_fi = (try raw_ri.next()).?;
|
const raw_fi = (try raw_ri.next()).?;
|
||||||
try std.testing.expectEqualStrings("200.00", (try raw_fi.next()).?.raw.?);
|
try std.testing.expectEqualStrings("200.00", (try raw_fi.next()).?.raw.?);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test "BoundedDiagnostics.errors returns a view into the diagnostics, not a copy" {
|
||||||
|
var diags: BoundedDiagnostics(4) = .empty;
|
||||||
|
try diags.addError(.{
|
||||||
|
.message = "error parsing numeric value",
|
||||||
|
.level = .err,
|
||||||
|
.line = 3,
|
||||||
|
.column = 15,
|
||||||
|
});
|
||||||
|
|
||||||
|
// `errors()` hands out a slice of `self.buffer`, so it has to be *our*
|
||||||
|
// buffer. Taking `self` by value copies the whole struct, and the slice then
|
||||||
|
// points into a temporary that dies with the call: every read through it is
|
||||||
|
// a read of a dead stack frame.
|
||||||
|
try std.testing.expectEqual(
|
||||||
|
@intFromPtr(&diags.buffer),
|
||||||
|
@intFromPtr(diags.errors().ptr),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue