diff options
| author | Vexu <git@vexu.eu> | 2020-05-07 12:39:57 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-05-07 12:39:57 +0300 |
| commit | 54b2a6ec41ae7be190163e94dd75bafb359cf239 (patch) | |
| tree | 4d29dcb2c5ced4ccc9ccff918109c65bff4a33a9 | |
| parent | b336dda0765ba345253ed4b25113a5db4386c3f0 (diff) | |
| parent | 0a76e11617a76d884a52bf49c7b2b9a706e7bc8c (diff) | |
| download | zig-54b2a6ec41ae7be190163e94dd75bafb359cf239.tar.gz zig-54b2a6ec41ae7be190163e94dd75bafb359cf239.zip | |
Merge pull request #5287 from marler8997/fixAllocWithPayload
fix copy/paste error in AllocWithOptionaPayload
| -rw-r--r-- | lib/std/mem.zig | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/lib/std/mem.zig b/lib/std/mem.zig index a40334e587..13fd3d05f3 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -124,9 +124,9 @@ pub const Allocator = struct { fn AllocWithOptionsPayload(comptime Elem: type, comptime alignment: ?u29, comptime sentinel: ?Elem) type { if (sentinel) |s| { - return [:s]align(alignment orelse @alignOf(T)) Elem; + return [:s]align(alignment orelse @alignOf(Elem)) Elem; } else { - return []align(alignment orelse @alignOf(T)) Elem; + return []align(alignment orelse @alignOf(Elem)) Elem; } } @@ -281,6 +281,22 @@ pub const Allocator = struct { } }; +var failAllocator = Allocator { + .reallocFn = failAllocatorRealloc, + .shrinkFn = failAllocatorShrink, +}; +fn failAllocatorRealloc(self: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) ![]u8 { + return error.OutOfMemory; +} +fn failAllocatorShrink(self: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) []u8 { + @panic("failAllocatorShrink should never be called because it cannot allocate"); +} + +test "mem.Allocator basics" { + testing.expectError(error.OutOfMemory, failAllocator.alloc(u8, 1)); + testing.expectError(error.OutOfMemory, failAllocator.allocSentinel(u8, 1, 0)); +} + /// Copy all of source into dest at position 0. /// dest.len must be >= source.len. /// dest.ptr must be <= src.ptr. |
