-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
way to cast arrays to a smaller array #1376
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
Comments
are you expecting that to make a copy? |
yes, but generally I wanted to use it as a function call
here is x25519 i was working on http://paste.debian.net/1037660/
where having a seperate variable just to pass as an argument is silly. |
that example makes sense. seems like we should be able to make |
as seen in the later example: |
wait. is this issue about copying or referencing? |
referencing, but as the function does not modify the array, copying does the same thing in this case. |
ok. the OP was misleading then. // this designates a storage location for 2 u32's
var smaller_array: [2]u32 = ...;
// this parameter does not necessitate any actual storage,
// and will in practice be a const reference, not a copy.
fn foo(a: [2]u32) void {}
// this is definitely a reference, not a copy.
var a: *[2]u32 = ...; i think the here's an idea: var array: [64]u32 = ...;
var first_half: *[32]u32 = array[0..32];
var second_half: *[32]u32 = array[32..]; In order for this to work, the compiler would need to know the slicing index/bounds at comptime. That's kind of an awkward requirement, since the comptime-knownness of values determines what kinds of type casts are allowed. More specifically, the compiler really only needs to know the length of the resulting slice, not the start index. That's even more awkward, since you could do This is leading me to think that maybe this operation should really be a different feature. How about this builtin function: var array: [64]u32 = ...;
var first_half: *[32]u32 = @arrayPtr(array, 0, 32);
var first_half: *[32]u32 = @arrayPtr(array, 32, 32);
// pseudocode signature:
fn @arrayPtr(array: [n]T, start: usize, comptime len: usize) *[len]T; I can almost imagine this being implemented in userspace, except for some complications with propagating |
I think #863 is related |
I went ahead and implemented a variation of PS: Forgive the non-standard formatting, as I'm an Allman native.
|
It should be noted that this has long since been included in |
I'm fairly certain this is a duplicate of the accepted proposal #863 |
I ran into a wall when trying to implement x25519 without being able to slice-and-dice arrays into smaller arrays. (without
@ptrCast
)The text was updated successfully, but these errors were encountered: