Skip to content

Commit

Permalink
Adapt to static arrays (#59)
Browse files Browse the repository at this point in the history
Uses package extensions so that it does not add the dependency. Is setup for backwards compatibility to pre-1.9 versions.
  • Loading branch information
ChrisRackauckas authored Feb 20, 2023
1 parent 3d592b4 commit 60c4106
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
11 changes: 10 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@ version = "3.5.0"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"

[compat]
Requires = "1"
julia = "1.6"

[extensions]
AdaptStaticArraysExt = "StaticArrays"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

[targets]
test = ["Test"]
test = ["StaticArrays", "Test"]

[weakdeps]
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
8 changes: 8 additions & 0 deletions ext/AdaptStaticArraysExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module AdaptStaticArraysCoreExt

using Adapt
isdefined(Base, :get_extension) ? (using StaticArrays) : (using ..StaticArrays)

Adapt.adapt_storage(::Type{<:SArray{S}}, xs::Array) where {S} = SArray{S}(xs)

end
7 changes: 7 additions & 0 deletions src/Adapt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,11 @@ include("arrays.jl")
# helpers
include("macro.jl")

import Requires
@static if !isdefined(Base, :get_extension)
function __init__()
Requires.@require StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" begin include("../ext/AdaptStaticArraysExt.jl") end
end
end

end # module
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,8 @@ end
bc = Base.broadcasted(f(mat.arr), (mat.arr,))
@test typeof(copy(adapt(CustomArray, bc))) == typeof(broadcast(f(mat), (mat,)))
end

@testset "StaticArrays" begin
using StaticArrays
adapt(SArray, [1,2,3]) isa SArray
end

0 comments on commit 60c4106

Please sign in to comment.