Skip to content

Commit

Permalink
Fix protect Array#map against concurrent mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed Nov 5, 2024
1 parent 4796f1e commit 0fc1860
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/array.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1113,7 +1113,9 @@ class Array(T)

# Optimized version of `Enumerable#map`.
def map(& : T -> U) : Array(U) forall U
Array(U).new(size) { |i| yield @buffer[i] }
map_with_index do |item, _|
yield item
end
end

# Modifies `self`, keeping only the elements in the collection for which the
Expand Down Expand Up @@ -1216,7 +1218,14 @@ class Array(T)
# results # => ["0: crystal", "1: pearl", "2: diamond"]
# ```
def map_with_index(offset = 0, & : T, Int32 -> _)
Array.new(size) { |i| yield @buffer[i], offset + i }
Array(U).build(size) do |build_buffer|

Check failure on line 1221 in src/array.cr

View workflow job for this annotation

GitHub Actions / LLVM 15

undefined constant U

Check failure on line 1221 in src/array.cr

View workflow job for this annotation

GitHub Actions / LLVM 16

undefined constant U

Check failure on line 1221 in src/array.cr

View workflow job for this annotation

GitHub Actions / LLVM 14

undefined constant U

Check failure on line 1221 in src/array.cr

View workflow job for this annotation

GitHub Actions / LLVM 19

undefined constant U

Check failure on line 1221 in src/array.cr

View workflow job for this annotation

GitHub Actions / LLVM 13

undefined constant U

Check failure on line 1221 in src/array.cr

View workflow job for this annotation

GitHub Actions / x86_64-mingw-w64-cross-compile

undefined constant U

Check failure on line 1221 in src/array.cr

View workflow job for this annotation

GitHub Actions / LLVM 17

undefined constant U

Check failure on line 1221 in src/array.cr

View workflow job for this annotation

GitHub Actions / LLVM 18

undefined constant U

Check failure on line 1221 in src/array.cr

View workflow job for this annotation

GitHub Actions / x86_64-windows-release / build

undefined constant U
i = 0
while i < size
build_buffer[i] = yield @buffer[i], offset + i
i &+= 1
end
i
end
end

# Returns an `Array` with the first *count* elements removed
Expand Down

0 comments on commit 0fc1860

Please sign in to comment.