From 0c7397b49f8c434cf1f714c08ab37d1429ab1be7 Mon Sep 17 00:00:00 2001 From: Jonathan Marler Date: Wed, 6 May 2020 23:08:08 -0600 Subject: fix copy/paste error in AllocWithOptionaPayload --- lib/std/mem.zig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/std') diff --git a/lib/std/mem.zig b/lib/std/mem.zig index a40334e587..210b5cc108 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; } } -- cgit v1.2.3 From 0a76e11617a76d884a52bf49c7b2b9a706e7bc8c Mon Sep 17 00:00:00 2001 From: Jonathan Marler Date: Wed, 6 May 2020 23:56:48 -0600 Subject: add failAllocator to enable some regression tests --- lib/std/mem.zig | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'lib/std') diff --git a/lib/std/mem.zig b/lib/std/mem.zig index 210b5cc108..13fd3d05f3 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -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. -- cgit v1.2.3