-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Cast from slices to array pointers #863
Comments
I like Option 1. Let's implement that and then see if we need to introduce anything else to make slicing seamless. |
* add `@bswap` builtin function. See #767 * comptime evaluation facilities are improved to be able to handle a `@ptrCast` with a backing array. * `@truncate` allows "truncating" a u0 value to any integer type, and the result is always comptime known to be `0`. * when specifying pointer alignment in a type expression, the alignment value of pointers which do not have addresses at runtime is ignored, and always has the default/ABI alignment * threw in a fix to freebsd/x86_64.zig to update syntax from language changes * some improvements are pending #863 closes #638 closes #1733 std lib API changes * io.InStream().readIntNe renamed to readIntNative * io.InStream().readIntLe renamed to readIntLittle * io.InStream().readIntBe renamed to readIntBig * introduced io.InStream().readIntForeign * io.InStream().readInt has parameter order changed * io.InStream().readVarInt has parameter order changed * io.InStream().writeIntNe renamed to writeIntNative * introduced io.InStream().writeIntForeign * io.InStream().writeIntLe renamed to writeIntLittle * io.InStream().writeIntBe renamed to writeIntBig * io.InStream().writeInt has parameter order changed * mem.readInt has different parameters and semantics * introduced mem.readIntNative * introduced mem.readIntForeign * mem.readIntBE renamed to mem.readIntBig and different API * mem.readIntLE renamed to mem.readIntLittle and different API * introduced mem.readIntSliceNative * introduced mem.readIntSliceForeign * introduced mem.readIntSliceLittle * introduced mem.readIntSliceBig * introduced mem.readIntSlice * mem.writeInt has different parameters and semantics * introduced mem.writeIntNative * introduced mem.writeIntForeign * mem.writeIntBE renamed to mem.readIntBig and different semantics * mem.writeIntLE renamed to mem.readIntLittle and different semantics * introduced mem.writeIntSliceForeign * introduced mem.writeIntSliceNative * introduced mem.writeIntSliceBig * introduced mem.writeIntSliceLittle * introduced mem.writeIntSlice * removed mem.endianSwapIfLe * removed mem.endianSwapIfBe * removed mem.endianSwapIf * added mem.littleToNative * added mem.bigToNative * added mem.toNative * added mem.nativeTo * added mem.nativeToLittle * added mem.nativeToBig
Note after implementing this, audit all the calls to |
Here is a fun thing this proposals will allow a[1..4].* = b[2..5].*; |
not sure if this was said already, but you can get a comptime len slice at a runtime offset with |
Would |
would this break zig's "no hidden control flow" or do |
There are a few functions implemented by the compiler that don't count as hidden control flow. memcpy and 128bit arithmetic are some that come to mind. The reason these don't count is that they're defined by the language, not user configurable. It's also not clear in each case if the optimizer will emit a function call or inline the assembly. The fact that there's a jump at all is kind of an implementation detail, not significant to the semantics. |
Landed in dc04e97. |
Array pointers are slices, but with a length known at compile time. This is powerful because it allows many of Zig's compile time constructs to work on them such as comptime bound check for constants
and(inline for
inline for
can't unroll runtime arrays. We can useinline while
to do it though).I propose making it safer and easier to convert from slices to array pointers so that it encourages people to utilize the static guarantees that array pointers provide.
The current method is no good.
ptrCast
is a scary builtin, and leaving out the2
ins[1..2]
will bypass the safety check that ensures that our slice is at least the size of the array we are casting too.Option 1 has some upsides. It will seamlessly flow around array pointers, giving you all their benefits without you even trying. Plus, array pointers should implicitly cast to slices (currently, only
&const [1]u8
->[]const u8
works, not&[1]u8
->[]u8
), so this shouldn't break much code. This does have the downside that changing aconst
declaration tovar
might break code that relies on these implicit array pointers.Option 2 favors the programmer being more explicit about when they want the static guaranty of the array pointer. This allows a more clearly expressed intent, and code your code will only break in predictable ways when
const
s are changed tovar
(where the cast takes place).Note, that indexing array pointers right now is a pain, as
[]
works on both pointers and arrays. #770 Fixes this.The text was updated successfully, but these errors were encountered: