mirror of
https://github.com/ziglang/zig.git
synced 2026-01-20 22:35:24 +00:00
FIX resize() for non u8 element types. (#9806)
This commit is contained in:
parent
4afe4bdfe7
commit
e14fcd60cb
@ -147,6 +147,46 @@ test "mem.Allocator basics" {
|
||||
try testing.expectError(error.OutOfMemory, failAllocator.allocSentinel(u8, 1, 0));
|
||||
}
|
||||
|
||||
test "Allocator.resize" {
|
||||
const primitiveIntTypes = .{
|
||||
i8,
|
||||
u8,
|
||||
i16,
|
||||
u16,
|
||||
i32,
|
||||
u32,
|
||||
i64,
|
||||
u64,
|
||||
i128,
|
||||
u128,
|
||||
isize,
|
||||
usize,
|
||||
};
|
||||
inline for (primitiveIntTypes) |T| {
|
||||
var values = try testing.allocator.alloc(T, 100);
|
||||
defer testing.allocator.free(values);
|
||||
|
||||
for (values) |*v, i| v.* = @intCast(T, i);
|
||||
values = try testing.allocator.resize(values, values.len + 10);
|
||||
try testing.expect(values.len == 110);
|
||||
}
|
||||
|
||||
const primitiveFloatTypes = .{
|
||||
f16,
|
||||
f32,
|
||||
f64,
|
||||
f128,
|
||||
};
|
||||
inline for (primitiveFloatTypes) |T| {
|
||||
var values = try testing.allocator.alloc(T, 100);
|
||||
defer testing.allocator.free(values);
|
||||
|
||||
for (values) |*v, i| v.* = @intToFloat(T, i);
|
||||
values = try testing.allocator.resize(values, values.len + 10);
|
||||
try testing.expect(values.len == 110);
|
||||
}
|
||||
}
|
||||
|
||||
/// Copy all of source into dest at position 0.
|
||||
/// dest.len must be >= source.len.
|
||||
/// If the slices overlap, dest.ptr must be <= src.ptr.
|
||||
|
||||
@ -313,7 +313,7 @@ pub fn resize(self: *Allocator, old_mem: anytype, new_n: usize) Error!@TypeOf(ol
|
||||
const new_byte_count = math.mul(usize, @sizeOf(T), new_n) catch return Error.OutOfMemory;
|
||||
const rc = try self.resizeFn(self, old_byte_slice, Slice.alignment, new_byte_count, 0, @returnAddress());
|
||||
assert(rc == new_byte_count);
|
||||
const new_byte_slice = old_mem.ptr[0..new_byte_count];
|
||||
const new_byte_slice = old_byte_slice.ptr[0..new_byte_count];
|
||||
return mem.bytesAsSlice(T, new_byte_slice);
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user