Skip to content

Commit 2779f31

Browse files
committed
feat: add extension for FixedSizeArrays (collect_structarray initializer)
1 parent f504f68 commit 2779f31

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

Project.toml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
99

1010
[weakdeps]
1111
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
12+
FixedSizeArrays = "3821ddf9-e5b5-40d5-8e25-6813ab96b5e2"
1213
GPUArraysCore = "46192b85-c4d5-4398-a991-12ede77f4527"
13-
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1414
KernelAbstractions = "63c18a36-062a-441e-b654-da1e3ab1ce7c"
15+
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1516
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
1617
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
1718

@@ -21,18 +22,20 @@ StructArraysGPUArraysCoreExt = ["GPUArraysCore", "KernelAbstractions"]
2122
StructArraysLinearAlgebraExt = "LinearAlgebra"
2223
StructArraysSparseArraysExt = "SparseArrays"
2324
StructArraysStaticArraysExt = "StaticArrays"
25+
StructArraysFixedSizeArraysExt = "FixedSizeArrays"
2426

2527
[compat]
2628
Adapt = "4"
2729
Aqua = "0.8"
2830
ConstructionBase = "1"
2931
DataAPI = "1"
3032
Documenter = "1"
33+
FixedSizeArrays = "1.2.0"
3134
GPUArraysCore = "0.2"
3235
InfiniteArrays = "0.15"
3336
JLArrays = "0.2"
34-
LinearAlgebra = "1"
3537
KernelAbstractions = "0.9"
38+
LinearAlgebra = "1"
3639
OffsetArrays = "1"
3740
PooledArrays = "1"
3841
SparseArrays = "1"
@@ -47,11 +50,12 @@ julia = "1.10"
4750
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
4851
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
4952
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
53+
FixedSizeArrays = "3821ddf9-e5b5-40d5-8e25-6813ab96b5e2"
5054
GPUArraysCore = "46192b85-c4d5-4398-a991-12ede77f4527"
5155
InfiniteArrays = "4858937d-0d70-526a-a4dd-2d5cb5dd786c"
5256
JLArrays = "27aeb0d3-9eb9-45fb-866b-73c2ecf80fcb"
53-
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
5457
KernelAbstractions = "63c18a36-062a-441e-b654-da1e3ab1ce7c"
58+
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
5559
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
5660
PooledArrays = "2dfb63ee-cc39-5dd5-95bd-886bf059d720"
5761
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
@@ -61,4 +65,4 @@ TypedTables = "9d95f2ec-7b3d-5a63-8d20-e2491e220bb9"
6165
WeakRefStrings = "ea10d353-3f73-51f8-a26c-33c1cb351aa5"
6266

6367
[targets]
64-
test = ["Adapt", "Aqua", "Documenter", "GPUArraysCore", "InfiniteArrays", "JLArrays", "LinearAlgebra", "KernelAbstractions", "OffsetArrays", "PooledArrays", "SparseArrays", "StaticArrays", "Test", "TypedTables", "WeakRefStrings"]
68+
test = ["Adapt", "Aqua", "Documenter", "FixedSizeArrays", "GPUArraysCore", "InfiniteArrays", "JLArrays", "LinearAlgebra", "KernelAbstractions", "OffsetArrays", "PooledArrays", "SparseArrays", "StaticArrays", "Test", "TypedTables", "WeakRefStrings"]
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module StructArraysFixedSizeArraysExt
2+
3+
using StructArrays
4+
using FixedSizeArrays: FixedSizeArrayDefault
5+
6+
export fixed_size_array_backed_initializer
7+
8+
fixed_size_array_of(::Type{S}, d::NTuple{N, Any}) where {S, N} = similar(FixedSizeArrayDefault{S, N}, d)
9+
10+
const fixed_size_array_backed_initializer = StructArrays.StructArrayInitializer(StructArrays.alwaysfalse, fixed_size_array_of)
11+
12+
# Make `fixed_size_array_backed_initializer` available in `StructArrays` using this hack
13+
# https://github.com/itsdfish/PackageExtensionsExample.jl/blob/41581ab0371cf512ace5dd063f7e7935effbb256/ext/DistributionsExt.jl#L28
14+
function __init__()
15+
Threads.@spawn begin
16+
sleep(0.01)
17+
Core.eval(
18+
StructArrays,
19+
quote
20+
ext = Base.get_extension(StructArrays, :StructArraysFixedSizeArraysExt)
21+
using .ext
22+
VERSION >= v"1.11.0-DEV.469" && eval(Meta.parse("public fixed_size_array_backed_initializer"))
23+
end
24+
)
25+
end
26+
end
27+
28+
end

test/runtests.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ using LinearAlgebra
1111
using Test
1212
using SparseArrays
1313
using InfiniteArrays
14+
using FixedSizeArrays
1415

1516
import Aqua
1617
import KernelAbstractions as KA
@@ -1605,6 +1606,18 @@ end
16051606
end
16061607
end
16071608

1609+
@testset "FixedSizeArray" begin
1610+
itr = (ComplexF64(x, x) for x in 1:10)
1611+
a = collect_structarray(
1612+
itr;
1613+
initializer = StructArrays.fixed_size_array_backed_initializer,
1614+
)
1615+
@test a isa StructArray
1616+
@test a.re isa FixedSizeArray
1617+
b = collect_structarray(itr)
1618+
@test all(a .== b)
1619+
end
1620+
16081621
@testset "project quality" begin
16091622
Aqua.test_all(StructArrays)
16101623
end

0 commit comments

Comments
 (0)