mirror of
https://github.com/ziglang/zig.git
synced 2024-11-27 07:32:44 +00:00
mem: explicit dupe and dupeZ error on Allocator
This commit is contained in:
parent
6484e279e5
commit
9126852ba9
@ -315,14 +315,14 @@ pub fn free(self: Allocator, memory: anytype) void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Copies `m` to newly allocated memory. Caller owns the memory.
|
/// Copies `m` to newly allocated memory. Caller owns the memory.
|
||||||
pub fn dupe(allocator: Allocator, comptime T: type, m: []const T) ![]T {
|
pub fn dupe(allocator: Allocator, comptime T: type, m: []const T) Error![]T {
|
||||||
const new_buf = try allocator.alloc(T, m.len);
|
const new_buf = try allocator.alloc(T, m.len);
|
||||||
@memcpy(new_buf, m);
|
@memcpy(new_buf, m);
|
||||||
return new_buf;
|
return new_buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Copies `m` to newly allocated memory, with a null-terminated element. Caller owns the memory.
|
/// Copies `m` to newly allocated memory, with a null-terminated element. Caller owns the memory.
|
||||||
pub fn dupeZ(allocator: Allocator, comptime T: type, m: []const T) ![:0]T {
|
pub fn dupeZ(allocator: Allocator, comptime T: type, m: []const T) Error![:0]T {
|
||||||
const new_buf = try allocator.alloc(T, m.len + 1);
|
const new_buf = try allocator.alloc(T, m.len + 1);
|
||||||
@memcpy(new_buf[0..m.len], m);
|
@memcpy(new_buf[0..m.len], m);
|
||||||
new_buf[m.len] = 0;
|
new_buf[m.len] = 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user