Skip to content

Commit dab4c04

Browse files
authored
Add Adapt.jl package extension to construct FixedSizeArrays which preserve wrapper types (#149)
* Add Adapt extension * Add adapt documentation * Add adapt extension tests
1 parent 663a708 commit dab4c04

File tree

6 files changed

+42
-0
lines changed

6 files changed

+42
-0
lines changed

Project.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ Collects = "08986516-18db-4a8b-8eaa-f5ef1828d8f1"
88

99
[weakdeps]
1010
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
11+
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
1112

1213
[extensions]
1314
RandomExt = "Random"
15+
AdaptExt = "Adapt"
1416

1517
[compat]
18+
Adapt = "4"
1619
Collects = "1"
1720
Random = "1"
1821
julia = "1.10"

docs/Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[deps]
2+
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
23
Changelog = "5217a498-cd5d-4ec6-b8c2-9b85a09b6e3e"
34
Collects = "08986516-18db-4a8b-8eaa-f5ef1828d8f1"
45
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
@@ -8,6 +9,7 @@ FixedSizeArrays = "3821ddf9-e5b5-40d5-8e25-6813ab96b5e2"
89
FixedSizeArrays = {path = ".."}
910

1011
[compat]
12+
Adapt = "4"
1113
Changelog = "1.1"
1214
Collects = "1"
1315
Documenter = "1"

docs/src/usage.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@ julia> FixedSizeArray{Float64}(arr) # construct from an `AbstractArray` value w
3434
30.0 14.0
3535
```
3636

37+
To "convert" a wrapper type like `Adjoint` while preserving the wrapper structure, you can use `Adapt`:
38+
39+
```jldoctest
40+
julia> using FixedSizeArrays, Adapt
41+
42+
julia> arr = transpose([10 20; 30 14])
43+
2×2 transpose(::Matrix{Int64}) with eltype Int64:
44+
10 30
45+
20 14
46+
47+
julia> adapt(FixedSizeArray, arr)
48+
2×2 transpose(::FixedSizeArray{Int64, 2, Memory{Int64}}) with eltype Int64:
49+
10 30
50+
20 14
51+
```
52+
3753
For 1- and 2-dimensional arrays you can use the aliases [`FixedSizeVector`](@ref) and [`FixedSizeMatrix`](@ref), respectively:
3854

3955
```@repl

ext/AdaptExt.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module AdaptExt
2+
3+
using Adapt
4+
using FixedSizeArrays: FixedSizeArray
5+
6+
Adapt.adapt_storage(fixed::Type{<:FixedSizeArray}, A) = fixed(A)
7+
8+
end # module AdaptExt

test/Project.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
[compat]
2+
Adapt = "4"
23
Aqua = "0.8"
34
Collects = "1"
5+
LinearAlgebra = "1"
46
OffsetArrays = "1.14"
57
Random = "1.10"
68
Test = "1.10"
79

810
[deps]
11+
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
912
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
1013
Collects = "08986516-18db-4a8b-8eaa-f5ef1828d8f1"
14+
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1115
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
1216
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1317
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

test/runtests.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Test
2+
using LinearAlgebra, Adapt
23
using FixedSizeArrays
34
using Collects: collect_as
45
using OffsetArrays: OffsetArray
@@ -669,4 +670,12 @@ end
669670
@test v == fv
670671
end
671672
end
673+
674+
@testset "Adapt" begin
675+
# Preserver wrapper types:
676+
arr = transpose([10 20; 30 14])
677+
farr = adapt(FixedSizeArray, arr)
678+
@test farr isa Transpose
679+
@test parent(farr) isa FixedSizeArray
680+
end
672681
end

0 commit comments

Comments
 (0)