Skip to content

Commit

Permalink
Replace calls to StaticArray(UInt8, 1)#unsafe_as(UInt8) (#14685)
Browse files Browse the repository at this point in the history
It's unnecessary to use `unsafe_as` to retrieve the value of a single-item `StaticArray`.
However, the entire `StaticArray` is unnecessary because we just need a slice pointing to the location of the value.
The resulting machinecode should be identical in all three variants.
  • Loading branch information
straight-shoota authored Jun 12, 2024
1 parent ef04b2e commit c141700
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/crystal/system/unix/getrandom.cr
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ module Crystal::System::Random
init unless @@initialized

if @@getrandom_available
buf = uninitialized UInt8[1]
getrandom(buf.to_slice)
buf.unsafe_as(UInt8)
buf = uninitialized UInt8
getrandom(pointerof(buf).to_slice(1))
buf
elsif urandom = @@urandom
urandom.read_byte.not_nil!
else
Expand Down
6 changes: 3 additions & 3 deletions src/crystal/system/wasi/random.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ module Crystal::System::Random
end

def self.next_u : UInt8
buf = uninitialized UInt8[1]
random_bytes(buf.to_slice)
buf.unsafe_as(UInt8)
buf = uninitialized UInt8
random_bytes(pointerof(buf).to_slice(1))
buf
end
end
6 changes: 3 additions & 3 deletions src/crystal/system/win32/random.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ module Crystal::System::Random
end

def self.next_u : UInt8
buf = uninitialized UInt8[1]
random_bytes(buf.to_slice)
buf.unsafe_as(UInt8)
buf = uninitialized UInt8
random_bytes(pointerof(buf).to_slice(1))
buf
end
end

0 comments on commit c141700

Please sign in to comment.