Just a heads up: looks like std.mem.copy
has been deprecated in favor of @memcpy
builtin.
Here’s a commit that migrated some of the (?) calls to std.mem.copy
to @memcpy
- Remove all usages of `std.mem.copy` and remove `std.mem.set` (#18143) · ziglang/zig@1e42a3d · GitHub , could be useful as a model to follow.
Also, note that there is a difference between them - std.mem.copy
was an alias for std.mem.copyForwards
, which worked (and still works) for overlapping memory buffers. @memcpy
, however, doesn’t relish the peach… I mean, overlapping buffers.
Old copy
declaration:
/// Deprecated: use `@memcpy` if the arguments do not overlap, or
/// `copyForwards` if they do.
pub const copy = copyForwards;
Documentation on @memcpy
:
Finally, the two memory regions must not overlap
5 Likes
Note that std.mem.copy
has been deprecated since April 2023. The relevant context with a giant amount of examples of how code was updated:
Where the change was made:
ziglang:master
← ziglang:memcpy-memset
opened 05:21AM - 14 Apr 23 UTC
Now they use slices or array pointers with any element type instead of requiring… byte pointers.
This is a breaking enhancement to the language.
* `std.mem.copy` will be used for only possibly-overlapping slices, and thus renamed to `copyForwards`.
* `std.mem.set` will be deleted in favor of the builtin.
closes #14040
closes #14094
## Merge Checklist
* [x] the TODO in llvm.zig airStore
* [x] `@memset` undefined not optimized out #14094
* [x] llvm: store undef if safety is disabled #15003
* [x] add test: runtime safety check for memcpy_alias
* [x] add test: runtime safety check for memcpy_len_mismatch
* [x] add test: compile error for unknown memcpy length
* [x] add test: compile error for non-matching memcpy lengths
* [x] add test: runtime safety for memset array to undefined, ABI size == 1
* [x] add test: runtime safety for memset array to undefined, ABI size > 1
* [x] add safety test: memset slice to undefined, ABI size == 1
* [x] add safety test: memset slice to undefined, ABI size > 1
* [ ] ~Sema: implement memcpy used with tuples~ #15479
Where tons of code was updated:
ziglang:master
← ziglang:use-mem-intrinsics
opened 02:39AM - 27 Apr 23 UTC
Follow-up from #15278. This branch updates the codebase to actually switch from … `std.mem.set` and `std.mem.copy` to `@memset` and `@memcpy` respectively.
I had to update zig1.wasm again because of bugs that this uncovered in the C backend.
There are a few more instances to update before merging.
3 Likes
Yes, it was a poor choice of words on my part. I should have used " std.mem.copy
has been removed in favor of @memcpy
builtin and / or std.mem.copyForwards
, of which it was an alias anyway".
I didn’t want to edit the post, probably should have.