-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ability to mark a struct field as "pinned" in memory #3803
Comments
I think this is really beneficial for zig to have some kind of restriction on object locations. This prevents a whole class of errors related to "copying/moving stuff that is still referenced somewhere". Maybe it could be useful to allow But for struct fields its a great addition to safety |
This strikes me as related to Eric Lengyel's disjoint proposal for aliasing in C++. |
See also competing proposal #3804 |
IRC sparked this idea: const FixedBufferAllocator = struct {
allocator: &Allocator,
buf: []u8,
};
|
That's basically just a different way of saying e.g. |
1 difference. today we have this pattern which works because we explicitly ask for address of pinned memory: /// Implementation of io.OutStream trait for File
pub const OutStream = struct {
file: File,
stream: Stream,
...
};
...
var file_stream = self.in_file.inStream();
const in = &file_stream.stream; but with field decl "as pointer" modifier we decorate the field, and now a "copy" of the field /// Implementation of io.OutStream trait for File
pub const OutStream = struct {
file: File,
- stream: Stream,
+ stream: &Stream,
...
};
...
var file_stream = self.in_file.inStream();
-const in = &file_stream.stream;
+const in = file_stream.stream; |
What about a zero-sized type For example with such a type |
One problem with marking 'pinmem' types with const FixedBufferAllocator = struct {
allocator: @Pin(Allocator),
buf: []u8,
}; That would make a field not trivially copyable (same as in #3803 (comment)). However copy by dereferencing or memcopy would still be possible. Edit: @LemonBoy Yep im thinking the same. We can make the interface itself pinned |
Closing this in favor of #7769. Putting pinned on values would mean introducing a new CV qualifier on pointers in general. Putting it on types should be enough granularity, though we could reconsider if use cases are brought forth. |
This is @dbandstra's comment extracted into a separate issue.
I propose to use the keyword
pinmem
since it is 2 words smashed together lowercase, and therefore unlikely to alias anything useful.Example code:
This would provide compile-time error protections to prevent against #591.
@fieldParentPtr
would give a compile error if the field used did not have thepinmem
attribute.This would become significantly more useful with #2761 and #2765 implemented.
Another example (see related issue #172):
This makes the language bigger and more complicated. One of Zig's goals is to be a small, simple language. So what justifies the change? It prevents footguns.
Accepted proposal #2414 moves the footgun of #591 to be caught by runtime safety. However this proposal further eradicates this possible mistake, by making it a compile error. From the Zen:
Additional use cases:
zig/lib/std/segmented_list.zig
Line 94 in a438a61
zig/lib/std/event/loop.zig
Lines 96 to 99 in a438a61
The text was updated successfully, but these errors were encountered: